java - Is there any advantage to using Buffered Streams when dealing with files if you're reading large chunks of data at a time? -


if i'm dealing files, i'll wrap inputstream , outputstream in bufferedinputstream , bufferedoutputstream - minimize amount of file io operations possible improve performance.

i believe considered practice , recommended.

the standard 'stream copy' code uses when copying inputstream outputstream this:

(assuming they're not using third party library , want remain compatible java 6)

byte[] buffer = new byte[buffer_size]; (int n = 0; (n = input.read(buffer)) != eof; ) {     output.write(buffer, 0, n); } 

what got me thinking is, set buffer_size 8192 (the default size of buffers in bufferedinputstream , bufferedoutputstream).

my question is, still benefical use bufferedinputstream , bufferedoutputstream? i'm not entirely sure how these classes work under hood, , don't know if there advantage using them in situation.

thanks.

in general no, if you're using buffer size isn't power of 2 there might benefit in having reads , writes power-of-2-aligned bufferedinputstream or bufferedoutputstream, default buffer size 8192 bytes. buffers should ideally multiple of file system block size, or better still cluster size.

you should note both these streams have greased paths when transfer larger internal buffer: internal buffer emptied , remainder of transfer direct, rather going through internal buffer , being copied twice. so, there no real disadvantage.


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -