8139706: JarFile.getBytes could use InputStream.readNBytes
authorredestad
Sun, 18 Oct 2015 01:43:06 +0200
changeset 33235 edfac9a33df3
parent 32997 98b3acb676c2
child 33236 e4c383318c28
8139706: JarFile.getBytes could use InputStream.readNBytes Reviewed-by: sherman, chegar, alanb
jdk/src/java.base/share/classes/java/util/jar/JarFile.java
--- a/jdk/src/java.base/share/classes/java/util/jar/JarFile.java	Fri Oct 16 19:05:49 2015 +0300
+++ b/jdk/src/java.base/share/classes/java/util/jar/JarFile.java	Sun Oct 18 01:43:06 2015 +0200
@@ -438,10 +438,19 @@
     private byte[] getBytes(ZipEntry ze) throws IOException {
         try (InputStream is = super.getInputStream(ze)) {
             int len = (int)ze.getSize();
-            byte[] b = is.readAllBytes();
-            if (len != -1 && b.length != len)
-                throw new EOFException("Expected:" + len + ", read:" + b.length);
-
+            int bytesRead;
+            byte[] b;
+            // trust specified entry sizes when reasonably small
+            if (len != -1 && len <= 65535) {
+                b = new byte[len];
+                bytesRead = is.readNBytes(b, 0, len);
+            } else {
+                b = is.readAllBytes();
+                bytesRead = b.length;
+            }
+            if (len != -1 && len != bytesRead) {
+                throw new EOFException("Expected:" + len + ", read:" + bytesRead);
+            }
             return b;
         }
     }