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) 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 @@ public class JarInputStream extends ZipInputStream {
{
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 @@ public class JarInputStream extends ZipInputStream {
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.

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,28 +1226,16 @@ 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,19 +1331,12 @@ 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();
}

View File

@ -2481,15 +2481,9 @@ public final class Main {
// 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();

View File

@ -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.MalformedURLException;
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 @@ public class MLet extends java.net.URLClassLoader
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()) {
Files.copy(is, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
return file.getAbsolutePath();
}
} finally {
is.close();
}

View File

@ -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 class CopyFile implements Transformer {
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());

View File

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

View File

@ -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 @@ final class ZipPath implements Path {
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) {