You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2009/10/09 11:31:51 UTC

svn commit: r823478 - in /harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar: JarFile.java Manifest.java

Author: tellison
Date: Fri Oct  9 09:31:51 2009
New Revision: 823478

URL: http://svn.apache.org/viewvc?rev=823478&view=rev
Log:
Enhance the methods for fully reading a stream so that they use less memory in common cases.

Modified:
    harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/JarFile.java
    harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/Manifest.java

Modified: harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/JarFile.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/JarFile.java?rev=823478&r1=823477&r2=823478&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/JarFile.java (original)
+++ harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/JarFile.java Fri Oct  9 09:31:51 2009
@@ -280,20 +280,40 @@
         return (JarEntry) getEntry(name);
     }
 
-    private byte[] getAllBytesFromStreamAndClose(InputStream is) throws IOException {
-        ByteArrayOutputStream bs = new ByteArrayOutputStream();
+    /*
+     * Drains the entire content from the given input stream and returns it as a
+     * byte[]. The stream is closed after being drained, or if an IOException
+     * occurs.
+     */
+    private byte[] getAllBytesFromStreamAndClose(InputStream is)
+            throws IOException {
         try {
-            byte[] buf = new byte[1024];
-            while (is.available() > 0) {
-                int iRead = is.read(buf, 0, buf.length);
-                if (iRead > 0) {
-                    bs.write(buf, 0, iRead);
+            // Initial read
+            byte[] buffer = new byte[1024];
+            int count = is.read(buffer);
+            int nextByte = is.read();
+
+            // Did we get it all in one read?
+            if (nextByte == -1) {
+                byte[] dest = new byte[count];
+                System.arraycopy(buffer, 0, dest, 0, count);
+                return dest;
+            }
+
+            // Requires additional reads
+            ByteArrayOutputStream baos = new ByteArrayOutputStream(count * 2);
+            baos.write(buffer, 0, count);
+            baos.write(nextByte);
+            while (true) {
+                count = is.read(buffer);
+                if (count == -1) {
+                    return baos.toByteArray();
                 }
+                baos.write(buffer, 0, count);
             }
         } finally {
             is.close();
         }
-        return bs.toByteArray();
     }
 
     /**

Modified: harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/Manifest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/Manifest.java?rev=823478&r1=823477&r2=823478&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/Manifest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/Manifest.java Fri Oct  9 09:31:51 2009
@@ -242,20 +242,34 @@
      * have a line feed within a reasonable number of characters.
      */
     private byte[] readFully(InputStream is) throws IOException {
-        ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        byte[] buffer = new byte[8192];
-
+        // Initial read
+        byte[] buffer = new byte[4096];
+        int count = is.read(buffer);
+        int nextByte = is.read();
+
+        // Did we get it all in one read?
+        if (nextByte == -1) {
+            byte[] dest = new byte[count];
+            System.arraycopy(buffer, 0, dest, 0, count);
+            return dest;
+        }
+
+        // Does it look like a manifest?
+        if (!containsLine(buffer, count)) {
+            // archive.2E=Manifest is too long
+            throw new IOException(Messages.getString("archive.2E")); //$NON-NLS-1$
+        }
+
+        // Requires additional reads
+        ByteArrayOutputStream baos = new ByteArrayOutputStream(count * 2);
+        baos.write(buffer, 0, count);
+        baos.write(nextByte);
         while (true) {
-            int count = is.read(buffer);
+            count = is.read(buffer);
             if (count == -1) {
-                // TODO: Do we need to copy this, or can we live with junk at the end?
                 return baos.toByteArray();
             }
             baos.write(buffer, 0, count);
-
-            if (!containsLine(buffer, count)) {
-                throw new IOException(Messages.getString("archive.2E")); //$NON-NLS-1$
-            }
         }
     }