Skip to content

Commit 68deb24

Browse files
turbanoffJulia Boes
authored and
Julia Boes
committed
8080272: Refactor I/O stream copying to use InputStream.transferTo/readAllBytes and Files.copy
Reviewed-by: mcimadamore, alanb
1 parent a31a23d commit 68deb24

File tree

7 files changed

+27
-102
lines changed

7 files changed

+27
-102
lines changed

src/java.base/share/classes/java/util/jar/JarInputStream.java

+2-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -90,7 +90,7 @@ private JarEntry checkManifest(JarEntry e)
9090
{
9191
if (e != null && JarFile.MANIFEST_NAME.equalsIgnoreCase(e.getName())) {
9292
man = new Manifest();
93-
byte bytes[] = getBytes(new BufferedInputStream(this));
93+
byte[] bytes = readAllBytes();
9494
man.read(new ByteArrayInputStream(bytes));
9595
closeEntry();
9696
if (doVerify) {
@@ -102,18 +102,6 @@ private JarEntry checkManifest(JarEntry e)
102102
return e;
103103
}
104104

105-
private byte[] getBytes(InputStream is)
106-
throws IOException
107-
{
108-
byte[] buffer = new byte[8192];
109-
ByteArrayOutputStream baos = new ByteArrayOutputStream(2048);
110-
int n;
111-
while ((n = is.read(buffer, 0, buffer.length)) != -1) {
112-
baos.write(buffer, 0, n);
113-
}
114-
return baos.toByteArray();
115-
}
116-
117105
/**
118106
* Returns the {@code Manifest} for this JAR file, or
119107
* {@code null} if none.

src/java.base/share/classes/sun/net/ftp/impl/FtpClient.java

+9-36
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2009, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -1217,7 +1217,6 @@ public sun.net.ftp.FtpClient setRestartOffset(long offset) {
12171217
* @throws IOException if the transfer fails.
12181218
*/
12191219
public sun.net.ftp.FtpClient getFile(String name, OutputStream local) throws sun.net.ftp.FtpProtocolException, IOException {
1220-
int mtu = 1500;
12211220
if (restartOffset > 0) {
12221221
Socket s;
12231222
try {
@@ -1227,27 +1226,15 @@ public sun.net.ftp.FtpClient getFile(String name, OutputStream local) throws sun
12271226
}
12281227
issueCommandCheck("RETR " + name);
12291228
getTransferSize();
1230-
InputStream remote = createInputStream(s.getInputStream());
1231-
byte[] buf = new byte[mtu * 10];
1232-
int l;
1233-
while ((l = remote.read(buf)) >= 0) {
1234-
if (l > 0) {
1235-
local.write(buf, 0, l);
1236-
}
1229+
try (InputStream remote = createInputStream(s.getInputStream())) {
1230+
remote.transferTo(local);
12371231
}
1238-
remote.close();
12391232
} else {
12401233
Socket s = openDataConnection("RETR " + name);
12411234
getTransferSize();
1242-
InputStream remote = createInputStream(s.getInputStream());
1243-
byte[] buf = new byte[mtu * 10];
1244-
int l;
1245-
while ((l = remote.read(buf)) >= 0) {
1246-
if (l > 0) {
1247-
local.write(buf, 0, l);
1248-
}
1235+
try (InputStream remote = createInputStream(s.getInputStream())) {
1236+
remote.transferTo(local);
12491237
}
1250-
remote.close();
12511238
}
12521239
return completePending();
12531240
}
@@ -1344,18 +1331,11 @@ public OutputStream putFileStream(String name, boolean unique)
13441331
*/
13451332
public sun.net.ftp.FtpClient putFile(String name, InputStream local, boolean unique) throws sun.net.ftp.FtpProtocolException, IOException {
13461333
String cmd = unique ? "STOU " : "STOR ";
1347-
int mtu = 1500;
13481334
if (type == TransferType.BINARY) {
13491335
Socket s = openDataConnection(cmd + name);
1350-
OutputStream remote = createOutputStream(s.getOutputStream());
1351-
byte[] buf = new byte[mtu * 10];
1352-
int l;
1353-
while ((l = local.read(buf)) >= 0) {
1354-
if (l > 0) {
1355-
remote.write(buf, 0, l);
1356-
}
1336+
try (OutputStream remote = createOutputStream(s.getOutputStream())) {
1337+
local.transferTo(remote);
13571338
}
1358-
remote.close();
13591339
}
13601340
return completePending();
13611341
}
@@ -1373,17 +1353,10 @@ public sun.net.ftp.FtpClient putFile(String name, InputStream local, boolean uni
13731353
* @throws IOException if an error occurred during the transmission.
13741354
*/
13751355
public sun.net.ftp.FtpClient appendFile(String name, InputStream local) throws sun.net.ftp.FtpProtocolException, IOException {
1376-
int mtu = 1500;
13771356
Socket s = openDataConnection("APPE " + name);
1378-
OutputStream remote = createOutputStream(s.getOutputStream());
1379-
byte[] buf = new byte[mtu * 10];
1380-
int l;
1381-
while ((l = local.read(buf)) >= 0) {
1382-
if (l > 0) {
1383-
remote.write(buf, 0, l);
1384-
}
1357+
try (OutputStream remote = createOutputStream(s.getOutputStream())) {
1358+
local.transferTo(remote);
13851359
}
1386-
remote.close();
13871360
return completePending();
13881361
}
13891362

src/java.base/share/classes/sun/security/tools/keytool/Main.java

+2-8
Original file line numberDiff line numberDiff line change
@@ -2481,15 +2481,9 @@ public static Collection<? extends CRL> loadCRLs(String src) throws Exception {
24812481
// otherwise, keytool -gencrl | keytool -printcrl
24822482
// might not work properly, since -gencrl is slow
24832483
// and there's no data in the pipe at the beginning.
2484-
ByteArrayOutputStream bout = new ByteArrayOutputStream();
2485-
byte[] b = new byte[4096];
2486-
while (true) {
2487-
int len = in.read(b);
2488-
if (len < 0) break;
2489-
bout.write(b, 0, len);
2490-
}
2484+
byte[] bytes = in.readAllBytes();
24912485
return CertificateFactory.getInstance("X509").generateCRLs(
2492-
new ByteArrayInputStream(bout.toByteArray()));
2486+
new ByteArrayInputStream(bytes));
24932487
} finally {
24942488
if (in != System.in) {
24952489
in.close();

src/java.management/share/classes/javax/management/loading/MLet.java

+4-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -45,6 +45,7 @@
4545
import java.net.URL;
4646
import java.net.URLStreamHandlerFactory;
4747
import java.nio.file.Files;
48+
import java.nio.file.StandardCopyOption;
4849
import java.security.AccessController;
4950
import java.security.PrivilegedAction;
5051
import java.util.ArrayList;
@@ -1144,19 +1145,8 @@ private synchronized String loadLibraryAsResource(String libname) {
11441145
libname + ".", null)
11451146
.toFile();
11461147
file.deleteOnExit();
1147-
FileOutputStream fileOutput = new FileOutputStream(file);
1148-
try {
1149-
byte[] buf = new byte[4096];
1150-
int n;
1151-
while ((n = is.read(buf)) >= 0) {
1152-
fileOutput.write(buf, 0, n);
1153-
}
1154-
} finally {
1155-
fileOutput.close();
1156-
}
1157-
if (file.exists()) {
1158-
return file.getAbsolutePath();
1159-
}
1148+
Files.copy(is, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
1149+
return file.getAbsolutePath();
11601150
} finally {
11611151
is.close();
11621152
}

src/jdk.compiler/share/classes/com/sun/tools/sjavac/CopyFile.java

+5-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -26,13 +26,10 @@
2626
package com.sun.tools.sjavac;
2727

2828
import java.io.File;
29-
import java.io.FileInputStream;
30-
import java.io.FileOutputStream;
3129
import java.io.IOException;
32-
import java.io.InputStream;
33-
import java.io.OutputStream;
34-
import java.io.Writer;
3530
import java.net.URI;
31+
import java.nio.file.Files;
32+
import java.nio.file.StandardCopyOption;
3633
import java.util.HashSet;
3734
import java.util.Map;
3835
import java.util.Set;
@@ -107,13 +104,8 @@ public boolean transform(CompilationService compilationService,
107104

108105
Log.info("Copying "+pkgNameF+File.separator+src.getName());
109106

110-
try (InputStream fin = new FileInputStream(src);
111-
OutputStream fout = new FileOutputStream(dest)) {
112-
byte[] buf = new byte[1024];
113-
int len;
114-
while ((len = fin.read(buf)) > 0){
115-
fout.write(buf, 0, len);
116-
}
107+
try {
108+
Files.copy(src.toPath(), dest.toPath(), StandardCopyOption.REPLACE_EXISTING);
117109
}
118110
catch(IOException e){
119111
Log.error("Could not copy the file "+src.getPath()+" to "+dest.getPath());

src/jdk.jfr/share/classes/jdk/jfr/internal/instrument/JIClassInstrumentation.java

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,7 +25,6 @@
2525

2626
package jdk.jfr.internal.instrument;
2727

28-
import java.io.ByteArrayOutputStream;
2928
import java.io.IOException;
3029
import java.io.InputStream;
3130
import java.lang.reflect.Method;
@@ -77,17 +76,10 @@ final class JIClassInstrumentation {
7776
}
7877

7978
private static byte[] getOriginalClassBytes(Class<?> clazz) throws IOException {
80-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
8179
String name = "/" + clazz.getName().replace(".", "/") + ".class";
82-
InputStream is = SecuritySupport.getResourceAsStream(name);
83-
int bytesRead;
84-
byte[] buffer = new byte[16384];
85-
while ((bytesRead = is.read(buffer, 0, buffer.length)) != -1) {
86-
baos.write(buffer, 0, bytesRead);
80+
try (InputStream is = SecuritySupport.getResourceAsStream(name)) {
81+
return is.readAllBytes();
8782
}
88-
baos.flush();
89-
is.close();
90-
return baos.toByteArray();
9183
}
9284

9385
private byte[] makeBytecode() throws IOException, ClassNotFoundException {

src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipPath.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2009, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -967,11 +967,7 @@ else if (opt == COPY_ATTRIBUTES)
967967
try (InputStream is = zfs.newInputStream(getResolvedPath());
968968
OutputStream os = target.newOutputStream())
969969
{
970-
byte[] buf = new byte[8192];
971-
int n;
972-
while ((n = is.read(buf)) != -1) {
973-
os.write(buf, 0, n);
974-
}
970+
is.transferTo(os);
975971
}
976972
}
977973
if (copyAttrs) {

0 commit comments

Comments
 (0)