Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Buffered and Unbuffered Streams in Java

I was going through some of the documentation on Java IO and just wanted to make sure whether I get this right:

Unbuffered Input Streams: FileInputStream, InputStreamReader, FileReader

Unbuffered Output Streams: FileOutputStream, OutputStreamWriter, FileWriter

Buffered Output Streams: PrintStream, PrintWriter

In addition, we have the BufferedInputStream, BufferedOutputStream, BufferedReader and BufferedWriter streams to convert the unbuffered streams into buffered versions.

Finally, I observed that for the Character Streams, viz. InputStreamReader, FileReader, OutputStreamWriter, FileWriter, an internal byte-buffer is maintained for the bytes before they are sent into the stream. This byte-buffer is not under our control. Hence, for Character Streams, buffering refers to the high-level character buffer for storing the characters coming in and going out of the program.

Is everything I said correct?

P.S. - I understand that this buffering issue is somewhat implementation dependent, but I just wish to confirm what the javadocs are saying

like image 340
Chatterjee Avatar asked Oct 18 '12 01:10

Chatterjee


People also ask

What are buffered I/O streams in Java?

To reduce this kind of overhead, the Java platform implements buffered I/O streams. Buffered input streams read data from a memory area known as a buffer; the native input API is called only when the buffer is empty. Similarly, buffered output streams write data to a buffer, and the native output API is called only when the buffer is full.

What is bufferedoutputstream in Java?

Java BufferedOutputStream Class Java BufferedOutputStream class is used for buffering an output stream. It internally uses buffer to store data. It adds more efficiency than to write data directly into a stream.

What is the difference between unbuffered stream and buffered stream?

Similarly, buffered output streams write data to a buffer, and the native output API is called only when the buffer is full. A program can convert an unbuffered stream into a buffered stream using the wrapping idiom we've used several times now, where the unbuffered stream object is passed to the constructor for a buffered stream class.

What is the difference between native and buffered input streams?

Buffered input streams read data from a memory area known as a buffer; the native input API is called only when the buffer is empty. Similarly, buffered output streams write data to a buffer, and the native output API is called only when the buffer is full.


1 Answers

Rules of thumb:

  1. Any InputStream / Reader that reads directly from an external source (FileInputStream, SocketInputStream, etc.) is 'raw' and considered unbuffered. (Though in reality, there is probably some buffering going on, depends on the implementation)

  2. Any 'raw' InputStream or Reader can be buffered by a BufferedInputStream or BufferedReader.

  3. Same assumptions for OuputStreams / Writers.

  4. Other stream decorators (i.e. GZIPInputStream, MD5InputStream, YourSpecialObjectWriter) probably do some buffering, but its not very harmful to buffer the source.

like image 127
romacafe Avatar answered Oct 17 '22 11:10

romacafe