Browse Source

re-use a bunch of temporary arrays - merge from Compress

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@1429370 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 12 years ago
parent
commit
98f306fa84
5 changed files with 55 additions and 61 deletions
  1. +8
    -6
      src/main/org/apache/tools/tar/TarInputStream.java
  2. +3
    -2
      src/main/org/apache/tools/zip/Zip64ExtendedInformationExtraField.java
  3. +2
    -1
      src/main/org/apache/tools/zip/ZipEntry.java
  4. +39
    -51
      src/main/org/apache/tools/zip/ZipFile.java
  5. +3
    -1
      src/main/org/apache/tools/zip/ZipOutputStream.java

+ 8
- 6
src/main/org/apache/tools/tar/TarInputStream.java View File

@@ -48,6 +48,9 @@ public class TarInputStream extends FilterInputStream {
private static final int LARGE_BUFFER_SIZE = 32 * 1024; private static final int LARGE_BUFFER_SIZE = 32 * 1024;
private static final int BYTE_MASK = 0xFF; private static final int BYTE_MASK = 0xFF;


private final byte[] SKIP_BUF = new byte[BUFFER_SIZE];
private final byte[] SMALL_BUF = new byte[SMALL_BUFFER_SIZE];

// CheckStyle:VisibilityModifier OFF - bc // CheckStyle:VisibilityModifier OFF - bc
protected boolean debug; protected boolean debug;
protected boolean hasHitEOF; protected boolean hasHitEOF;
@@ -196,11 +199,11 @@ public class TarInputStream extends FilterInputStream {
// This is horribly inefficient, but it ensures that we // This is horribly inefficient, but it ensures that we
// properly skip over bytes via the TarBuffer... // properly skip over bytes via the TarBuffer...
// //
byte[] skipBuf = new byte[BUFFER_SIZE];
long skip = numToSkip; long skip = numToSkip;
while (skip > 0) { while (skip > 0) {
int realSkip = (int) (skip > skipBuf.length ? skipBuf.length : skip);
int numRead = read(skipBuf, 0, realSkip);
int realSkip = (int) (skip > SKIP_BUF.length
? SKIP_BUF.length : skip);
int numRead = read(SKIP_BUF, 0, realSkip);
if (numRead == -1) { if (numRead == -1) {
break; break;
} }
@@ -303,10 +306,9 @@ public class TarInputStream extends FilterInputStream {
if (currEntry.isGNULongNameEntry()) { if (currEntry.isGNULongNameEntry()) {
// read in the name // read in the name
ByteArrayOutputStream longName = new ByteArrayOutputStream(); ByteArrayOutputStream longName = new ByteArrayOutputStream();
byte[] buf = new byte[SMALL_BUFFER_SIZE];
int length = 0; int length = 0;
while ((length = read(buf)) >= 0) {
longName.write(buf, 0, length);
while ((length = read(SMALL_BUF)) >= 0) {
longName.write(SMALL_BUF, 0, length);
} }
getNextEntry(); getNextEntry();
if (currEntry == null) { if (currEntry == null) {


+ 3
- 2
src/main/org/apache/tools/zip/Zip64ExtendedInformationExtraField.java View File

@@ -80,6 +80,7 @@ public class Zip64ExtendedInformationExtraField
private static final String LFH_MUST_HAVE_BOTH_SIZES_MSG = private static final String LFH_MUST_HAVE_BOTH_SIZES_MSG =
"Zip64 extended information must contain" "Zip64 extended information must contain"
+ " both size values in the local file header."; + " both size values in the local file header.";
private static final byte[] EMPTY = new byte[0];


private ZipEightByteInteger size, compressedSize, relativeHeaderOffset; private ZipEightByteInteger size, compressedSize, relativeHeaderOffset;
private ZipLong diskStart; private ZipLong diskStart;
@@ -159,7 +160,7 @@ public class Zip64ExtendedInformationExtraField
addSizes(data); addSizes(data);
return data; return data;
} }
return new byte[0];
return EMPTY;
} }


/** {@inheritDoc} */ /** {@inheritDoc} */
@@ -351,4 +352,4 @@ public class Zip64ExtendedInformationExtraField
} }
return off; return off;
} }
}
}

+ 2
- 1
src/main/org/apache/tools/zip/ZipEntry.java View File

@@ -53,6 +53,7 @@ public class ZipEntry extends java.util.zip.ZipEntry implements Cloneable {
public static final int PLATFORM_FAT = 0; public static final int PLATFORM_FAT = 0;
private static final int SHORT_MASK = 0xFFFF; private static final int SHORT_MASK = 0xFFFF;
private static final int SHORT_SHIFT = 16; private static final int SHORT_SHIFT = 16;
private static final byte[] EMPTY = new byte[0];


/** /**
* The {@link java.util.zip.ZipEntry} base class only supports * The {@link java.util.zip.ZipEntry} base class only supports
@@ -483,7 +484,7 @@ public class ZipEntry extends java.util.zip.ZipEntry implements Cloneable {
*/ */
public byte[] getLocalFileDataExtra() { public byte[] getLocalFileDataExtra() {
byte[] extra = getExtra(); byte[] extra = getExtra();
return extra != null ? extra : new byte[0];
return extra != null ? extra : EMPTY;
} }


/** /**


+ 39
- 51
src/main/org/apache/tools/zip/ZipFile.java View File

@@ -131,6 +131,12 @@ public class ZipFile {
*/ */
private boolean closed; private boolean closed;


// cached buffers
private final byte[] DWORD_BUF = new byte[DWORD];
private final byte[] WORD_BUF = new byte[WORD];
private final byte[] CFH_BUF = new byte[CFH_LEN];
private final byte[] SHORT_BUF = new byte[SHORT];

/** /**
* Opens the given file for reading, assuming the platform's * Opens the given file for reading, assuming the platform's
* native encoding for file names. * native encoding for file names.
@@ -405,9 +411,8 @@ public class ZipFile {


positionAtCentralDirectory(); positionAtCentralDirectory();


byte[] signatureBytes = new byte[WORD];
archive.readFully(signatureBytes);
long sig = ZipLong.getValue(signatureBytes);
archive.readFully(WORD_BUF);
long sig = ZipLong.getValue(WORD_BUF);


if (sig != CFH_SIG && startsWithLocalFileHeader()) { if (sig != CFH_SIG && startsWithLocalFileHeader()) {
throw new IOException("central directory is empty, can't expand" throw new IOException("central directory is empty, can't expand"
@@ -416,8 +421,8 @@ public class ZipFile {


while (sig == CFH_SIG) { while (sig == CFH_SIG) {
readCentralDirectoryEntry(noUTF8Flag); readCentralDirectoryEntry(noUTF8Flag);
archive.readFully(signatureBytes);
sig = ZipLong.getValue(signatureBytes);
archive.readFully(WORD_BUF);
sig = ZipLong.getValue(WORD_BUF);
} }
return noUTF8Flag; return noUTF8Flag;
} }
@@ -434,19 +439,17 @@ public class ZipFile {
private void private void
readCentralDirectoryEntry(Map<ZipEntry, NameAndComment> noUTF8Flag) readCentralDirectoryEntry(Map<ZipEntry, NameAndComment> noUTF8Flag)
throws IOException { throws IOException {
byte[] cfh = new byte[CFH_LEN];

archive.readFully(cfh);
archive.readFully(CFH_BUF);
int off = 0; int off = 0;
ZipEntry ze = new ZipEntry(); ZipEntry ze = new ZipEntry();


int versionMadeBy = ZipShort.getValue(cfh, off);
int versionMadeBy = ZipShort.getValue(CFH_BUF, off);
off += SHORT; off += SHORT;
ze.setPlatform((versionMadeBy >> BYTE_SHIFT) & NIBLET_MASK); ze.setPlatform((versionMadeBy >> BYTE_SHIFT) & NIBLET_MASK);


off += SHORT; // skip version info off += SHORT; // skip version info


final GeneralPurposeBit gpFlag = GeneralPurposeBit.parse(cfh, off);
final GeneralPurposeBit gpFlag = GeneralPurposeBit.parse(CFH_BUF, off);
final boolean hasUTF8Flag = gpFlag.usesUTF8ForNames(); final boolean hasUTF8Flag = gpFlag.usesUTF8ForNames();
final ZipEncoding entryEncoding = final ZipEncoding entryEncoding =
hasUTF8Flag ? ZipEncodingHelper.UTF8_ZIP_ENCODING : zipEncoding; hasUTF8Flag ? ZipEncodingHelper.UTF8_ZIP_ENCODING : zipEncoding;
@@ -454,38 +457,38 @@ public class ZipFile {


off += SHORT; off += SHORT;


ze.setMethod(ZipShort.getValue(cfh, off));
ze.setMethod(ZipShort.getValue(CFH_BUF, off));
off += SHORT; off += SHORT;


long time = ZipUtil.dosToJavaTime(ZipLong.getValue(cfh, off));
long time = ZipUtil.dosToJavaTime(ZipLong.getValue(CFH_BUF, off));
ze.setTime(time); ze.setTime(time);
off += WORD; off += WORD;


ze.setCrc(ZipLong.getValue(cfh, off));
ze.setCrc(ZipLong.getValue(CFH_BUF, off));
off += WORD; off += WORD;


ze.setCompressedSize(ZipLong.getValue(cfh, off));
ze.setCompressedSize(ZipLong.getValue(CFH_BUF, off));
off += WORD; off += WORD;


ze.setSize(ZipLong.getValue(cfh, off));
ze.setSize(ZipLong.getValue(CFH_BUF, off));
off += WORD; off += WORD;


int fileNameLen = ZipShort.getValue(cfh, off);
int fileNameLen = ZipShort.getValue(CFH_BUF, off);
off += SHORT; off += SHORT;


int extraLen = ZipShort.getValue(cfh, off);
int extraLen = ZipShort.getValue(CFH_BUF, off);
off += SHORT; off += SHORT;


int commentLen = ZipShort.getValue(cfh, off);
int commentLen = ZipShort.getValue(CFH_BUF, off);
off += SHORT; off += SHORT;


int diskStart = ZipShort.getValue(cfh, off);
int diskStart = ZipShort.getValue(CFH_BUF, off);
off += SHORT; off += SHORT;


ze.setInternalAttributes(ZipShort.getValue(cfh, off));
ze.setInternalAttributes(ZipShort.getValue(CFH_BUF, off));
off += SHORT; off += SHORT;


ze.setExternalAttributes(ZipLong.getValue(cfh, off));
ze.setExternalAttributes(ZipLong.getValue(CFH_BUF, off));
off += WORD; off += WORD;


byte[] fileName = new byte[fileNameLen]; byte[] fileName = new byte[fileNameLen];
@@ -494,7 +497,7 @@ public class ZipFile {


// LFH offset, // LFH offset,
OffsetEntry offset = new OffsetEntry(); OffsetEntry offset = new OffsetEntry();
offset.headerOffset = ZipLong.getValue(cfh, off);
offset.headerOffset = ZipLong.getValue(CFH_BUF, off);
// data offset will be filled later // data offset will be filled later
entries.put(ze, offset); entries.put(ze, offset);


@@ -684,24 +687,17 @@ public class ZipFile {
private void positionAtCentralDirectory64() private void positionAtCentralDirectory64()
throws IOException { throws IOException {
skipBytes(ZIP64_EOCDL_LOCATOR_OFFSET); skipBytes(ZIP64_EOCDL_LOCATOR_OFFSET);
byte[] zip64EocdOffset = new byte[DWORD];
archive.readFully(zip64EocdOffset);
archive.seek(ZipEightByteInteger.getLongValue(zip64EocdOffset));
byte[] sig = new byte[WORD];
archive.readFully(sig);
if (sig[POS_0] != ZipOutputStream.ZIP64_EOCD_SIG[POS_0]
|| sig[POS_1] != ZipOutputStream.ZIP64_EOCD_SIG[POS_1]
|| sig[POS_2] != ZipOutputStream.ZIP64_EOCD_SIG[POS_2]
|| sig[POS_3] != ZipOutputStream.ZIP64_EOCD_SIG[POS_3]
) {
archive.readFully(DWORD_BUF);
archive.seek(ZipEightByteInteger.getLongValue(DWORD_BUF));
archive.readFully(WORD_BUF);
if (!Arrays.equals(WORD_BUF, ZipOutputStream.ZIP64_EOCD_SIG)) {
throw new ZipException("archive's ZIP64 end of central " throw new ZipException("archive's ZIP64 end of central "
+ "directory locator is corrupt."); + "directory locator is corrupt.");
} }
skipBytes(ZIP64_EOCD_CFD_LOCATOR_OFFSET skipBytes(ZIP64_EOCD_CFD_LOCATOR_OFFSET
- WORD /* signature has already been read */); - WORD /* signature has already been read */);
byte[] cfdOffset = new byte[DWORD];
archive.readFully(cfdOffset);
archive.seek(ZipEightByteInteger.getLongValue(cfdOffset));
archive.readFully(DWORD_BUF);
archive.seek(ZipEightByteInteger.getLongValue(DWORD_BUF));
} }


/** /**
@@ -717,9 +713,8 @@ public class ZipFile {
throw new ZipException("archive is not a ZIP archive"); throw new ZipException("archive is not a ZIP archive");
} }
skipBytes(CFD_LOCATOR_OFFSET); skipBytes(CFD_LOCATOR_OFFSET);
byte[] cfdOffset = new byte[WORD];
archive.readFully(cfdOffset);
archive.seek(ZipLong.getValue(cfdOffset));
archive.readFully(WORD_BUF);
archive.seek(ZipLong.getValue(WORD_BUF));
} }


/** /**
@@ -814,11 +809,10 @@ public class ZipFile {
OffsetEntry offsetEntry = ent.getValue(); OffsetEntry offsetEntry = ent.getValue();
long offset = offsetEntry.headerOffset; long offset = offsetEntry.headerOffset;
archive.seek(offset + LFH_OFFSET_FOR_FILENAME_LENGTH); archive.seek(offset + LFH_OFFSET_FOR_FILENAME_LENGTH);
byte[] b = new byte[SHORT];
archive.readFully(b);
int fileNameLen = ZipShort.getValue(b);
archive.readFully(b);
int extraFieldLen = ZipShort.getValue(b);
archive.readFully(SHORT_BUF);
int fileNameLen = ZipShort.getValue(SHORT_BUF);
archive.readFully(SHORT_BUF);
int extraFieldLen = ZipShort.getValue(SHORT_BUF);
int lenToSkip = fileNameLen; int lenToSkip = fileNameLen;
while (lenToSkip > 0) { while (lenToSkip > 0) {
int skipped = archive.skipBytes(lenToSkip); int skipped = archive.skipBytes(lenToSkip);
@@ -854,14 +848,8 @@ public class ZipFile {
*/ */
private boolean startsWithLocalFileHeader() throws IOException { private boolean startsWithLocalFileHeader() throws IOException {
archive.seek(0); archive.seek(0);
final byte[] start = new byte[WORD];
archive.readFully(start);
for (int i = 0; i < start.length; i++) {
if (start[i] != ZipOutputStream.LFH_SIG[i]) {
return false;
}
}
return true;
archive.readFully(WORD_BUF);
return Arrays.equals(WORD_BUF, ZipOutputStream.LFH_SIG);
} }


/** /**


+ 3
- 1
src/main/org/apache/tools/zip/ZipOutputStream.java View File

@@ -121,6 +121,8 @@ public class ZipOutputStream extends FilterOutputStream {
@Deprecated @Deprecated
public static final int EFS_FLAG = GeneralPurposeBit.UFT8_NAMES_FLAG; public static final int EFS_FLAG = GeneralPurposeBit.UFT8_NAMES_FLAG;


private static final byte[] EMPTY = new byte[0];

/** /**
* Current entry. * Current entry.
* *
@@ -488,7 +490,7 @@ public class ZipOutputStream extends FilterOutputStream {
} }


if (!entry.hasWritten) { if (!entry.hasWritten) {
write(new byte[0], 0, 0);
write(EMPTY, 0, 0);
} }


flushDeflater(); flushDeflater();


Loading…
Cancel
Save