8193842: Replace Files.copy(InputStream,OutputStream) with InputStream.transferTo(OutputStream)

Reviewed-by: clanger, alanb
This commit is contained in:
Brian Burkhalter 2017-12-20 08:05:04 -08:00
parent ede41aa311
commit c4f1bb0019

View file

@ -2954,22 +2954,6 @@ public final class Files {
return newBufferedWriter(path, StandardCharsets.UTF_8, options); return newBufferedWriter(path, StandardCharsets.UTF_8, options);
} }
/**
* 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 * Copies all bytes from an input stream to a file. On return, the input
* stream will be at end of stream. * stream will be at end of stream.
@ -3082,7 +3066,7 @@ public final class Files {
// do the copy // do the copy
try (OutputStream out = ostream) { try (OutputStream out = ostream) {
return copy(in, out); return in.transferTo(out);
} }
} }
@ -3124,7 +3108,7 @@ public final class Files {
Objects.requireNonNull(out); Objects.requireNonNull(out);
try (InputStream in = newInputStream(source)) { try (InputStream in = newInputStream(source)) {
return copy(in, out); return in.transferTo(out);
} }
} }