You may not always need buffering so, for that, the answer would be No, in some cases it's just overhead.
There is another reason it is "No" and it can be more serious. BufferedInputStream
(or BufferedReader
) can cause unpredictable failures when used with network socket when you also have enabled a timeout on the socket. The timeout can occur while reading a packet. You would no longer be able to access the data that were transferred to that point - even if you knew that there was some non-zero number of bytes (see java.net.SocketTimeoutException
which is a subclass of java.io.InterruptedIOException
so has bytesTransferred
variable available).
If you are wondering how a socket timeout could occur while reading, just think of calling the read(bytes[])
method and the original packet that contains the message ended up being split but one of the partial packets is delayed beyond the timeout (or the remaining portion of the timeout). This can happen more frequently when wrapped again in something that implements java.io.DataInput
(any of the reads for multiple byte values, like readLong()
or readFully()
or the BufferedReader.readLine()
method.
Note that java.io.DataInputStream
also is a bad candidate for socket streams that have a timeout since it doesn't behave well with timeout exceptions either.