1

googled for it and read many related articles but I didn't really understand how using bufferstream performs better when compared to reading same number of bytes at a time using read method on streams. from what I understand, bufferstream reads many number of bytes at a time from the source stream to save round trips. but how is this different than using read(byte[], index, count) where count = same number of bytes you read using buffer stream. Please help me understand it.

2 Answers 2

5

When reading from many data sources (such as files), the slowest part of the operation is often the execution of each individual read request. So the key is to reduce the number of requests saying 'give me N bytes of data from source XXX'.

In the case of file access, it's very likely to be more efficient to do 1 read of 10 MB than it is to do 10 reads of 1 MB.

What buffering does under the hood is read more than you ask for, anticipating that you might read again shortly afterwards.

So, although you only get back the block of data you asked for, the rest is kept stored in the buffer. That way, when you next request data, the data you want is often already in the buffer, and fewer requests to the underlying data source are needed.

Note that the read-ahead behaviour is typically first performed at the creation of the buffer, or when the first read request is executed. Then the buffer will top itself up with additional read-aheads as required.

Sign up to request clarification or add additional context in comments.

2 Comments

Stream.read(byte[] buffer, offset, count). suppose say count=10MB - in this case, does this read method makes more than 1 call to OS?
Well.. that totally depends what the data source / OS / etc. are. All I know is it's a stream. You could be using files, standard input, any kind of custom stream, under Windows or Linux.. Can't possibly say from the information given! :) The point is that using a stream buffer will serve to reduce the number of calls made to the underlying stream, whatever the implementation / OS happens to be.
1

Usually when dealing with streams of data the underlying transport or media has a defined packet size. This means there's a minimum amount of data you can read with a single request. e.g. Even if you read 2 bytes, a 1k packet is received.

When you're using a buffered stream you'd get 1K of data when you ask for 2 bytes, if you ask for another 2 bytes it'll come from the buffer instead of instigating another request.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.