git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@569089 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -29,6 +29,8 @@ import java.util.zip.ZipException; | |||||
| // CheckStyle:HideUtilityClassConstructorCheck OFF (bc) | // CheckStyle:HideUtilityClassConstructorCheck OFF (bc) | ||||
| public class ExtraFieldUtils { | public class ExtraFieldUtils { | ||||
| private static final int WORD = 4; | |||||
| /** | /** | ||||
| * Static registry of known extra fields. | * Static registry of known extra fields. | ||||
| * | * | ||||
| @@ -95,23 +97,23 @@ public class ExtraFieldUtils { | |||||
| public static ZipExtraField[] parse(byte[] data) throws ZipException { | public static ZipExtraField[] parse(byte[] data) throws ZipException { | ||||
| Vector v = new Vector(); | Vector v = new Vector(); | ||||
| int start = 0; | int start = 0; | ||||
| while (start <= data.length - 4) { | |||||
| while (start <= data.length - WORD) { | |||||
| ZipShort headerId = new ZipShort(data, start); | ZipShort headerId = new ZipShort(data, start); | ||||
| int length = (new ZipShort(data, start + 2)).getValue(); | 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 | throw new ZipException("data starting at " + start | ||||
| + " is in unknown format"); | + " is in unknown format"); | ||||
| } | } | ||||
| try { | try { | ||||
| ZipExtraField ze = createExtraField(headerId); | ZipExtraField ze = createExtraField(headerId); | ||||
| ze.parseFromLocalFileData(data, start + 4, length); | |||||
| ze.parseFromLocalFileData(data, start + WORD, length); | |||||
| v.addElement(ze); | v.addElement(ze); | ||||
| } catch (InstantiationException ie) { | } catch (InstantiationException ie) { | ||||
| throw new ZipException(ie.getMessage()); | throw new ZipException(ie.getMessage()); | ||||
| } catch (IllegalAccessException iae) { | } catch (IllegalAccessException iae) { | ||||
| throw new ZipException(iae.getMessage()); | throw new ZipException(iae.getMessage()); | ||||
| } | } | ||||
| start += (length + 4); | |||||
| start += (length + WORD); | |||||
| } | } | ||||
| if (start != data.length) { // array not exhausted | if (start != data.length) { // array not exhausted | ||||
| throw new ZipException("data starting at " + start | throw new ZipException("data starting at " + start | ||||
| @@ -130,7 +132,7 @@ public class ExtraFieldUtils { | |||||
| * @since 1.1 | * @since 1.1 | ||||
| */ | */ | ||||
| public static byte[] mergeLocalFileDataData(ZipExtraField[] data) { | public static byte[] mergeLocalFileDataData(ZipExtraField[] data) { | ||||
| int sum = 4 * data.length; | |||||
| int sum = WORD * data.length; | |||||
| for (int i = 0; i < data.length; i++) { | for (int i = 0; i < data.length; i++) { | ||||
| sum += data[i].getLocalFileDataLength().getValue(); | sum += data[i].getLocalFileDataLength().getValue(); | ||||
| } | } | ||||
| @@ -142,8 +144,8 @@ public class ExtraFieldUtils { | |||||
| System.arraycopy(data[i].getLocalFileDataLength().getBytes(), | System.arraycopy(data[i].getLocalFileDataLength().getBytes(), | ||||
| 0, result, start + 2, 2); | 0, result, start + 2, 2); | ||||
| byte[] local = data[i].getLocalFileDataData(); | 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; | return result; | ||||
| } | } | ||||
| @@ -155,7 +157,7 @@ public class ExtraFieldUtils { | |||||
| * @since 1.1 | * @since 1.1 | ||||
| */ | */ | ||||
| public static byte[] mergeCentralDirectoryData(ZipExtraField[] data) { | public static byte[] mergeCentralDirectoryData(ZipExtraField[] data) { | ||||
| int sum = 4 * data.length; | |||||
| int sum = WORD * data.length; | |||||
| for (int i = 0; i < data.length; i++) { | for (int i = 0; i < data.length; i++) { | ||||
| sum += data[i].getCentralDirectoryLength().getValue(); | sum += data[i].getCentralDirectoryLength().getValue(); | ||||
| } | } | ||||
| @@ -167,8 +169,8 @@ public class ExtraFieldUtils { | |||||
| System.arraycopy(data[i].getCentralDirectoryLength().getBytes(), | System.arraycopy(data[i].getCentralDirectoryLength().getBytes(), | ||||
| 0, result, start + 2, 2); | 0, result, start + 2, 2); | ||||
| byte[] local = data[i].getCentralDirectoryData(); | 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; | return result; | ||||
| } | } | ||||
| @@ -30,6 +30,8 @@ public class ZipEntry extends java.util.zip.ZipEntry implements Cloneable { | |||||
| private static final int PLATFORM_UNIX = 3; | private static final int PLATFORM_UNIX = 3; | ||||
| private static final int PLATFORM_FAT = 0; | 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 internalAttributes = 0; | ||||
| private int platform = PLATFORM_FAT; | private int platform = PLATFORM_FAT; | ||||
| @@ -142,11 +144,13 @@ public class ZipEntry extends java.util.zip.ZipEntry implements Cloneable { | |||||
| * @since Ant 1.5.2 | * @since Ant 1.5.2 | ||||
| */ | */ | ||||
| public void setUnixMode(int mode) { | public void setUnixMode(int mode) { | ||||
| // CheckStyle:MagicNumberCheck OFF - no point | |||||
| setExternalAttributes((mode << 16) | setExternalAttributes((mode << 16) | ||||
| // MS-DOS read-only attribute | // MS-DOS read-only attribute | ||||
| | ((mode & 0200) == 0 ? 1 : 0) | | ((mode & 0200) == 0 ? 1 : 0) | ||||
| // MS-DOS directory flag | // MS-DOS directory flag | ||||
| | (isDirectory() ? 0x10 : 0)); | | (isDirectory() ? 0x10 : 0)); | ||||
| // CheckStyle:MagicNumberCheck ON | |||||
| platform = PLATFORM_UNIX; | platform = PLATFORM_UNIX; | ||||
| } | } | ||||
| @@ -156,7 +160,7 @@ public class ZipEntry extends java.util.zip.ZipEntry implements Cloneable { | |||||
| * @since Ant 1.6 | * @since Ant 1.6 | ||||
| */ | */ | ||||
| public int getUnixMode() { | public int getUnixMode() { | ||||
| return (int) ((getExternalAttributes() >> 16) & 0xFFFF); | |||||
| return (int) ((getExternalAttributes() >> SHORT_SHIFT) & SHORT_MASK); | |||||
| } | } | ||||
| /** | /** | ||||
| @@ -62,6 +62,12 @@ public class ZipFile { | |||||
| private static final int HASH_SIZE = 509; | private static final int HASH_SIZE = 509; | ||||
| private static final int SHORT = 2; | private static final int SHORT = 2; | ||||
| private static final int WORD = 4; | 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 | * Maps ZipEntrys to Longs, recording the offsets of the local | ||||
| @@ -277,7 +283,7 @@ public class ZipFile { | |||||
| int versionMadeBy = ZipShort.getValue(cfh, off); | int versionMadeBy = ZipShort.getValue(cfh, off); | ||||
| off += SHORT; | off += SHORT; | ||||
| ze.setPlatform((versionMadeBy >> 8) & 0x0F); | |||||
| ze.setPlatform((versionMadeBy >> BYTE_SHIFT) & NIBLET_MASK); | |||||
| off += WORD; // skip version info and general purpose byte | off += WORD; // skip version info and general purpose byte | ||||
| @@ -381,13 +387,13 @@ public class ZipFile { | |||||
| byte[] sig = ZipOutputStream.EOCD_SIG; | byte[] sig = ZipOutputStream.EOCD_SIG; | ||||
| int curr = archive.read(); | int curr = archive.read(); | ||||
| while (curr != -1) { | while (curr != -1) { | ||||
| if (curr == sig[0]) { | |||||
| if (curr == sig[POS_0]) { | |||||
| curr = archive.read(); | curr = archive.read(); | ||||
| if (curr == sig[1]) { | |||||
| if (curr == sig[POS_1]) { | |||||
| curr = archive.read(); | curr = archive.read(); | ||||
| if (curr == sig[SHORT]) { | |||||
| if (curr == sig[POS_2]) { | |||||
| curr = archive.read(); | curr = archive.read(); | ||||
| if (curr == sig[3]) { | |||||
| if (curr == sig[POS_3]) { | |||||
| found = true; | found = true; | ||||
| break; | break; | ||||
| } | } | ||||
| @@ -471,12 +477,14 @@ public class ZipFile { | |||||
| */ | */ | ||||
| private static long dosToJavaTime(long dosTime) { | private static long dosToJavaTime(long dosTime) { | ||||
| Calendar cal = Calendar.getInstance(); | Calendar cal = Calendar.getInstance(); | ||||
| // CheckStyle:MagicNumberCheck OFF - no point | |||||
| cal.set(Calendar.YEAR, (int) ((dosTime >> 25) & 0x7f) + 1980); | cal.set(Calendar.YEAR, (int) ((dosTime >> 25) & 0x7f) + 1980); | ||||
| cal.set(Calendar.MONTH, (int) ((dosTime >> 21) & 0x0f) - 1); | cal.set(Calendar.MONTH, (int) ((dosTime >> 21) & 0x0f) - 1); | ||||
| cal.set(Calendar.DATE, (int) (dosTime >> 16) & 0x1f); | cal.set(Calendar.DATE, (int) (dosTime >> 16) & 0x1f); | ||||
| cal.set(Calendar.HOUR_OF_DAY, (int) (dosTime >> 11) & 0x1f); | cal.set(Calendar.HOUR_OF_DAY, (int) (dosTime >> 11) & 0x1f); | ||||
| cal.set(Calendar.MINUTE, (int) (dosTime >> 5) & 0x3f); | cal.set(Calendar.MINUTE, (int) (dosTime >> 5) & 0x3f); | ||||
| cal.set(Calendar.SECOND, (int) (dosTime << 1) & 0x3e); | cal.set(Calendar.SECOND, (int) (dosTime << 1) & 0x3e); | ||||
| // CheckStyle:MagicNumberCheck ON | |||||
| return cal.getTime().getTime(); | return cal.getTime().getTime(); | ||||
| } | } | ||||
| @@ -25,6 +25,22 @@ package org.apache.tools.zip; | |||||
| */ | */ | ||||
| public final class ZipLong implements Cloneable { | 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; | private long value; | ||||
| /** | /** | ||||
| @@ -79,11 +95,11 @@ public final class ZipLong implements Cloneable { | |||||
| * @return value as four bytes in big endian byte order | * @return value as four bytes in big endian byte order | ||||
| */ | */ | ||||
| public static byte[] getBytes(long value) { | 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; | return result; | ||||
| } | } | ||||
| @@ -94,10 +110,10 @@ public final class ZipLong implements Cloneable { | |||||
| * @return the correspondanding Java long value | * @return the correspondanding Java long value | ||||
| */ | */ | ||||
| public static long getValue(byte[] bytes, int offset) { | 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; | return value; | ||||
| } | } | ||||
| @@ -57,6 +57,7 @@ public class ZipOutputStream extends FilterOutputStream { | |||||
| private static final int BYTE_MASK = 0xFF; | private static final int BYTE_MASK = 0xFF; | ||||
| private static final int SHORT = 2; | private static final int SHORT = 2; | ||||
| private static final int WORD = 4; | private static final int WORD = 4; | ||||
| private static final int BUFFER_SIZE = 512; | |||||
| /** | /** | ||||
| * Compression method for deflated entries. | * Compression method for deflated entries. | ||||
| @@ -221,7 +222,7 @@ public class ZipOutputStream extends FilterOutputStream { | |||||
| * | * | ||||
| * @since 1.14 | * @since 1.14 | ||||
| */ | */ | ||||
| protected byte[] buf = new byte[512]; | |||||
| protected byte[] buf = new byte[BUFFER_SIZE]; | |||||
| // CheckStyle:VisibilityModifier ON | // CheckStyle:VisibilityModifier ON | ||||
| @@ -24,6 +24,9 @@ package org.apache.tools.zip; | |||||
| * | * | ||||
| */ | */ | ||||
| public final class ZipShort implements Cloneable { | 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; | private int value; | ||||
| @@ -62,8 +65,8 @@ public final class ZipShort implements Cloneable { | |||||
| */ | */ | ||||
| public byte[] getBytes() { | public byte[] getBytes() { | ||||
| byte[] result = new byte[2]; | 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; | return result; | ||||
| } | } | ||||
| @@ -83,8 +86,8 @@ public final class ZipShort implements Cloneable { | |||||
| */ | */ | ||||
| public static byte[] getBytes(int value) { | public static byte[] getBytes(int value) { | ||||
| byte[] result = new byte[2]; | 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; | return result; | ||||
| } | } | ||||
| @@ -95,8 +98,8 @@ public final class ZipShort implements Cloneable { | |||||
| * @return the correspondanding java int value | * @return the correspondanding java int value | ||||
| */ | */ | ||||
| public static int getValue(byte[] bytes, int offset) { | 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; | return value; | ||||
| } | } | ||||