8080272: Refactor I/O stream copying to use InputStream.transferTo/readAllBytes and Files.copy

Reviewed-by: mcimadamore, alanb
This commit is contained in:
Andrey Turbanov 2021-03-16 10:10:05 +00:00 committed by Julia Boes
parent a31a23d5e7
commit 68deb24b38
7 changed files with 27 additions and 102 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -1217,7 +1217,6 @@ public class FtpClient extends sun.net.ftp.FtpClient {
* @throws IOException if the transfer fails.
*/
public sun.net.ftp.FtpClient getFile(String name, OutputStream local) throws sun.net.ftp.FtpProtocolException, IOException {
int mtu = 1500;
if (restartOffset > 0) {
Socket s;
try {
@ -1227,27 +1226,15 @@ public class FtpClient extends sun.net.ftp.FtpClient {
}
issueCommandCheck("RETR " + name);
getTransferSize();
InputStream remote = createInputStream(s.getInputStream());
byte[] buf = new byte[mtu * 10];
int l;
while ((l = remote.read(buf)) >= 0) {
if (l > 0) {
local.write(buf, 0, l);
}
try (InputStream remote = createInputStream(s.getInputStream())) {
remote.transferTo(local);
}
remote.close();
} else {
Socket s = openDataConnection("RETR " + name);
getTransferSize();
InputStream remote = createInputStream(s.getInputStream());
byte[] buf = new byte[mtu * 10];
int l;
while ((l = remote.read(buf)) >= 0) {
if (l > 0) {
local.write(buf, 0, l);
}
try (InputStream remote = createInputStream(s.getInputStream())) {
remote.transferTo(local);
}
remote.close();
}
return completePending();
}
@ -1344,18 +1331,11 @@ public class FtpClient extends sun.net.ftp.FtpClient {
*/
public sun.net.ftp.FtpClient putFile(String name, InputStream local, boolean unique) throws sun.net.ftp.FtpProtocolException, IOException {
String cmd = unique ? "STOU " : "STOR ";
int mtu = 1500;
if (type == TransferType.BINARY) {
Socket s = openDataConnection(cmd + name);
OutputStream remote = createOutputStream(s.getOutputStream());
byte[] buf = new byte[mtu * 10];
int l;
while ((l = local.read(buf)) >= 0) {
if (l > 0) {
remote.write(buf, 0, l);
}
try (OutputStream remote = createOutputStream(s.getOutputStream())) {
local.transferTo(remote);
}
remote.close();
}
return completePending();
}
@ -1373,17 +1353,10 @@ public class FtpClient extends sun.net.ftp.FtpClient {
* @throws IOException if an error occurred during the transmission.
*/
public sun.net.ftp.FtpClient appendFile(String name, InputStream local) throws sun.net.ftp.FtpProtocolException, IOException {
int mtu = 1500;
Socket s = openDataConnection("APPE " + name);
OutputStream remote = createOutputStream(s.getOutputStream());
byte[] buf = new byte[mtu * 10];
int l;
while ((l = local.read(buf)) >= 0) {
if (l > 0) {
remote.write(buf, 0, l);
}
try (OutputStream remote = createOutputStream(s.getOutputStream())) {
local.transferTo(remote);
}
remote.close();
return completePending();
}