From 68deb24b38349b851cfb157d34c26d63bfff821b Mon Sep 17 00:00:00 2001 From: Andrey Turbanov Date: Tue, 16 Mar 2021 10:10:05 +0000 Subject: [PATCH] 8080272: Refactor I/O stream copying to use InputStream.transferTo/readAllBytes and Files.copy Reviewed-by: mcimadamore, alanb --- .../classes/java/util/jar/JarInputStream.java | 16 +------ .../classes/sun/net/ftp/impl/FtpClient.java | 45 ++++--------------- .../sun/security/tools/keytool/Main.java | 10 +---- .../javax/management/loading/MLet.java | 18 ++------ .../com/sun/tools/sjavac/CopyFile.java | 18 +++----- .../instrument/JIClassInstrumentation.java | 14 ++---- .../share/classes/jdk/nio/zipfs/ZipPath.java | 8 +--- 7 files changed, 27 insertions(+), 102 deletions(-) diff --git a/src/java.base/share/classes/java/util/jar/JarInputStream.java b/src/java.base/share/classes/java/util/jar/JarInputStream.java index 9f1bfaea88c..e567d58a3e5 100644 --- a/src/java.base/share/classes/java/util/jar/JarInputStream.java +++ b/src/java.base/share/classes/java/util/jar/JarInputStream.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 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 @@ -90,7 +90,7 @@ private JarEntry checkManifest(JarEntry e) { if (e != null && JarFile.MANIFEST_NAME.equalsIgnoreCase(e.getName())) { man = new Manifest(); - byte bytes[] = getBytes(new BufferedInputStream(this)); + byte[] bytes = readAllBytes(); man.read(new ByteArrayInputStream(bytes)); closeEntry(); if (doVerify) { @@ -102,18 +102,6 @@ private JarEntry checkManifest(JarEntry e) return e; } - private byte[] getBytes(InputStream is) - throws IOException - { - byte[] buffer = new byte[8192]; - ByteArrayOutputStream baos = new ByteArrayOutputStream(2048); - int n; - while ((n = is.read(buffer, 0, buffer.length)) != -1) { - baos.write(buffer, 0, n); - } - return baos.toByteArray(); - } - /** * Returns the {@code Manifest} for this JAR file, or * {@code null} if none. diff --git a/src/java.base/share/classes/sun/net/ftp/impl/FtpClient.java b/src/java.base/share/classes/sun/net/ftp/impl/FtpClient.java index 442860138c5..03d3b33f77c 100644 --- a/src/java.base/share/classes/sun/net/ftp/impl/FtpClient.java +++ b/src/java.base/share/classes/sun/net/ftp/impl/FtpClient.java @@ -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 sun.net.ftp.FtpClient setRestartOffset(long offset) { * @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 sun.net.ftp.FtpClient getFile(String name, OutputStream local) throws sun } 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 OutputStream putFileStream(String name, boolean unique) */ 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 sun.net.ftp.FtpClient putFile(String name, InputStream local, boolean uni * @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(); } diff --git a/src/java.base/share/classes/sun/security/tools/keytool/Main.java b/src/java.base/share/classes/sun/security/tools/keytool/Main.java index 0e6ea8aeb66..1f39d5ac598 100644 --- a/src/java.base/share/classes/sun/security/tools/keytool/Main.java +++ b/src/java.base/share/classes/sun/security/tools/keytool/Main.java @@ -2481,15 +2481,9 @@ public static Collection loadCRLs(String src) throws Exception { // otherwise, keytool -gencrl | keytool -printcrl // might not work properly, since -gencrl is slow // and there's no data in the pipe at the beginning. - ByteArrayOutputStream bout = new ByteArrayOutputStream(); - byte[] b = new byte[4096]; - while (true) { - int len = in.read(b); - if (len < 0) break; - bout.write(b, 0, len); - } + byte[] bytes = in.readAllBytes(); return CertificateFactory.getInstance("X509").generateCRLs( - new ByteArrayInputStream(bout.toByteArray())); + new ByteArrayInputStream(bytes)); } finally { if (in != System.in) { in.close(); diff --git a/src/java.management/share/classes/javax/management/loading/MLet.java b/src/java.management/share/classes/javax/management/loading/MLet.java index 500b773abf7..1af020f38f9 100644 --- a/src/java.management/share/classes/javax/management/loading/MLet.java +++ b/src/java.management/share/classes/javax/management/loading/MLet.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 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 @@ -45,6 +45,7 @@ import java.net.URL; import java.net.URLStreamHandlerFactory; import java.nio.file.Files; +import java.nio.file.StandardCopyOption; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.ArrayList; @@ -1144,19 +1145,8 @@ private synchronized String loadLibraryAsResource(String libname) { libname + ".", null) .toFile(); file.deleteOnExit(); - FileOutputStream fileOutput = new FileOutputStream(file); - try { - byte[] buf = new byte[4096]; - int n; - while ((n = is.read(buf)) >= 0) { - fileOutput.write(buf, 0, n); - } - } finally { - fileOutput.close(); - } - if (file.exists()) { - return file.getAbsolutePath(); - } + Files.copy(is, file.toPath(), StandardCopyOption.REPLACE_EXISTING); + return file.getAbsolutePath(); } finally { is.close(); } diff --git a/src/jdk.compiler/share/classes/com/sun/tools/sjavac/CopyFile.java b/src/jdk.compiler/share/classes/com/sun/tools/sjavac/CopyFile.java index d3685eacfc6..805ea85d32b 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/sjavac/CopyFile.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/sjavac/CopyFile.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 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 @@ -26,13 +26,10 @@ package com.sun.tools.sjavac; import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.Writer; import java.net.URI; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; import java.util.HashSet; import java.util.Map; import java.util.Set; @@ -107,13 +104,8 @@ public boolean transform(CompilationService compilationService, Log.info("Copying "+pkgNameF+File.separator+src.getName()); - try (InputStream fin = new FileInputStream(src); - OutputStream fout = new FileOutputStream(dest)) { - byte[] buf = new byte[1024]; - int len; - while ((len = fin.read(buf)) > 0){ - fout.write(buf, 0, len); - } + try { + Files.copy(src.toPath(), dest.toPath(), StandardCopyOption.REPLACE_EXISTING); } catch(IOException e){ Log.error("Could not copy the file "+src.getPath()+" to "+dest.getPath()); diff --git a/src/jdk.jfr/share/classes/jdk/jfr/internal/instrument/JIClassInstrumentation.java b/src/jdk.jfr/share/classes/jdk/jfr/internal/instrument/JIClassInstrumentation.java index ea6e2eb2679..24c0437d577 100644 --- a/src/jdk.jfr/share/classes/jdk/jfr/internal/instrument/JIClassInstrumentation.java +++ b/src/jdk.jfr/share/classes/jdk/jfr/internal/instrument/JIClassInstrumentation.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 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 @@ -25,7 +25,6 @@ package jdk.jfr.internal.instrument; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Method; @@ -77,17 +76,10 @@ final class JIClassInstrumentation { } private static byte[] getOriginalClassBytes(Class clazz) throws IOException { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); String name = "/" + clazz.getName().replace(".", "/") + ".class"; - InputStream is = SecuritySupport.getResourceAsStream(name); - int bytesRead; - byte[] buffer = new byte[16384]; - while ((bytesRead = is.read(buffer, 0, buffer.length)) != -1) { - baos.write(buffer, 0, bytesRead); + try (InputStream is = SecuritySupport.getResourceAsStream(name)) { + return is.readAllBytes(); } - baos.flush(); - is.close(); - return baos.toByteArray(); } private byte[] makeBytecode() throws IOException, ClassNotFoundException { diff --git a/src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipPath.java b/src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipPath.java index e453bb0cf1d..8d64187cc47 100644 --- a/src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipPath.java +++ b/src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipPath.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, 2019, 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 @@ -967,11 +967,7 @@ else if (opt == COPY_ATTRIBUTES) try (InputStream is = zfs.newInputStream(getResolvedPath()); OutputStream os = target.newOutputStream()) { - byte[] buf = new byte[8192]; - int n; - while ((n = is.read(buf)) != -1) { - os.write(buf, 0, n); - } + is.transferTo(os); } } if (copyAttrs) {