OpenJDK7プロジェクトjava.nio.file.Filesには、以下の関数があります。私の質問は、whileループ条件を>ではなく>=にする必要があるかどうかです。これは、source.read javadocが、EOFに達すると、0ではなく-1を返すと言っているためです。
/**
* 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;
}