When I copy Inputstream to OutputStream , sometimes I occur the EOFException.
From the document of EOFException may used by data input streams to signal end of stream.
So does it mean that it’s safe to return when a EOFException occurred ?
public static void copy(InputStream in, OutputStream out) throws IOException {
byte[] buff = new byte[BUF_SIZE];
int n = -1;
try {
while ((n = in.read(buff)) >= 0) {
out.write(buff, 0, n);
}
} catch (EOFException eof) {
// reach EOF , return
}
}
EOFException cannot be thrown in InputStream.read(byte[]), its API says IOException is thrown “if the first byte cannot be read for any reason other than the end of the file…”.
EOFException is used for other purposes, eg. java.io.DataInputStream.readFully(byte[] b) may throw EOFException if input stream reaches the end before reading all the bytes.
But, in any case, EOFException indicates that an error occurred, you should not ignore it and return as if nothing happened.