javainputstreambufferedinputstream

Difference between InputStream.read(byte b) and BufferedInputStream in Java


I just saw that the InputStream (link to Java 11 API) class has a method read(byte[] b) to read the data stream byte wise. Isn't that a kind of "buffered reading"?

Further I saw, that the BufferedInputStream (link to Java 11 API) does not have an own implementation of read(byte[] b). It is using the method of its parent class FilterInputStream.

So, does the InputStream class also support buffered reading? And where is the difference to the class BufferedInputStream?

Edit

Corrected "read(byte b)" to "read(byte[] b)".


Solution

  • I will assume you mean byte[] b, not byte b.

    As per the Javadoc, the default implementation for read(byte[] b) is simply calling read(b, 0, b.length). As this method is overridden in the BufferedInputStream, you can say that read(byte[] b) is, for all intents and purposes, also overridden.

    The additional functionality provided by BufferedInputStream is support for the mark and reset methods, which effectively allows you to bookmark a point in the stream and re-read from that bookmark. The buffer maintains the bytes required to support this operation. Otherwise, it will simply read what is available at the time, without any buffering.