Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you decide what byte[] size to use for InputStream.read()?

When reading from InputStreams, how do you decide what size to use for the byte[]?

int nRead; byte[] data = new byte[16384]; // <-- this number is the one I'm wondering about  while ((nRead = is.read(data, 0, data.length)) != -1) {   ...do something.. } 

When do you use a small one vs a large one? What are the differences? Does the number want to be in increments of 1024? Does it make a difference if it is an InputStream from the network vs the disk?

Thanks much, I can't seem to find a clear answer elsewhere.

like image 880
cottonBallPaws Avatar asked Jan 05 '12 20:01

cottonBallPaws


People also ask

How does InputStream read () method work?

read() method reads the next byte of the data from the the input stream and returns int in the range of 0 to 255. If no byte is available because the end of the stream has been reached, the returned value is -1.

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

You use the buffered input stream to read as many bytes as the bytes[] array size. You consume the bytes read and then move on to reading more bytes from the file. Hence you don't need know the file size in order to read it.


2 Answers

Most people use powers of 2 for the size. If the buffer is at least 512 bytes, it doesn't make much difference ( < 20% )

For network the optimal size can be 2 KB to 8 KB (The underlying packet size is typically up to ~1.5 KB) For disk access, the fastest size can be 8K to 64 KB. If you use 8K or 16K you won't have a problem.

Note for network downloads, you are likely to find you usually don't use the whole buffer. Wasting a few KB doesn't matter much for 99% of use cases.

like image 120
Peter Lawrey Avatar answered Oct 09 '22 21:10

Peter Lawrey


In that situation, I always use a reasonable power of 2, somewhere in the range of 2K to 16K. In general, different InputStreams will have different optimal values, but there is no easy way to determine the value.

In order to determine the optimal value, you'd need to understand more about the exact type of InputStream you are dealing with, as well as things like the specifications of the hardware that are servicing the InputStream.

Worrying about this is probably a case of premature optimization.

like image 42
JohnnyO Avatar answered Oct 09 '22 20:10

JohnnyO