You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by bo...@apache.org on 2009/02/18 16:21:34 UTC

svn commit: r745537 - in /ant/core/trunk/src: main/org/apache/tools/zip/ZipEntry.java tests/junit/org/apache/tools/zip/ZipEntryTest.java

Author: bodewig
Date: Wed Feb 18 15:21:32 2009
New Revision: 745537

URL: http://svn.apache.org/viewvc?rev=745537&view=rev
Log:
allow extra fields to be added at the front

Modified:
    ant/core/trunk/src/main/org/apache/tools/zip/ZipEntry.java
    ant/core/trunk/src/tests/junit/org/apache/tools/zip/ZipEntryTest.java

Modified: ant/core/trunk/src/main/org/apache/tools/zip/ZipEntry.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/zip/ZipEntry.java?rev=745537&r1=745536&r2=745537&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/zip/ZipEntry.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/zip/ZipEntry.java Wed Feb 18 15:21:32 2009
@@ -215,6 +215,9 @@
     /**
      * Adds an extra fields - replacing an already present extra field
      * of the same type.
+     *
+     * <p>If no extra field of the same type exists, the field will be
+     * added as last field.</p>
      * @param ze an extra field
      * @since 1.1
      */
@@ -227,6 +230,25 @@
     }
 
     /**
+     * Adds an extra fields - replacing an already present extra field
+     * of the same type.
+     *
+     * <p>The new extra field will be the first one.</p>
+     * @param ze an extra field
+     * @since 1.1
+     */
+    public void addAsFirstExtraField(ZipExtraField ze) {
+        LinkedHashMap copy = extraFields;
+        extraFields = new LinkedHashMap();
+        extraFields.put(ze.getHeaderId(), ze);
+        if (copy != null) {
+            copy.remove(ze.getHeaderId());
+            extraFields.putAll(copy);
+        }
+        setExtra();
+    }
+
+    /**
      * Remove an extra fields.
      * @param type the type of extra field to remove
      * @since 1.1

Modified: ant/core/trunk/src/tests/junit/org/apache/tools/zip/ZipEntryTest.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/tests/junit/org/apache/tools/zip/ZipEntryTest.java?rev=745537&r1=745536&r2=745537&view=diff
==============================================================================
--- ant/core/trunk/src/tests/junit/org/apache/tools/zip/ZipEntryTest.java (original)
+++ ant/core/trunk/src/tests/junit/org/apache/tools/zip/ZipEntryTest.java Wed Feb 18 15:21:32 2009
@@ -85,6 +85,46 @@
         }
     }
 
+    /**
+     * test handling of extra fields
+     *
+     * @since 1.1
+     */
+    public void testAddAsFirstExtraField() {
+        AsiExtraField a = new AsiExtraField();
+        a.setDirectory(true);
+        a.setMode(0755);
+        UnrecognizedExtraField u = new UnrecognizedExtraField();
+        u.setHeaderId(new ZipShort(1));
+        u.setLocalFileDataData(new byte[0]);
+
+        ZipEntry ze = new ZipEntry("test/");
+        ze.setExtraFields(new ZipExtraField[] {a, u});
+        byte[] data1 = ze.getExtra();
+
+        UnrecognizedExtraField u2 = new UnrecognizedExtraField();
+        u2.setHeaderId(new ZipShort(1));
+        u2.setLocalFileDataData(new byte[] {1});
+
+        ze.addAsFirstExtraField(u2);
+        byte[] data2 = ze.getExtra();
+        ZipExtraField[] result = ze.getExtraFields();
+        assertEquals("second pass", 2, result.length);
+        assertSame(u2, result[0]);
+        assertSame(a, result[1]);
+        assertEquals("length second pass", data1.length + 1, data2.length);
+
+        UnrecognizedExtraField u3 = new UnrecognizedExtraField();
+        u3.setHeaderId(new ZipShort(2));
+        u3.setLocalFileDataData(new byte[] {1});
+        ze.addAsFirstExtraField(u3);
+        result = ze.getExtraFields();
+        assertEquals("third pass", 3, result.length);
+        assertSame(u3, result[0]);
+        assertSame(u2, result[1]);
+        assertSame(a, result[2]);
+    }
+
     public void testUnixMode() {
         ZipEntry ze = new ZipEntry("foo");
         assertEquals(0, ze.getPlatform());