The abstract InputStream.read()
method reads a single byte from an input source, and returns its value as an int
, in the range 0 to 255. It will return -1 only if the end of the input stream has been reached. The similar Reader.read()
method reads a single character, and returns its value as an int
, in the ragen range 0-65,535. It also returns -1 only if the end of the stream has been reached. Both methods are meant to be overridden by subclasses.
These methods are often used to read a byte or char from a stream. Unfortunately many programmers eagerly prematurely convert the resulting int
back to a byte
or char
before checking the value for -1. It is vital to check the return value for -1 before narrowing it to a byte
or char
.
...