Merge change 24110 into eclair

* changes:
  JarFile was not able to verify signed files with size 0.
diff --git a/libcore/archive/src/main/java/java/util/jar/JarFile.java b/libcore/archive/src/main/java/java/util/jar/JarFile.java
index d6e8339..6f4eb83 100644
--- a/libcore/archive/src/main/java/java/util/jar/JarFile.java
+++ b/libcore/archive/src/main/java/java/util/jar/JarFile.java
@@ -68,6 +68,10 @@
 
         private JarVerifier.VerifierEntry entry;
 
+        // BEGIN android-added
+        private boolean done = false;
+        // END android-added
+
         JarFileInputStream(InputStream is, ZipEntry ze,
                 JarVerifier.VerifierEntry e) {
             super(is);
@@ -78,6 +82,10 @@
 
         @Override
         public int read() throws IOException {
+            // BEGIN android-changed
+            if (done) {
+                return -1;
+            }
             if (count > 0) {
                 int r = super.read();
                 if (r != -1) {
@@ -87,16 +95,24 @@
                     count = 0;
                 }
                 if (count == 0) {
+                    done = true;
                     entry.verify();
                 }
                 return r;
             } else {
+                done = true;
+                entry.verify();
                 return -1;
             }
+            // END android-changed
         }
 
         @Override
         public int read(byte[] buf, int off, int nbytes) throws IOException {
+            // BEGIN android-changed
+            if (done) {
+                return -1;
+            }
             if (count > 0) {
                 int r = super.read(buf, off, nbytes);
                 if (r != -1) {
@@ -110,22 +126,25 @@
                     count = 0;
                 }
                 if (count == 0) {
+                    done = true;
                     entry.verify();
                 }
                 return r;
             } else {
+                done = true;
+                entry.verify();
                 return -1;
             }
+            // END android-changed
         }
 
         // BEGIN android-added
         @Override
         public int available() throws IOException {
-            if (count > 0) {
-                return super.available();
-            } else {
+            if (done) {
                 return 0;
             }
+            return super.available();
         }
         // END android-added
 
diff --git a/libcore/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarFileTest.java b/libcore/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarFileTest.java
index 96321a4..d2a5110 100644
--- a/libcore/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarFileTest.java
+++ b/libcore/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarFileTest.java
@@ -18,10 +18,15 @@
 
 
 import dalvik.annotation.AndroidOnly;
-import dalvik.annotation.TestTargetClass;
 import dalvik.annotation.TestLevel;
+import dalvik.annotation.TestTargetClass;
 import dalvik.annotation.TestTargetNew;
 
+import junit.framework.TestCase;
+
+import tests.support.Support_PlatformFile;
+import tests.support.resource.Support_Resources;
+
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileOutputStream;
@@ -41,10 +46,6 @@
 import java.util.zip.ZipException;
 import java.util.zip.ZipFile;
 
-import junit.framework.TestCase;
-import tests.support.Support_PlatformFile;
-import tests.support.resource.Support_Resources;
-
 
 @TestTargetClass(JarFile.class)
 public class JarFileTest extends TestCase {
@@ -74,14 +75,22 @@
 
     private final String jarName5 = "hyts_signed_inc.jar";
 
-    private final String integrateJar = "Integrate.jar";
-
     private final String entryName = "foo/bar/A.class";
 
     private final String entryName3 = "coucou/FileAccess.class";
 
+    private final String integrateJar = "Integrate.jar";
+
     private final String integrateJarEntry = "Test.class";
 
+    private final String emptyEntryJar = "EmptyEntries_signed.jar";
+
+    private final String emptyEntry1 = "subfolder/internalSubset01.js";
+
+    private final String emptyEntry2 = "svgtest.js";
+
+    private final String emptyEntry3 = "svgunit.js";
+
     private File resources;
 
     // custom security manager
@@ -1061,4 +1070,33 @@
             // expected
         }
     }
+
+    /**
+     * The jar is intact, but the entry object is modified.
+     */
+    @TestTargetNew(
+            level = TestLevel.PARTIAL_COMPLETE,
+            notes = "Regression test for issue introduced by HAROMNY-4569. "
+                + "signed archives containing files with size 0 could not get verified",
+            method = "getInputStream",
+            args = {ZipEntry.class}
+    )
+    public void testJarVerificationEmptyEntry() throws IOException {
+        Support_Resources.copyFile(resources, null, emptyEntryJar);
+        File f = new File(resources, emptyEntryJar);
+
+        JarFile jarFile = new JarFile(f);
+
+        ZipEntry zipEntry = jarFile.getJarEntry(emptyEntry1);
+        int res = jarFile.getInputStream(zipEntry).read(new byte[100], 0, 100);
+        assertEquals("Wrong length of empty jar entry", -1, res);
+
+        zipEntry = jarFile.getJarEntry(emptyEntry2);
+        res = jarFile.getInputStream(zipEntry).read(new byte[100], 0, 100);
+        assertEquals("Wrong length of empty jar entry", -1, res);
+
+        zipEntry = jarFile.getJarEntry(emptyEntry3);
+        res = jarFile.getInputStream(zipEntry).read();
+        assertEquals("Wrong length of empty jar entry", -1, res);
+    }
 }
diff --git a/libcore/support/src/test/java/tests/resources/EmptyEntries_signed.jar b/libcore/support/src/test/java/tests/resources/EmptyEntries_signed.jar
new file mode 100644
index 0000000..237d244
--- /dev/null
+++ b/libcore/support/src/test/java/tests/resources/EmptyEntries_signed.jar
Binary files differ