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
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.
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.