Skip to content

Commit

Permalink
Merge pull request #27043 from geoand/transfer-to
Browse files Browse the repository at this point in the history
Use InputStream.transferTo instead of manually using a byte array
  • Loading branch information
gsmet authored Aug 1, 2022
2 parents 58f9efc + 711256e commit 34f683a
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ public File readFrom(Class<File> type, Type genericType,
}

try (OutputStream output = new BufferedOutputStream(new FileOutputStream(downloadedFile))) {
int read;
final byte[] buf = new byte[2048];
while ((read = entityStream.read(buf)) != -1) {
output.write(buf, 0, read);
}
entityStream.transferTo(output);
}

return downloadedFile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ public void writeTo(InputStream inputStream, Class<?> type, Type genericType, An

protected void writeTo(InputStream inputStream, OutputStream entityStream) throws IOException {
try {
byte[] buffer = new byte[8192];
int c;
while ((c = inputStream.read(buffer)) != -1) {
entityStream.write(buffer, 0, c);
}
inputStream.transferTo(entityStream);
} finally {
try {
inputStream.close();
Expand Down

0 comments on commit 34f683a

Please sign in to comment.