harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r823576 - in /harmony/enhanced/classlib/trunk/modules/archive/src: main/java/java/util/jar/ main/java/org/apache/harmony/archive/util/ test/java-internal/org/apache/harmony/archive/util/
Date Fri, 09 Oct 2009 15:06:49 GMT
Author: tellison
Date: Fri Oct  9 15:06:48 2009
New Revision: 823576

URL: http://svn.apache.org/viewvc?rev=823576&view=rev
Log:
Updates for clarity.
 - rename the Util methods and given them some javadoc.
 - remove the general regionMatches, and replace with equals and endsWith methods.
 - give JarFile a few comments to explain what is happening.

Modified:
    harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/Attributes.java
    harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/JarFile.java
    harmony/enhanced/classlib/trunk/modules/archive/src/main/java/org/apache/harmony/archive/util/Util.java
    harmony/enhanced/classlib/trunk/modules/archive/src/test/java-internal/org/apache/harmony/archive/util/UtilTest.java

Modified: harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/Attributes.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/Attributes.java?rev=823576&r1=823575&r2=823576&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/Attributes.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/Attributes.java
Fri Oct  9 15:06:48 2009
@@ -225,7 +225,7 @@
         }
 
         /**
-         * returns whether the argument provided is the same as the attribute
+         * Returns whether the argument provided is the same as the attribute
          * name.
          *
          * @return if the attribute names correspond.
@@ -239,7 +239,7 @@
                 return false;
             }
 
-            return Util.equalsIgnoreCase(name, ((Name) object).name);
+            return Util.asciiEqualsIgnoreCase(name, ((Name) object).name);
         }
 
         /**

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=823576&r1=823575&r2=823576&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 15:06:48 2009
@@ -45,10 +45,13 @@
      */
     public static final String MANIFEST_NAME = "META-INF/MANIFEST.MF"; //$NON-NLS-1$
 
+    // The directory containing the manifest.
     static final String META_DIR = "META-INF/"; //$NON-NLS-1$
 
+    // The manifest after it has been read from the JAR.
     private Manifest manifest;
 
+    // The entry for the MANIFEST.MF file before it is read.
     private ZipEntry manifestEntry;
 
     JarVerifier verifier;
@@ -213,7 +216,6 @@
      */
     public JarFile(String filename) throws IOException {
         this(filename, true);
-
     }
 
     /**
@@ -329,6 +331,7 @@
      */
     public Manifest getManifest() throws IOException {
         if (closed) {
+            // archive.35=JarFile has been closed
             throw new IllegalStateException(Messages.getString("archive.35")); //$NON-NLS-1$
         }
         if (manifest != null) {
@@ -341,39 +344,48 @@
                 verifier.addMetaEntry(manifestEntry.getName(), buffer);
             }
             manifest = new Manifest(buffer, verifier != null);
-            manifestEntry = null;
+            manifestEntry = null;  // Can discard the entry now.
         } catch (NullPointerException e) {
             manifestEntry = null;
         }
         return manifest;
     }
 
+    /**
+     * Called by the JarFile constructors, this method reads the contents of the
+     * file's META-INF/ directory and picks out the MANIFEST.MF file and
+     * verifier signature files if they exist. Any signature files found are
+     * registered with the verifier.
+     * 
+     * @throws IOException
+     *             if there is a problem reading the jar file entries.
+     */
     private void readMetaEntries() throws IOException {
+        // Get all meta directory entries
         ZipEntry[] metaEntries = getMetaEntriesImpl();
-        int dirLength = META_DIR.length();
+        if (metaEntries == null) {
+            verifier = null;
+            return;
+        }
 
         boolean signed = false;
 
-        if (null != metaEntries) {
-            for (ZipEntry entry : metaEntries) {
-                String entryName = entry.getName();
-                if (manifestEntry == null
-                        && manifest == null
-                        && Util.ASCIIIgnoreCaseRegionMatches(entryName,
-                                dirLength, MANIFEST_NAME, dirLength,
-                                MANIFEST_NAME.length() - dirLength)) {
-                    manifestEntry = entry;
-                    if (verifier == null) {
-                        break;
-                    }
-                } else if (verifier != null
-                        && entryName.length() > dirLength
-                        && (Util.ASCIIIgnoreCaseRegionMatches(entryName,
-                                entryName.length() - 3, ".SF", 0, 3) //$NON-NLS-1$
-                                || Util.ASCIIIgnoreCaseRegionMatches(entryName,
-                                        entryName.length() - 4, ".DSA", 0, 4) //$NON-NLS-1$
-                        || Util.ASCIIIgnoreCaseRegionMatches(entryName,
-                                entryName.length() - 4, ".RSA", 0, 4))) { //$NON-NLS-1$
+        for (ZipEntry entry : metaEntries) {
+            String entryName = entry.getName();
+            // Is this the entry for META-INF/MANIFEST.MF ?
+            if (manifestEntry == null
+                    && Util.asciiEqualsIgnoreCase(MANIFEST_NAME, entryName)) {
+                manifestEntry = entry;
+                // If there is no verifier then we don't need to look any further.
+                if (verifier == null) {
+                    break;
+                }
+            } else {
+                // Is this an entry that the verifier needs?
+                if (verifier != null
+                        && (Util.asciiEndsWithIgnoreCase(entryName, ".SF")
+                                || Util.asciiEndsWithIgnoreCase(entryName, ".DSA")
+                                || Util.asciiEndsWithIgnoreCase(entryName, ".RSA"))) {
                     signed = true;
                     InputStream is = super.getInputStream(entry);
                     byte[] buf = getAllBytesFromStreamAndClose(is);
@@ -381,6 +393,8 @@
                 }
             }
         }
+
+        // If there were no signature files, then no verifier work to do.
         if (!signed) {
             verifier = null;
         }
@@ -449,24 +463,28 @@
         return je;
     }
 
+    /**
+     * Returns all the ZipEntry's that relate to files in the
+     * JAR's META-INF directory.
+     *
+     * @return the list of ZipEntry's or {@code null} if there are none.
+     */
     private ZipEntry[] getMetaEntriesImpl() {
-        List<ZipEntry> list = new ArrayList<ZipEntry>();
-
+        List<ZipEntry> list = new ArrayList<ZipEntry>(8);
         Enumeration<? extends ZipEntry> allEntries = entries();
         while (allEntries.hasMoreElements()) {
             ZipEntry ze = allEntries.nextElement();
-            if (ze.getName().startsWith("META-INF/") && //$NON-NLS-1$
-                    ze.getName().length() > 9) {
+            if (ze.getName().startsWith(META_DIR)
+                    && ze.getName().length() > META_DIR.length()) {
                 list.add(ze);
             }
         }
-        if (list.size() != 0) {
-            ZipEntry[] result = new ZipEntry[list.size()];
-            list.toArray(result);
-            return result;
-        } else {
+        if (list.size() == 0) {
             return null;
         }
+        ZipEntry[] result = new ZipEntry[list.size()];
+        list.toArray(result);
+        return result;
     }
 
     /**

Modified: harmony/enhanced/classlib/trunk/modules/archive/src/main/java/org/apache/harmony/archive/util/Util.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/org/apache/harmony/archive/util/Util.java?rev=823576&r1=823575&r2=823576&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/archive/src/main/java/org/apache/harmony/archive/util/Util.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/archive/src/main/java/org/apache/harmony/archive/util/Util.java
Fri Oct  9 15:06:48 2009
@@ -17,62 +17,113 @@
 
 package org.apache.harmony.archive.util;
 
+/**
+ * Helpers for the archive module.
+ */
 public class Util {
 
-    public static boolean ASCIIIgnoreCaseRegionMatches(String s1, int start1,
-            String s2, int start2, int length) {
-
-        if (s1 != null && s2 != null) {
-            if (start1 < 0 || length > s1.length() - start1) {
-                return false;
-            }
-            if (start2 < 0 || length > s2.length() - start2) {
+    /**
+     * Returns whether the given source string ends with the suffix, ignoring
+     * case and assuming that the strings are ascii encoded.
+     * 
+     * @param source
+     *            the string to match.
+     * @param suffix
+     *            the suffix to test.
+     * @return {@code true} if the source does end with the given suffix, or
+     *         {@code false} if not.
+     */
+    public static boolean asciiEndsWithIgnoreCase(String source, String suffix) {
+        int length = suffix.length();
+        if (length > source.length()) {
+            return false;
+        }
+        int offset = source.length() - length;
+        for (int i = 0; i < length; i++) {
+            char c1 = source.charAt(i + offset);
+            char c2 = suffix.charAt(i);
+            if (c1 != c2 && toASCIIUpperCase(c1) != toASCIIUpperCase(c2)) {
                 return false;
             }
-
-            char c1, c2;
-            for (int i = 0; i < length; i++) {
-                if ((c1 = s1.charAt(start1++)) != (c2 = s2.charAt(start2++))
-                        && toASCIIUpperCase(c1) != toASCIIUpperCase(c2)) {
-                    return false;
-                }
-            }
-            return true;
         }
-        throw new NullPointerException();
+        return true;
     }
 
-    public static final boolean equalsIgnoreCase(byte[] buf1, byte[] buf2) {
+    /**
+     * Compares the given byte arrays and returns whether they are equal,
+     * ignoring case differences and assuming they are ascii-encoded strings.
+     * 
+     * @param buf1
+     *            first byte array to compare.
+     * @param buf2
+     *            second byte array to compare.
+     * @return the result of the comparison.
+     */
+    public static boolean asciiEqualsIgnoreCase(byte[] buf1, byte[] buf2) {
+        if (buf1 == null || buf2 == null) {
+            return false;
+        }
         if (buf1 == buf2) {
             return true;
         }
-
-        if (buf1 == null || buf2 == null || buf1.length != buf2.length) {
+        if (buf1.length != buf2.length) {
             return false;
         }
 
-        byte b1, b2;
-
         for (int i = 0; i < buf1.length; i++) {
-            if ((b1 = buf1[i]) != (b2 = buf2[i])
-                    && toASCIIUpperCase(b1) != toASCIIUpperCase(b2)) {
+            byte b1 = buf1[i];
+            byte b2 = buf2[i];
+            if (b1 != b2 && toASCIIUpperCase(b1) != toASCIIUpperCase(b2)) {
                 return false;
             }
         }
         return true;
     }
 
-    static final char toASCIIUpperCase(char c) {
-        if ('a' <= c && c <= 'z') {
-            return (char) (c - ('a' - 'A'));
+    /**
+     * Compares the given strings and returns whether they are equal, ignoring
+     * case differences and assuming they are ascii-encoded strings.
+     * 
+     * @param s1
+     *            first string to compare.
+     * @param s2
+     *            second string to compare.
+     * @return the result of the comparison.
+     */
+    public static boolean asciiEqualsIgnoreCase(String s1, String s2) {
+        if (s1 == null || s2 == null) {
+            return false;
         }
-        return c;
+        if (s1 == s2) {
+            return true;
+        }
+
+        int length = s1.length();
+        if (length != s2.length()) {
+            return false;
+        }
+
+        for (int i = 0; i < length; i++) {
+            char c1 = s1.charAt(i);
+            char c2 = s2.charAt(i);
+            if (c1 != c2 && toASCIIUpperCase(c1) != toASCIIUpperCase(c2)) {
+                return false;
+            }
+        }
+        return true;
     }
 
-    static final byte toASCIIUpperCase(byte b) {
+    private static final byte toASCIIUpperCase(byte b) {
         if ('a' <= b && b <= 'z') {
             return (byte) (b - ('a' - 'A'));
         }
         return b;
     }
+
+    private static final char toASCIIUpperCase(char c) {
+        if ('a' <= c && c <= 'z') {
+            return (char) (c - ('a' - 'A'));
+        }
+        return c;
+    }
 }

Modified: harmony/enhanced/classlib/trunk/modules/archive/src/test/java-internal/org/apache/harmony/archive/util/UtilTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/archive/src/test/java-internal/org/apache/harmony/archive/util/UtilTest.java?rev=823576&r1=823575&r2=823576&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/archive/src/test/java-internal/org/apache/harmony/archive/util/UtilTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/archive/src/test/java-internal/org/apache/harmony/archive/util/UtilTest.java
Fri Oct  9 15:06:48 2009
@@ -43,44 +43,27 @@
         }
     }
 
-    public void testASCIIIgnoreCaseRegionMatches() {
+    public void testasciiEndsWithIgnoreCase() {
         final String s1 = ASCII_ALPHABET_LC;
         final String s2 = ASCII_ALPHABET_UC;
-        for (int i = 0; i < s1.length(); i++) {
-            assertTrue(Util.ASCIIIgnoreCaseRegionMatches(s1, i, s2, i, s1
-                    .length()
-                    - i));
-        }
-    }
-
-    public void testToASCIIUpperCaseByte() {
-        for (int i = 0; i < ASCII_ALPHABET_LC_BYTES.length; i++) {
-            assertEquals(ASCII_ALPHABET_UC_BYTES[i], Util
-                    .toASCIIUpperCase(ASCII_ALPHABET_LC_BYTES[i]));
-        }
-        for (int i = 0; i < ASCII_ALPHABET_UC_BYTES.length; i++) {
-            assertEquals(ASCII_ALPHABET_UC_BYTES[i], Util
-                    .toASCIIUpperCase(ASCII_ALPHABET_UC_BYTES[i]));
-        }
+        assertTrue(Util.asciiEndsWithIgnoreCase(s1, s2));
+        assertTrue(Util.asciiEndsWithIgnoreCase(s2, s1));
+        assertTrue(Util.asciiEndsWithIgnoreCase(s1, "wxyz"));
     }
 
-    public void testToASCIIUpperCaseChar() {
-        for (int i = 0; i < ASCII_ALPHABET_LC.length(); i++) {
-            assertEquals(ASCII_ALPHABET_UC.charAt(i), Util
-                    .toASCIIUpperCase(ASCII_ALPHABET_LC.charAt(i)));
-        }
-        for (int i = 0; i < ASCII_ALPHABET_UC.length(); i++) {
-            assertEquals(ASCII_ALPHABET_UC.charAt(i), Util
-                    .toASCIIUpperCase(ASCII_ALPHABET_UC.charAt(i)));
-        }
+    public void testasciiEqualsIgnoreCase() {
+        final String s1 = ASCII_ALPHABET_LC;
+        final String s2 = ASCII_ALPHABET_UC;
+        assertTrue(Util.asciiEqualsIgnoreCase(s1, s2));
+        assertTrue(Util.asciiEqualsIgnoreCase(s2, s1));
     }
 
     public void testEqualsIgnoreCaseByteArrayByteArray() {
-        assertTrue(Util.equalsIgnoreCase(ASCII_ALPHABET_LC_BYTES,
+        assertTrue(Util.asciiEqualsIgnoreCase(ASCII_ALPHABET_LC_BYTES,
                 ASCII_ALPHABET_LC_BYTES));
-        assertTrue(Util.equalsIgnoreCase(ASCII_ALPHABET_LC_BYTES,
+        assertTrue(Util.asciiEqualsIgnoreCase(ASCII_ALPHABET_LC_BYTES,
                 ASCII_ALPHABET_UC_BYTES));
-        assertTrue(Util.equalsIgnoreCase(ASCII_ALPHABET_UC_BYTES,
+        assertTrue(Util.asciiEqualsIgnoreCase(ASCII_ALPHABET_UC_BYTES,
                 ASCII_ALPHABET_UC_BYTES));
     }
 



Mime
View raw message