Tim Holloway wrote:If this method had been invented in the Java era, "no more data" would probably be an exception
I partly disagree. Yes, there should probably be an exception if a read is attempted when no more data is available (there is in fact class java.io.EOFException). However, there should also be a way of detecting EOF in the first place, otherwise you would have to use the EOFException as the "stop reading" signal. That could be done by adding a method eof()
or something like that that can return when EOF is reached. This would then lead to this:
* It's now possible to declare the variables required for reading inside the method body. For performance reasons the byte should probably still be declared outside though.
* A single byte read can return a byte because there is no more need for 257 values (-1 plus 0-255).
* A multiple byte read still needs to return the number of bytes read, because eof()
only tells you that the end of the stream hasn't been reached yet, but not how many are available. (Method available()
still shouldn't be used, because this information may simply be unknown.)