Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How java.io.Buffer* stream differs from normal streams?

1) How does buffered streams work in background, how do they differ from normal streams and what are the advantage(s) of using them?

2) DataInputStream is also Byte based. But it is having methods to readLine(). What's the point in here?

like image 285
i2ijeya Avatar asked Nov 12 '09 10:11

i2ijeya


People also ask

What is the difference between stream and buffer in Java?

A Buffer is a portion in the memory that is used to store a stream of data from peripheral devices. Then from this buffer this stream of data is collected and stored in variables. A stream can be defined as a continuous flow of data.

What happens when we use a buffered stream instead of a normal stream?

Internally a buffer array is used and instead of reading bytes individually from the underlying input stream enough bytes are read to fill the buffer. This generally results in faster performance as less reads are required on the underlying input stream.

Is buffer the same as stream?

So what is the difference between Stream & Buffer? A buffer has a specified, definite length whereas a stream does not. A stream is a sequence of bytes that is read and/or written to, while a buffer is a sequence of bytes that is stored.

What is the main purpose of buffered input output stream in Java?

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

From the BufferedInputStream javadoc:

A BufferedInputStream adds functionality to another input stream-namely, the ability to buffer the input and to support the mark and reset methods. When the BufferedInputStream is created, an internal buffer array is created. As bytes from the stream are read or skipped, the internal buffer is refilled as necessary from the contained input stream, many bytes at a time. The mark operation remembers a point in the input stream and the reset operation causes all the bytes read since the most recent mark operation to be reread before new bytes are taken from the contained input stream.

Internally a buffer array is used and instead of reading bytes individually from the underlying input stream enough bytes are read to fill the buffer. This generally results in faster performance as less reads are required on the underlying input stream.

The opposite is then true for BufferedOutputStream.

mark() and reset() could be used as follows:

1 BufferedInputStream bis = new BufferedInputStream(is);
2 byte[] b = new byte[4];
3 bis.read(b); // read 4 bytes into b
4 bis.mark(10); // mark the stream at the current position - we can read 10 bytes before the mark point becomes invalid
5 bis.read(b); // read another 4 bytes into b
6 bis.reset(); // resets the position in the stream back to when mark was called
7 bis.read(b); // re-read the same 4 bytes as line 5 into b

To explain mark/reset some more...

The BufferInputStream internally remembers the current position in the buffer. As you read bytes the position will increment. A call to mark(10) will save the current position. Subsequent calls to read will continue to increment the current position but a call to reset will set the current position back to its value when mark was called.

The argument to mark specifies how many bytes you can read after calling mark before the mark position gets invalidated. Once the mark position is invalidated you can no longer call reset to return to it.

For example, if mark(2) had been used in line 4 above an IOException would be thrown when reset() is called on line 6 as the mark position would have been invalidated since we read more than 2 bytes.

like image 50
Mark Avatar answered Oct 13 '22 02:10

Mark