8193842: Replace Files.copy(InputStream,OutputStream) with InputStream.transferTo(OutputStream)
Reviewed-by: clanger, alanb
--- a/src/java.base/share/classes/java/nio/file/Files.java Wed Dec 20 15:33:31 2017 +0000
+++ b/src/java.base/share/classes/java/nio/file/Files.java Wed Dec 20 08:05:04 2017 -0800
@@ -2955,22 +2955,6 @@
}
/**
- * 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;
- }
-
- /**
* Copies all bytes from an input stream to a file. On return, the input
* stream will be at end of stream.
*
@@ -3082,7 +3066,7 @@
// do the copy
try (OutputStream out = ostream) {
- return copy(in, out);
+ return in.transferTo(out);
}
}
@@ -3124,7 +3108,7 @@
Objects.requireNonNull(out);
try (InputStream in = newInputStream(source)) {
- return copy(in, out);
+ return in.transferTo(out);
}
}