Does it make sense to always wrap an InputStream as BufferedInputStream, when I know whether the given InputStream is something other than buffered?
No.
It makes sense if you are likely to perform lots of small reads (one byte or a few bytes at a time), or if you want to use some of the higher level functionality offered by the buffered APIs; for example the BufferedReader.readLine()
method.
However, if you are only going to perform large block reads using the read(byte[])
and / or read(byte[], int, int)
methods, wrapping the InputStream
in a BufferedInputStream
does not help.
(In response to @Peter Tillman's comment on his own Answer, the block read use-cases definitely represent more than 0.1% of uses of InputStream
classes!! However, he is correct in the sense that it is usually harmless to use a buffered API when you don't need to.)
BufferedInputStream
adds over a plainInputStream
. This might be true from an API sense, but as other have said,BufferedInputStream
takes care of buffering reads for you. Reading byte-at-a-time from a bareFileInputStream
is 40x slower than reading from one wrapped in aBufferedInputStream
. That said, return theInputStream
and keep your method signature as such. Users can wrap if they wish. – Iatry