13

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

Chatterjee
  • 299
  • 2
  • 9
  • 3
    It sounds more or less right, but you should rely on what it says in the Javadoc, not what anybody else says. Implementations are obliged to do what it says in the Javadoc. You left out `ObjectOutputStream,` which both is and isn't buffered, depending on what exactly it's doing. You also left out the Filter streams and readers. – user207421 Oct 18 '12 at 01:24
  • I like this question. No problem to solve, just seeking understanding. That's refreshing. – John Kugelman Oct 18 '12 at 01:26
  • I agree the docs are unclear about what exactly is getting buffered for example [for `OutputStreamWriter`](http://docs.oracle.com/javase/7/docs/api/java/io/OutputStreamWriter.html). – Dave L. Oct 18 '12 at 02:02
  • @EJP: Just confirming the rest of the list: PushBackInputStream and PushBackReader are obviously buffered. However, I can't determine whether LineNumberInputStream, DataInputStream and DataOutputStream, FilterInputStream and FilterOutputStream are buffered or not. Also, what I found strange was that Buffered(Input/Output)Stream is a subclass of Filter(Input/Output)Stream. However, in case of Buffered(Reader/Writer), it is not a subclass of Filter(Reader/Writer) and instead directly extends (Reader/Writer). I was expecting an analogy here. – Chatterjee Oct 19 '12 at 02:54
  • @EJP: Finally, in case of ObjectOutputStream, what did you mean by "both is and isn't buffered, depending on what exactly it's doing"? Also, what about ObjectInputStream? Sorry for the inconvenience. – Chatterjee Oct 19 '12 at 02:55
  • `ObjectOutputStream` is buffered while you are sending primitives, but sending an object causes a flush. `ObjectInputStream` is buffered. I believe *all* the `Reader` and `Writer` classes are buffered. – user207421 Mar 30 '21 at 22:56

1 Answers1

2

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.

romacafe
  • 3,098
  • 2
  • 23
  • 27