changeset 13790:c9e76bc2aae1

8138978: Examine usages of sun.misc.IOUtils Reviewed-by: mbalao
author andrew
date Fri, 03 Jan 2020 04:06:38 +0000
parents efd68acff50d
children 226eafcdf737
files src/share/classes/com/sun/jndi/ldap/Connection.java src/share/classes/java/util/jar/JarFile.java src/share/classes/sun/applet/AppletClassLoader.java src/share/classes/sun/reflect/misc/MethodUtil.java src/share/classes/sun/security/timestamp/HttpTimestamper.java test/sun/security/tools/jarsigner/TimestampCheck.java
diffstat 6 files changed, 50 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/jndi/ldap/Connection.java	Fri Jan 03 02:57:01 2020 +0000
+++ b/src/share/classes/com/sun/jndi/ldap/Connection.java	Fri Jan 03 04:06:38 2020 +0000
@@ -47,8 +47,6 @@
 import javax.net.ssl.SSLParameters;
 import javax.net.ssl.SSLSocket;
 
-import sun.misc.IOUtils;
-
 /**
   * A thread that creates a connection to an LDAP server.
   * After the connection, the thread reads from the connection.
@@ -886,7 +884,7 @@
                     }
 
                     // read in seqlen bytes
-                    byte[] left = IOUtils.readFully(in, seqlen, false);
+                    byte[] left = readFully(in, seqlen);
                     inbuf = Arrays.copyOf(inbuf, offset + left.length);
                     System.arraycopy(left, 0, inbuf, offset, left.length);
                     offset += left.length;
@@ -981,6 +979,31 @@
         }
     }
 
+    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.
     /*public void sendSearchReqs(String dn, int numReqs) {
--- a/src/share/classes/java/util/jar/JarFile.java	Fri Jan 03 02:57:01 2020 +0000
+++ b/src/share/classes/java/util/jar/JarFile.java	Fri Jan 03 04:06:38 2020 +0000
@@ -422,7 +422,12 @@
      */
     private byte[] getBytes(ZipEntry ze) throws IOException {
         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;
         }
     }
 
--- a/src/share/classes/sun/applet/AppletClassLoader.java	Fri Jan 03 02:57:01 2020 +0000
+++ b/src/share/classes/sun/applet/AppletClassLoader.java	Fri Jan 03 04:06:38 2020 +0000
@@ -33,6 +33,7 @@
 import java.net.MalformedURLException;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
+import java.io.EOFException;
 import java.io.File;
 import java.io.FilePermission;
 import java.io.IOException;
@@ -333,7 +334,9 @@
 
         byte[] b;
         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 {
             in.close();
         }
--- a/src/share/classes/sun/reflect/misc/MethodUtil.java	Fri Jan 03 02:57:01 2020 +0000
+++ b/src/share/classes/sun/reflect/misc/MethodUtil.java	Fri Jan 03 04:06:38 2020 +0000
@@ -25,6 +25,7 @@
 
 package sun.reflect.misc;
 
+import java.io.EOFException;
 import java.security.AllPermission;
 import java.security.AccessController;
 import java.security.PermissionCollection;
@@ -42,9 +43,9 @@
 import java.lang.reflect.Modifier;
 import java.util.HashMap;
 import java.util.Map;
+
 import sun.misc.IOUtils;
 
-
 class Trampoline {
     static {
         if (Trampoline.class.getClassLoader() == null) {
@@ -382,15 +383,12 @@
             }
         }
         int len = uc.getContentLength();
-        InputStream in = new BufferedInputStream(uc.getInputStream());
-
-        byte[] b;
-        try {
-            b = IOUtils.readFully(in, len, true);
-        } finally {
-            in.close();
+        try (InputStream in = new BufferedInputStream(uc.getInputStream())) {
+            byte[] b = IOUtils.readAllBytes(in);
+            if (len != -1 && b.length != len)
+                throw new EOFException("Expected:" + len + ", read:" + b.length);
+            return b;
         }
-        return b;
     }
 
 
--- a/src/share/classes/sun/security/timestamp/HttpTimestamper.java	Fri Jan 03 02:57:01 2020 +0000
+++ b/src/share/classes/sun/security/timestamp/HttpTimestamper.java	Fri Jan 03 04:06:38 2020 +0000
@@ -27,6 +27,7 @@
 
 import java.io.BufferedInputStream;
 import java.io.DataOutputStream;
+import java.io.EOFException;
 import java.io.IOException;
 import java.net.URI;
 import java.net.URL;
@@ -147,8 +148,11 @@
             }
             verifyMimeType(connection.getContentType());
 
-            int contentLength = connection.getContentLength();
-            replyBuffer = IOUtils.readFully(input, contentLength, false);
+            int clen = connection.getContentLength();
+            replyBuffer = IOUtils.readAllBytes(input);
+            if (clen != -1 && replyBuffer.length != clen)
+                throw new EOFException("Expected:" + clen +
+                                       ", read:" + replyBuffer.length);
 
             if (debug != null) {
                 debug.println("received timestamp response (length=" +
--- a/test/sun/security/tools/jarsigner/TimestampCheck.java	Fri Jan 03 02:57:01 2020 +0000
+++ b/test/sun/security/tools/jarsigner/TimestampCheck.java	Fri Jan 03 04:06:38 2020 +0000
@@ -743,7 +743,7 @@
         try (JarFile jf = new JarFile(file)) {
             JarEntry je = jf.getJarEntry("META-INF/SIGNER.RSA");
             try (InputStream is = jf.getInputStream(je)) {
-                byte[] content = IOUtils.readFully(is, -1, true);
+                byte[] content = IOUtils.readAllBytes(is);
                 PKCS7 p7 = new PKCS7(content);
                 SignerInfo[] si = p7.getSignerInfos();
                 if (si == null || si.length == 0) {