ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From peterrei...@apache.org
Subject svn commit: r569089 - in /ant/core/trunk/src/main/org/apache/tools/zip: ExtraFieldUtils.java ZipEntry.java ZipFile.java ZipLong.java ZipOutputStream.java ZipShort.java
Date Thu, 23 Aug 2007 17:58:35 GMT
Author: peterreilly
Date: Thu Aug 23 10:58:32 2007
New Revision: 569089

URL: http://svn.apache.org/viewvc?rev=569089&view=rev
Log:
magic numbers

Modified:
    ant/core/trunk/src/main/org/apache/tools/zip/ExtraFieldUtils.java
    ant/core/trunk/src/main/org/apache/tools/zip/ZipEntry.java
    ant/core/trunk/src/main/org/apache/tools/zip/ZipFile.java
    ant/core/trunk/src/main/org/apache/tools/zip/ZipLong.java
    ant/core/trunk/src/main/org/apache/tools/zip/ZipOutputStream.java
    ant/core/trunk/src/main/org/apache/tools/zip/ZipShort.java

Modified: ant/core/trunk/src/main/org/apache/tools/zip/ExtraFieldUtils.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/zip/ExtraFieldUtils.java?rev=569089&r1=569088&r2=569089&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/zip/ExtraFieldUtils.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/zip/ExtraFieldUtils.java Thu Aug 23 10:58:32
2007
@@ -29,6 +29,8 @@
 // CheckStyle:HideUtilityClassConstructorCheck OFF (bc)
 public class ExtraFieldUtils {
 
+    private static final int WORD = 4;
+
     /**
      * Static registry of known extra fields.
      *
@@ -95,23 +97,23 @@
     public static ZipExtraField[] parse(byte[] data) throws ZipException {
         Vector v = new Vector();
         int start = 0;
-        while (start <= data.length - 4) {
+        while (start <= data.length - WORD) {
             ZipShort headerId = new ZipShort(data, start);
             int length = (new ZipShort(data, start + 2)).getValue();
-            if (start + 4 + length > data.length) {
+            if (start + WORD + length > data.length) {
                 throw new ZipException("data starting at " + start
                     + " is in unknown format");
             }
             try {
                 ZipExtraField ze = createExtraField(headerId);
-                ze.parseFromLocalFileData(data, start + 4, length);
+                ze.parseFromLocalFileData(data, start + WORD, length);
                 v.addElement(ze);
             } catch (InstantiationException ie) {
                 throw new ZipException(ie.getMessage());
             } catch (IllegalAccessException iae) {
                 throw new ZipException(iae.getMessage());
             }
-            start += (length + 4);
+            start += (length + WORD);
         }
         if (start != data.length) { // array not exhausted
             throw new ZipException("data starting at " + start
@@ -130,7 +132,7 @@
      * @since 1.1
      */
     public static byte[] mergeLocalFileDataData(ZipExtraField[] data) {
-        int sum = 4 * data.length;
+        int sum = WORD * data.length;
         for (int i = 0; i < data.length; i++) {
             sum += data[i].getLocalFileDataLength().getValue();
         }
@@ -142,8 +144,8 @@
             System.arraycopy(data[i].getLocalFileDataLength().getBytes(),
                              0, result, start + 2, 2);
             byte[] local = data[i].getLocalFileDataData();
-            System.arraycopy(local, 0, result, start + 4, local.length);
-            start += (local.length + 4);
+            System.arraycopy(local, 0, result, start + WORD, local.length);
+            start += (local.length + WORD);
         }
         return result;
     }
@@ -155,7 +157,7 @@
      * @since 1.1
      */
     public static byte[] mergeCentralDirectoryData(ZipExtraField[] data) {
-        int sum = 4 * data.length;
+        int sum = WORD * data.length;
         for (int i = 0; i < data.length; i++) {
             sum += data[i].getCentralDirectoryLength().getValue();
         }
@@ -167,8 +169,8 @@
             System.arraycopy(data[i].getCentralDirectoryLength().getBytes(),
                              0, result, start + 2, 2);
             byte[] local = data[i].getCentralDirectoryData();
-            System.arraycopy(local, 0, result, start + 4, local.length);
-            start += (local.length + 4);
+            System.arraycopy(local, 0, result, start + WORD, local.length);
+            start += (local.length + WORD);
         }
         return result;
     }

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=569089&r1=569088&r2=569089&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 Thu Aug 23 10:58:32 2007
@@ -30,6 +30,8 @@
 
     private static final int PLATFORM_UNIX = 3;
     private static final int PLATFORM_FAT  = 0;
+    private static final int SHORT_MASK = 0xFFFF;
+    private static final int SHORT_SHIFT = 16;
 
     private int internalAttributes = 0;
     private int platform = PLATFORM_FAT;
@@ -142,11 +144,13 @@
      * @since Ant 1.5.2
      */
     public void setUnixMode(int mode) {
+        // CheckStyle:MagicNumberCheck OFF - no point
         setExternalAttributes((mode << 16)
                               // MS-DOS read-only attribute
                               | ((mode & 0200) == 0 ? 1 : 0)
                               // MS-DOS directory flag
                               | (isDirectory() ? 0x10 : 0));
+        // CheckStyle:MagicNumberCheck ON
         platform = PLATFORM_UNIX;
     }
 
@@ -156,7 +160,7 @@
      * @since Ant 1.6
      */
     public int getUnixMode() {
-        return (int) ((getExternalAttributes() >> 16) & 0xFFFF);
+        return (int) ((getExternalAttributes() >> SHORT_SHIFT) & SHORT_MASK);
     }
 
     /**

Modified: ant/core/trunk/src/main/org/apache/tools/zip/ZipFile.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/zip/ZipFile.java?rev=569089&r1=569088&r2=569089&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/zip/ZipFile.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/zip/ZipFile.java Thu Aug 23 10:58:32 2007
@@ -62,6 +62,12 @@
     private static final int HASH_SIZE = 509;
     private static final int SHORT     =   2;
     private static final int WORD      =   4;
+    private static final int NIBLET_MASK = 0x0f;
+    private static final int BYTE_SHIFT = 8;
+    private static final int POS_0 = 0;
+    private static final int POS_1 = 1;
+    private static final int POS_2 = 2;
+    private static final int POS_3 = 3;
 
     /**
      * Maps ZipEntrys to Longs, recording the offsets of the local
@@ -277,7 +283,7 @@
 
             int versionMadeBy = ZipShort.getValue(cfh, off);
             off += SHORT;
-            ze.setPlatform((versionMadeBy >> 8) & 0x0F);
+            ze.setPlatform((versionMadeBy >> BYTE_SHIFT) & NIBLET_MASK);
 
             off += WORD; // skip version info and general purpose byte
 
@@ -381,13 +387,13 @@
             byte[] sig = ZipOutputStream.EOCD_SIG;
             int curr = archive.read();
             while (curr != -1) {
-                if (curr == sig[0]) {
+                if (curr == sig[POS_0]) {
                     curr = archive.read();
-                    if (curr == sig[1]) {
+                    if (curr == sig[POS_1]) {
                         curr = archive.read();
-                        if (curr == sig[SHORT]) {
+                        if (curr == sig[POS_2]) {
                             curr = archive.read();
-                            if (curr == sig[3]) {
+                            if (curr == sig[POS_3]) {
                                 found = true;
                                 break;
                             }
@@ -471,12 +477,14 @@
      */
     private static long dosToJavaTime(long dosTime) {
         Calendar cal = Calendar.getInstance();
+        // CheckStyle:MagicNumberCheck OFF - no point
         cal.set(Calendar.YEAR, (int) ((dosTime >> 25) & 0x7f) + 1980);
         cal.set(Calendar.MONTH, (int) ((dosTime >> 21) & 0x0f) - 1);
         cal.set(Calendar.DATE, (int) (dosTime >> 16) & 0x1f);
         cal.set(Calendar.HOUR_OF_DAY, (int) (dosTime >> 11) & 0x1f);
         cal.set(Calendar.MINUTE, (int) (dosTime >> 5) & 0x3f);
         cal.set(Calendar.SECOND, (int) (dosTime << 1) & 0x3e);
+        // CheckStyle:MagicNumberCheck ON
         return cal.getTime().getTime();
     }
 

Modified: ant/core/trunk/src/main/org/apache/tools/zip/ZipLong.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/zip/ZipLong.java?rev=569089&r1=569088&r2=569089&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/zip/ZipLong.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/zip/ZipLong.java Thu Aug 23 10:58:32 2007
@@ -25,6 +25,22 @@
  */
 public final class ZipLong implements Cloneable {
 
+    private static final int WORD = 4;
+    private static final int BYTE_BIT_SIZE = 8;
+    private static final int BYTE_MASK = 0xFF;
+
+    private static final int BYTE_1 = 1;
+    private static final int BYTE_1_MASK = 0xFF00;
+    private static final int BYTE_1_SHIFT = 8;
+
+    private static final int BYTE_2 = 2;
+    private static final int BYTE_2_MASK = 0xFF0000;
+    private static final int BYTE_2_SHIFT = 16;
+
+    private static final int BYTE_3 = 3;
+    private static final long BYTE_3_MASK = 0xFF000000L;
+    private static final int BYTE_3_SHIFT = 24;
+
     private long value;
 
     /**
@@ -79,11 +95,11 @@
      * @return value as four bytes in big endian byte order
      */
     public static byte[] getBytes(long value) {
-        byte[] result = new byte[4];
-        result[0] = (byte) ((value & 0xFF));
-        result[1] = (byte) ((value & 0xFF00) >> 8);
-        result[2] = (byte) ((value & 0xFF0000) >> 16);
-        result[3] = (byte) ((value & 0xFF000000L) >> 24);
+        byte[] result = new byte[WORD];
+        result[0] = (byte) ((value & BYTE_MASK));
+        result[BYTE_1] = (byte) ((value & BYTE_1_MASK) >> BYTE_1_SHIFT);
+        result[BYTE_2] = (byte) ((value & BYTE_2_MASK) >> BYTE_2_SHIFT);
+        result[BYTE_3] = (byte) ((value & BYTE_3_MASK) >> BYTE_3_SHIFT);
         return result;
     }
 
@@ -94,10 +110,10 @@
      * @return the correspondanding Java long value
      */
     public static long getValue(byte[] bytes, int offset) {
-        long value = (bytes[offset + 3] << 24) & 0xFF000000L;
-        value += (bytes[offset + 2] << 16) & 0xFF0000;
-        value += (bytes[offset + 1] << 8) & 0xFF00;
-        value += (bytes[offset] & 0xFF);
+        long value = (bytes[offset + BYTE_3] << BYTE_3_SHIFT) & BYTE_3_MASK;
+        value += (bytes[offset + BYTE_2] << BYTE_2_SHIFT) & BYTE_2_MASK;
+        value += (bytes[offset + BYTE_1] << BYTE_1_SHIFT) & BYTE_1_MASK;
+        value += (bytes[offset] & BYTE_MASK);
         return value;
     }
 

Modified: ant/core/trunk/src/main/org/apache/tools/zip/ZipOutputStream.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/zip/ZipOutputStream.java?rev=569089&r1=569088&r2=569089&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/zip/ZipOutputStream.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/zip/ZipOutputStream.java Thu Aug 23 10:58:32
2007
@@ -57,6 +57,7 @@
     private static final int BYTE_MASK = 0xFF;
     private static final int SHORT = 2;
     private static final int WORD = 4;
+    private static final int BUFFER_SIZE = 512;
 
     /**
      * Compression method for deflated entries.
@@ -221,7 +222,7 @@
      *
      * @since 1.14
      */
-    protected byte[] buf = new byte[512];
+    protected byte[] buf = new byte[BUFFER_SIZE];
 
     // CheckStyle:VisibilityModifier ON
 

Modified: ant/core/trunk/src/main/org/apache/tools/zip/ZipShort.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/zip/ZipShort.java?rev=569089&r1=569088&r2=569089&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/zip/ZipShort.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/zip/ZipShort.java Thu Aug 23 10:58:32 2007
@@ -24,6 +24,9 @@
  *
  */
 public final class ZipShort implements Cloneable {
+    private static final int BYTE_MASK = 0xFF;
+    private static final int BYTE_1_MASK = 0xFF00;
+    private static final int BYTE_1_SHIFT = 8;
 
     private int value;
 
@@ -62,8 +65,8 @@
      */
     public byte[] getBytes() {
         byte[] result = new byte[2];
-        result[0] = (byte) (value & 0xFF);
-        result[1] = (byte) ((value & 0xFF00) >> 8);
+        result[0] = (byte) (value & BYTE_MASK);
+        result[1] = (byte) ((value & BYTE_1_MASK) >> BYTE_1_SHIFT);
         return result;
     }
 
@@ -83,8 +86,8 @@
      */
     public static byte[] getBytes(int value) {
         byte[] result = new byte[2];
-        result[0] = (byte) (value & 0xFF);
-        result[1] = (byte) ((value & 0xFF00) >> 8);
+        result[0] = (byte) (value & BYTE_MASK);
+        result[1] = (byte) ((value & BYTE_1_MASK) >> BYTE_1_SHIFT);
         return result;
     }
 
@@ -95,8 +98,8 @@
      * @return the correspondanding java int value
      */
     public static int getValue(byte[] bytes, int offset) {
-        int value = (bytes[offset + 1] << 8) & 0xFF00;
-        value += (bytes[offset] & 0xFF);
+        int value = (bytes[offset + 1] << BYTE_1_SHIFT) & BYTE_1_MASK;
+        value += (bytes[offset] & BYTE_MASK);
         return value;
     }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Mime
View raw message