The javadoc for java.io.EOFException states:
"Signals that an end of file or end of stream has been reached unexpectedly
during input.
This exception is mainly used by data input streams, which generally expect a
binary file in a specific format, and for which an end of stream is an unusual
condition. Most other input streams return a special value on end of stream."
However, DataInputStream.readInt, DataInputStream.readBoolean, etc. have no
way of signalling an end-of-file condition except by throwing this exception.
A typical reading loop from a DataInput stream is:
try {
for (;;) {
int i = in.readInt();
// ...
}
} catch (EOFException e) {
in.close();
} catch (IOException e) {
// ... handle an error
}
This is in contrast to the typical InputStream or Reader loop:
try {
while ((ch = in.read()) != -1)
// ...
in.close();
} catch (IOException e) {
// ... handle an error
}
This behavior is noted in the Java Programming Language, 2nd Edition, Gosling/
Arnold, Sec. 12.21, p. 256.
Since an EOFException can be thrown in this expected situation, the javadoc
should be updated to reflect this.
"Signals that an end of file or end of stream has been reached unexpectedly
during input.
This exception is mainly used by data input streams, which generally expect a
binary file in a specific format, and for which an end of stream is an unusual
condition. Most other input streams return a special value on end of stream."
However, DataInputStream.readInt, DataInputStream.readBoolean, etc. have no
way of signalling an end-of-file condition except by throwing this exception.
A typical reading loop from a DataInput stream is:
try {
for (;;) {
int i = in.readInt();
// ...
}
} catch (EOFException e) {
in.close();
} catch (IOException e) {
// ... handle an error
}
This is in contrast to the typical InputStream or Reader loop:
try {
while ((ch = in.read()) != -1)
// ...
in.close();
} catch (IOException e) {
// ... handle an error
}
This behavior is noted in the Java Programming Language, 2nd Edition, Gosling/
Arnold, Sec. 12.21, p. 256.
Since an EOFException can be thrown in this expected situation, the javadoc
should be updated to reflect this.