8080272: Refactor I/O stream copying to use InputStream.transferTo/readAllBytes and Files.copy
Reviewed-by: mcimadamore, alanb
This commit is contained in:
parent
a31a23d5e7
commit
68deb24b38
@ -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.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* 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())) {
|
if (e != null && JarFile.MANIFEST_NAME.equalsIgnoreCase(e.getName())) {
|
||||||
man = new Manifest();
|
man = new Manifest();
|
||||||
byte bytes[] = getBytes(new BufferedInputStream(this));
|
byte[] bytes = readAllBytes();
|
||||||
man.read(new ByteArrayInputStream(bytes));
|
man.read(new ByteArrayInputStream(bytes));
|
||||||
closeEntry();
|
closeEntry();
|
||||||
if (doVerify) {
|
if (doVerify) {
|
||||||
@ -102,18 +102,6 @@ public class JarInputStream extends ZipInputStream {
|
|||||||
return 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
|
* Returns the {@code Manifest} for this JAR file, or
|
||||||
* {@code null} if none.
|
* {@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.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* 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.
|
* @throws IOException if the transfer fails.
|
||||||
*/
|
*/
|
||||||
public sun.net.ftp.FtpClient getFile(String name, OutputStream local) throws sun.net.ftp.FtpProtocolException, IOException {
|
public sun.net.ftp.FtpClient getFile(String name, OutputStream local) throws sun.net.ftp.FtpProtocolException, IOException {
|
||||||
int mtu = 1500;
|
|
||||||
if (restartOffset > 0) {
|
if (restartOffset > 0) {
|
||||||
Socket s;
|
Socket s;
|
||||||
try {
|
try {
|
||||||
@ -1227,27 +1226,15 @@ public class FtpClient extends sun.net.ftp.FtpClient {
|
|||||||
}
|
}
|
||||||
issueCommandCheck("RETR " + name);
|
issueCommandCheck("RETR " + name);
|
||||||
getTransferSize();
|
getTransferSize();
|
||||||
InputStream remote = createInputStream(s.getInputStream());
|
try (InputStream remote = createInputStream(s.getInputStream())) {
|
||||||
byte[] buf = new byte[mtu * 10];
|
remote.transferTo(local);
|
||||||
int l;
|
|
||||||
while ((l = remote.read(buf)) >= 0) {
|
|
||||||
if (l > 0) {
|
|
||||||
local.write(buf, 0, l);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
remote.close();
|
|
||||||
} else {
|
} else {
|
||||||
Socket s = openDataConnection("RETR " + name);
|
Socket s = openDataConnection("RETR " + name);
|
||||||
getTransferSize();
|
getTransferSize();
|
||||||
InputStream remote = createInputStream(s.getInputStream());
|
try (InputStream remote = createInputStream(s.getInputStream())) {
|
||||||
byte[] buf = new byte[mtu * 10];
|
remote.transferTo(local);
|
||||||
int l;
|
|
||||||
while ((l = remote.read(buf)) >= 0) {
|
|
||||||
if (l > 0) {
|
|
||||||
local.write(buf, 0, l);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
remote.close();
|
|
||||||
}
|
}
|
||||||
return completePending();
|
return completePending();
|
||||||
}
|
}
|
||||||
@ -1344,18 +1331,11 @@ 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 {
|
public sun.net.ftp.FtpClient putFile(String name, InputStream local, boolean unique) throws sun.net.ftp.FtpProtocolException, IOException {
|
||||||
String cmd = unique ? "STOU " : "STOR ";
|
String cmd = unique ? "STOU " : "STOR ";
|
||||||
int mtu = 1500;
|
|
||||||
if (type == TransferType.BINARY) {
|
if (type == TransferType.BINARY) {
|
||||||
Socket s = openDataConnection(cmd + name);
|
Socket s = openDataConnection(cmd + name);
|
||||||
OutputStream remote = createOutputStream(s.getOutputStream());
|
try (OutputStream remote = createOutputStream(s.getOutputStream())) {
|
||||||
byte[] buf = new byte[mtu * 10];
|
local.transferTo(remote);
|
||||||
int l;
|
|
||||||
while ((l = local.read(buf)) >= 0) {
|
|
||||||
if (l > 0) {
|
|
||||||
remote.write(buf, 0, l);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
remote.close();
|
|
||||||
}
|
}
|
||||||
return completePending();
|
return completePending();
|
||||||
}
|
}
|
||||||
@ -1373,17 +1353,10 @@ public class FtpClient extends sun.net.ftp.FtpClient {
|
|||||||
* @throws IOException if an error occurred during the transmission.
|
* @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 {
|
public sun.net.ftp.FtpClient appendFile(String name, InputStream local) throws sun.net.ftp.FtpProtocolException, IOException {
|
||||||
int mtu = 1500;
|
|
||||||
Socket s = openDataConnection("APPE " + name);
|
Socket s = openDataConnection("APPE " + name);
|
||||||
OutputStream remote = createOutputStream(s.getOutputStream());
|
try (OutputStream remote = createOutputStream(s.getOutputStream())) {
|
||||||
byte[] buf = new byte[mtu * 10];
|
local.transferTo(remote);
|
||||||
int l;
|
|
||||||
while ((l = local.read(buf)) >= 0) {
|
|
||||||
if (l > 0) {
|
|
||||||
remote.write(buf, 0, l);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
remote.close();
|
|
||||||
return completePending();
|
return completePending();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2481,15 +2481,9 @@ public final class Main {
|
|||||||
// otherwise, keytool -gencrl | keytool -printcrl
|
// otherwise, keytool -gencrl | keytool -printcrl
|
||||||
// might not work properly, since -gencrl is slow
|
// might not work properly, since -gencrl is slow
|
||||||
// and there's no data in the pipe at the beginning.
|
// and there's no data in the pipe at the beginning.
|
||||||
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
byte[] bytes = in.readAllBytes();
|
||||||
byte[] b = new byte[4096];
|
|
||||||
while (true) {
|
|
||||||
int len = in.read(b);
|
|
||||||
if (len < 0) break;
|
|
||||||
bout.write(b, 0, len);
|
|
||||||
}
|
|
||||||
return CertificateFactory.getInstance("X509").generateCRLs(
|
return CertificateFactory.getInstance("X509").generateCRLs(
|
||||||
new ByteArrayInputStream(bout.toByteArray()));
|
new ByteArrayInputStream(bytes));
|
||||||
} finally {
|
} finally {
|
||||||
if (in != System.in) {
|
if (in != System.in) {
|
||||||
in.close();
|
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.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* 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.URL;
|
||||||
import java.net.URLStreamHandlerFactory;
|
import java.net.URLStreamHandlerFactory;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.StandardCopyOption;
|
||||||
import java.security.AccessController;
|
import java.security.AccessController;
|
||||||
import java.security.PrivilegedAction;
|
import java.security.PrivilegedAction;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -1144,19 +1145,8 @@ public class MLet extends java.net.URLClassLoader
|
|||||||
libname + ".", null)
|
libname + ".", null)
|
||||||
.toFile();
|
.toFile();
|
||||||
file.deleteOnExit();
|
file.deleteOnExit();
|
||||||
FileOutputStream fileOutput = new FileOutputStream(file);
|
Files.copy(is, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||||
try {
|
return file.getAbsolutePath();
|
||||||
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();
|
|
||||||
}
|
|
||||||
} finally {
|
} finally {
|
||||||
is.close();
|
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.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -26,13 +26,10 @@
|
|||||||
package com.sun.tools.sjavac;
|
package com.sun.tools.sjavac;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.io.Writer;
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.StandardCopyOption;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@ -107,13 +104,8 @@ public class CopyFile implements Transformer {
|
|||||||
|
|
||||||
Log.info("Copying "+pkgNameF+File.separator+src.getName());
|
Log.info("Copying "+pkgNameF+File.separator+src.getName());
|
||||||
|
|
||||||
try (InputStream fin = new FileInputStream(src);
|
try {
|
||||||
OutputStream fout = new FileOutputStream(dest)) {
|
Files.copy(src.toPath(), dest.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||||
byte[] buf = new byte[1024];
|
|
||||||
int len;
|
|
||||||
while ((len = fin.read(buf)) > 0){
|
|
||||||
fout.write(buf, 0, len);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch(IOException e){
|
catch(IOException e){
|
||||||
Log.error("Could not copy the file "+src.getPath()+" to "+dest.getPath());
|
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.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -25,7 +25,6 @@
|
|||||||
|
|
||||||
package jdk.jfr.internal.instrument;
|
package jdk.jfr.internal.instrument;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
@ -77,17 +76,10 @@ final class JIClassInstrumentation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static byte[] getOriginalClassBytes(Class<?> clazz) throws IOException {
|
private static byte[] getOriginalClassBytes(Class<?> clazz) throws IOException {
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
||||||
String name = "/" + clazz.getName().replace(".", "/") + ".class";
|
String name = "/" + clazz.getName().replace(".", "/") + ".class";
|
||||||
InputStream is = SecuritySupport.getResourceAsStream(name);
|
try (InputStream is = SecuritySupport.getResourceAsStream(name)) {
|
||||||
int bytesRead;
|
return is.readAllBytes();
|
||||||
byte[] buffer = new byte[16384];
|
|
||||||
while ((bytesRead = is.read(buffer, 0, buffer.length)) != -1) {
|
|
||||||
baos.write(buffer, 0, bytesRead);
|
|
||||||
}
|
}
|
||||||
baos.flush();
|
|
||||||
is.close();
|
|
||||||
return baos.toByteArray();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private byte[] makeBytecode() throws IOException, ClassNotFoundException {
|
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.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* 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());
|
try (InputStream is = zfs.newInputStream(getResolvedPath());
|
||||||
OutputStream os = target.newOutputStream())
|
OutputStream os = target.newOutputStream())
|
||||||
{
|
{
|
||||||
byte[] buf = new byte[8192];
|
is.transferTo(os);
|
||||||
int n;
|
|
||||||
while ((n = is.read(buf)) != -1) {
|
|
||||||
os.write(buf, 0, n);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (copyAttrs) {
|
if (copyAttrs) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user