Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

@@ -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.
@@ -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 String getWorkingDirectory() throws sun.net.ftp.FtpProtocolException, IOE
* @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 String getWorkingDirectory() throws sun.net.ftp.FtpProtocolException, IOE
}
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 OutputStream putFileStream(String name, boolean unique)
* @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();
}

@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2020, 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
@@ -2454,15 +2454,9 @@ private void doPrintEntries(PrintStream out)
// 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));
This conversation was marked as resolved by turbanoff

This comment has been minimized.

@stsypanov

stsypanov Dec 21, 2020
Contributor

Let's just pass in into generateCRLs instead of reading all bytes and rewrapping them into InputStream again?

This comment has been minimized.

@turbanoff

turbanoff Dec 21, 2020
Author Contributor

Looks like it was done intentionally by original author of the code.
Check comment above:

            // Read the full stream before feeding to X509Factory,
            // otherwise, keytool -gencrl | keytool -printcrl
            // might not work properly, since -gencrl is slow
            // and there's no data in the pipe at the beginning.

This comment has been minimized.

@stsypanov

stsypanov Dec 21, 2020
Contributor

Let's keep it then

} finally {
if (in != System.in) {
in.close();
@@ -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();
}
@@ -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());
@@ -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 @@
}

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 {
@@ -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) {