Well, first you made a false dichotomy. One completely normal circumstance is that the buffer will not be full because there are not many bytes left in the file. This is not an IOException
, but this does not mean that the contents of the entire file has not been read.
The spectrum says that the method will either return -1, indicating the end of the stream, or block until at least one byte is read. Artists of InputStream
can optimize as they match (for example, a TCP stream can return data as soon as a packet arrives, regardless of the choice of buffer size). A FileInputStream
can fill a buffer with a single data block. As the caller, you have no idea, except when the method returns -1
, you need to continue reading.
Edit
In practice, in your example, the only circumstance that I will see where the buffer will not be filled (with the standard implementation) is if the file size has changed after you allocated the buffer, but before you start reading it. Since you did not lock the file, this is possible.
Mark peters
source share