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/03/22 10:09:25 UTC

svn commit: r757150 - in /harmony/enhanced/classlib/trunk/modules: archive/src/main/java/java/util/jar/Manifest.java luni/src/main/java/org/apache/harmony/luni/util/InputStreamExposer.java

Author: tellison
Date: Sun Mar 22 09:09:24 2009
New Revision: 757150

URL: http://svn.apache.org/viewvc?rev=757150&view=rev
Log:
Fix for HARMONY-6121 ([classlib][archive] org.apache.harmony.archive.tests.java.util.jar.ManifestTest fails)

Modified:
    harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/Manifest.java
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/InputStreamExposer.java

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=757150&r1=757149&r2=757150&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 Sun Mar 22 09:09:24 2009
@@ -17,6 +17,7 @@
 
 package java.util.jar;
 
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -204,10 +205,15 @@
      */
     public void read(InputStream is) throws IOException {
         byte[] buf;
+        // Try to read get a reference to the bytes directly
         try {
             buf = InputStreamExposer.expose(is);
-        } catch (OutOfMemoryError oome) {
-            throw new IOException(Messages.getString("archive.2E")); //$NON-NLS-1$
+        } catch (UnsupportedOperationException uoe) {
+            buf = readFully(is);
+        }
+
+        if (buf.length == 0) {
+            return;
         }
 
         // a workaround for HARMONY-5662
@@ -226,7 +232,45 @@
         im.initEntries(entries, chunks);
         im = null;
     }
+    
+    /*
+     * Helper to read the entire contents of the manifest from the
+     * given input stream.  Usually we can do this in a single read
+     * but we need to account for 'infinite' streams, by ensuring we
+     * 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];
 
+        while (true) {
+            int 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$
+            }
+        }
+    }
+
+    /*
+     * Check to see if the buffer contains a newline or carriage
+     * return character within the first 'length' bytes.  Used to
+     * check the validity of the manifest input stream.
+     */
+    private boolean containsLine(byte[] buffer, int length) {
+        for (int i = 0; i < length; i++) {
+            if (buffer[i] == 0x0A || buffer[i] == 0x0D) {
+                return true;
+            }
+        }
+        return false;
+    }
+    
     /**
      * Returns the hashCode for this instance.
      * 

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/InputStreamExposer.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/InputStreamExposer.java?rev=757150&r1=757149&r2=757150&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/InputStreamExposer.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/InputStreamExposer.java Sun Mar 22 09:09:24 2009
@@ -99,8 +99,9 @@
      * @param is
      *            the stream to be read.
      * @return the snapshot wrapping the buffer where the bytes are read to.
+     * @throws UnsupportedOperationException if the input stream data cannot be exposed
      */
-    public static byte[] expose(InputStream is) throws IOException {
+    public static byte[] expose(InputStream is) throws IOException, UnsupportedOperationException {
         if (is instanceof ExposedByteArrayInputStream) {
             return ((ExposedByteArrayInputStream) is).expose();
         }
@@ -109,17 +110,7 @@
             return expose((ByteArrayInputStream) is);
         }
 
-        // this may be slow, put optimizations suitable for your stream
-        // before trying this
-        ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        byte[] buffer = new byte[8192];
-
-        while (true) {
-            int count = is.read(buffer);
-            if (count == -1) {
-                return baos.toByteArray();
-            }
-            baos.write(buffer, 0, count);
-        }
+        // We don't know how to do this
+        throw new UnsupportedOperationException();
     }
 }