0

The docs for InputStream.read(bytes) say "This method blocks until input data is available, end of file is detected, or an exception is thrown."

However, java.io.FileInputStream extends java.io.InputStream and the docs for FileInputStream.read(bytes) state no such guarantee:

 * Reads up to {@code b.length} bytes of data from this input
 * stream into an array of bytes. This method blocks until some input
 * is available.

In practice, FileInputStream can indeed return before all the input is available, so it seems both the docs and the implementation do not follow the contract of InputStream.

Is this a bug? Is it a known bug?

UPDATE: to clarify: System.in is a FileInputStream. I am seeing System.in.read(buf) return a nonzero int that is neither all of the bytes, nor -1. If I use System.in.readNBytes(buf, 0, theNumberOfBytesIAmExpecting) then I get all the bytes the sender is sending.

2
  • How do you define a File? Commented Feb 11, 2022 at 20:00
  • Louis’s answer is correct. read has never been specified to return any particular number of bytes, in either class. readNBytes is specifically guaranteed to return the number of requested bytes, unless EOF is reached first. Commented Feb 11, 2022 at 20:37

2 Answers 2

2

It is not a contract violation, and is therefore not a bug.

The doc for InputStream specifies that it blocks until "input data is available," which is not the same thing as "all the input is available."

If you're referring to the "end of file" aspect, continue to read the Javadoc for FileInputStream.read, which specifies

the total number of bytes read into the buffer, or -1 if there is no more data because the end of the file has been reached.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your answer. I added a clarification to the question.
Yes. The behavior you describe is perfectly compliant with the contract. Note also the InputStream first sentence: "Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer."
0

I think that the situation is more subtle, - InputStream.read(byte[] b) just delegates to InputStream.read(byte[] b, int off, int len)

And for read(byte[] b, int off, int len) it seems that the contract/promise broken (or at least different).

For InputStream.read(byte[] b, int off, int len) the documentation states:

The default implementation of this method blocks until the requested amount of input data len has been read, end of file is detected, or an exception is thrown. Subclasses are encouraged to provide a more efficient implementation of this method.

(and it is implemented this way: - the requested amount of data is always read)

For FileInputStream.read(byte[] b, int off, int len) - there is no such promise (and implementation may not read all the requested amount).

Many other high-level methods delegate to read(byte[] b, int off, int len) and I would consider that at least for this method the promise of InputStream (to read either the requested amount or till the end of the steam) is broken in FileInputStream and it creates confusion.

2 Comments

Any sentence that starts with “The default implementation of this method” is not part of the contract. The method contract starts with: “Reads up to len bytes of data from the input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read.” That seems very clear to me.
The default implementation working that way is not remotely the same thing as the contract saying that it has to work that way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.