在 OpenJDK7 项目java.nio.file.Files中,有如下函数。我的问题是,while 循环条件应该是 >= 而不是 >?这是因为 source.read javadoc 说当到达 EOF 时,它将返回 -1 而不是 0。
/**
* Reads all bytes from an input stream and writes them to an output stream.
*/
private static long copy(InputStream source, OutputStream sink)
throws IOException
{
long nread = 0L;
byte[] buf = new byte[BUFFER_SIZE];
int n;
while ((n = source.read(buf)) > 0) {
sink.write(buf, 0, n);
nread += n;
}
return nread;
}