Skip to content

Commit

Permalink
8080272: Refactor I/O stream copying to use InputStream.transferTo/re…
Browse files Browse the repository at this point in the history
…adAllBytes and Files.copy

Reviewed-by: mcimadamore, alanb
  • Loading branch information
turbanoff authored and Julia Boes committed Mar 16, 2021
1 parent a31a23d commit 68deb24
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 102 deletions.
16 changes: 2 additions & 14 deletions 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
Expand Down Expand Up @@ -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) {
Expand All @@ -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.
Expand Down
45 changes: 9 additions & 36 deletions 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
Expand Down Expand Up @@ -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 {
Expand All @@ -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();
}
Expand Down Expand Up @@ -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();
}
Expand All @@ -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();
}

Expand Down
10 changes: 2 additions & 8 deletions src/java.base/share/classes/sun/security/tools/keytool/Main.java
Expand Up @@ -2481,15 +2481,9 @@ public static Collection<? extends CRL> 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();
Expand Down
@@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
18 changes: 5 additions & 13 deletions 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
Expand All @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
@@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 2 additions & 6 deletions 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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 68deb24

Please sign in to comment.