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

后端 未结 5 1633
生来不讨喜
生来不讨喜 2020-12-05 02:42

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 o         


        
5条回答
  •  攒了一身酷
    2020-12-05 02:47

    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.

提交回复
热议问题