Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I decide how many bytes to read from an inputstream?

I am trying to read from an InputStream. I wrote below code

byte[] bytes = new byte[1024 * 32];
                while (bufferedInStream.read(bytes) != -1) {
                    bufferedOutStream.write(bytes);
                }

What I don't understand is how many bytes I should read in an iteration? The stream contains a file saved on the disk.

I read here but I did not understand the post really.

like image 411
Krishna Chaitanya Avatar asked Dec 06 '22 00:12

Krishna Chaitanya


1 Answers

Say you had a flow of water from a pipe into a bath. You then used a bucket to get water from the bath and carry to say to your garden to water the lawn. The bath is the buffer. When you are walking across the lawn the buffer is filling up so when you return there is a bucket ful for you to take again.

If the bath is tiny then it could overflow while you are walking with the bucket and so you will lose water. If you have a massive bath then it is unlikely to overflow. so a larger buffer is more convenient. but of course a larger bath costs more money and takes up more space.

A buffer in your program takes up memory space. And you don't want to take up all your available memory for your buffer just because it is convenient.

Generally in your read function you can specify how many bytes to read. so even if you have a small buffer you could do this (pseudocode):

const int bufsize = 50;
buf[bufsize];
unsigned read;
while ((read = is.read(buf, bufsize)) != NULL) {
   // do something with data - up to read bytes
}

In above code bufzise is MAXIMUM data to read into the buffer.

If your read function does not allow you to specify a maximum number of bytes to read then you need to supply a buffer large enough to receive the largest possible read amount.

So the optimal buffer size is application specific. Only the application developer will know the characteristics of the data. Eg how fast is the flow of water into the bath. What bath size can you afford (embedded apps), how quickly can you carry bucket from bath across garden and back again.

like image 120
Angus Comber Avatar answered Jan 31 '23 00:01

Angus Comber