提交 0889428f 编写于 作者: A andrew

8138978: Examine usages of sun.misc.IOUtils

Reviewed-by: mbalao
上级 149b89c9
...@@ -47,8 +47,6 @@ import javax.naming.ldap.Control; ...@@ -47,8 +47,6 @@ import javax.naming.ldap.Control;
import javax.net.ssl.SSLParameters; import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocket;
import sun.misc.IOUtils;
/** /**
* A thread that creates a connection to an LDAP server. * A thread that creates a connection to an LDAP server.
* After the connection, the thread reads from the connection. * After the connection, the thread reads from the connection.
...@@ -886,7 +884,7 @@ public final class Connection implements Runnable { ...@@ -886,7 +884,7 @@ public final class Connection implements Runnable {
} }
// read in seqlen bytes // read in seqlen bytes
byte[] left = IOUtils.readFully(in, seqlen, false); byte[] left = readFully(in, seqlen);
inbuf = Arrays.copyOf(inbuf, offset + left.length); inbuf = Arrays.copyOf(inbuf, offset + left.length);
System.arraycopy(left, 0, inbuf, offset, left.length); System.arraycopy(left, 0, inbuf, offset, left.length);
offset += left.length; offset += left.length;
...@@ -981,6 +979,31 @@ System.err.println("bytesread: " + bytesread); ...@@ -981,6 +979,31 @@ System.err.println("bytesread: " + bytesread);
} }
} }
private static byte[] readFully(InputStream is, int length)
throws IOException
{
byte[] buf = new byte[Math.min(length, 8192)];
int nread = 0;
while (nread < length) {
int bytesToRead;
if (nread >= buf.length) { // need to allocate a larger buffer
bytesToRead = Math.min(length - nread, buf.length + 8192);
if (buf.length < nread + bytesToRead) {
buf = Arrays.copyOf(buf, nread + bytesToRead);
}
} else {
bytesToRead = buf.length - nread;
}
int count = is.read(buf, nread, bytesToRead);
if (count < 0) {
if (buf.length != nread)
buf = Arrays.copyOf(buf, nread);
break;
}
nread += count;
}
return buf;
}
// This code must be uncommented to run the LdapAbandonTest. // This code must be uncommented to run the LdapAbandonTest.
/*public void sendSearchReqs(String dn, int numReqs) { /*public void sendSearchReqs(String dn, int numReqs) {
......
...@@ -422,7 +422,12 @@ class JarFile extends ZipFile { ...@@ -422,7 +422,12 @@ class JarFile extends ZipFile {
*/ */
private byte[] getBytes(ZipEntry ze) throws IOException { private byte[] getBytes(ZipEntry ze) throws IOException {
try (InputStream is = super.getInputStream(ze)) { try (InputStream is = super.getInputStream(ze)) {
return IOUtils.readFully(is, (int)ze.getSize(), true); int len = (int)ze.getSize();
byte[] b = IOUtils.readAllBytes(is);
if (len != -1 && b.length != len)
throw new EOFException("Expected:" + len + ", read:" + b.length);
return b;
} }
} }
......
...@@ -33,6 +33,7 @@ import java.net.URLConnection; ...@@ -33,6 +33,7 @@ import java.net.URLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.io.EOFException;
import java.io.File; import java.io.File;
import java.io.FilePermission; import java.io.FilePermission;
import java.io.IOException; import java.io.IOException;
...@@ -333,7 +334,9 @@ public class AppletClassLoader extends URLClassLoader { ...@@ -333,7 +334,9 @@ public class AppletClassLoader extends URLClassLoader {
byte[] b; byte[] b;
try { try {
b = IOUtils.readFully(in, len, true); b = IOUtils.readAllBytes(in);
if (len != -1 && b.length != len)
throw new EOFException("Expected:" + len + ", read:" + b.length);
} finally { } finally {
in.close(); in.close();
} }
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
package sun.reflect.misc; package sun.reflect.misc;
import java.io.EOFException;
import java.security.AllPermission; import java.security.AllPermission;
import java.security.AccessController; import java.security.AccessController;
import java.security.PermissionCollection; import java.security.PermissionCollection;
...@@ -42,8 +43,8 @@ import java.lang.reflect.AccessibleObject; ...@@ -42,8 +43,8 @@ import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import sun.misc.IOUtils;
import sun.misc.IOUtils;
class Trampoline { class Trampoline {
static { static {
...@@ -382,16 +383,13 @@ public final class MethodUtil extends SecureClassLoader { ...@@ -382,16 +383,13 @@ public final class MethodUtil extends SecureClassLoader {
} }
} }
int len = uc.getContentLength(); int len = uc.getContentLength();
InputStream in = new BufferedInputStream(uc.getInputStream()); try (InputStream in = new BufferedInputStream(uc.getInputStream())) {
byte[] b = IOUtils.readAllBytes(in);
byte[] b; if (len != -1 && b.length != len)
try { throw new EOFException("Expected:" + len + ", read:" + b.length);
b = IOUtils.readFully(in, len, true);
} finally {
in.close();
}
return b; return b;
} }
}
protected PermissionCollection getPermissions(CodeSource codesource) protected PermissionCollection getPermissions(CodeSource codesource)
......
...@@ -27,6 +27,7 @@ package sun.security.timestamp; ...@@ -27,6 +27,7 @@ package sun.security.timestamp;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.net.URL; import java.net.URL;
...@@ -147,8 +148,11 @@ public class HttpTimestamper implements Timestamper { ...@@ -147,8 +148,11 @@ public class HttpTimestamper implements Timestamper {
} }
verifyMimeType(connection.getContentType()); verifyMimeType(connection.getContentType());
int contentLength = connection.getContentLength(); int clen = connection.getContentLength();
replyBuffer = IOUtils.readFully(input, contentLength, false); replyBuffer = IOUtils.readAllBytes(input);
if (clen != -1 && replyBuffer.length != clen)
throw new EOFException("Expected:" + clen +
", read:" + replyBuffer.length);
if (debug != null) { if (debug != null) {
debug.println("received timestamp response (length=" + debug.println("received timestamp response (length=" +
......
...@@ -743,7 +743,7 @@ public class TimestampCheck { ...@@ -743,7 +743,7 @@ public class TimestampCheck {
try (JarFile jf = new JarFile(file)) { try (JarFile jf = new JarFile(file)) {
JarEntry je = jf.getJarEntry("META-INF/SIGNER.RSA"); JarEntry je = jf.getJarEntry("META-INF/SIGNER.RSA");
try (InputStream is = jf.getInputStream(je)) { try (InputStream is = jf.getInputStream(je)) {
byte[] content = IOUtils.readFully(is, -1, true); byte[] content = IOUtils.readAllBytes(is);
PKCS7 p7 = new PKCS7(content); PKCS7 p7 = new PKCS7(content);
SignerInfo[] si = p7.getSignerInfos(); SignerInfo[] si = p7.getSignerInfos();
if (si == null || si.length == 0) { if (si == null || si.length == 0) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册