commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r1429363 - in /commons/proper/compress/trunk/src: changes/ main/java/org/apache/commons/compress/archivers/ar/ main/java/org/apache/commons/compress/archivers/cpio/ main/java/org/apache/commons/compress/archivers/tar/ main/java/org/apache/c...
Date Sat, 05 Jan 2013 19:28:42 GMT
Author: bodewig
Date: Sat Jan  5 19:28:42 2013
New Revision: 1429363

URL: http://svn.apache.org/viewvc?rev=1429363&view=rev
Log:
COMPRESS-172 reuse temporary byte-arrays in several places to reduce garbage collection pressure
- inspired by a patch by Thomas Mair

Modified:
    commons/proper/compress/trunk/src/changes/changes.xml
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/Zip64ExtendedInformationExtraField.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/gzip/GzipCompressorInputStream.java

Modified: commons/proper/compress/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/changes/changes.xml?rev=1429363&r1=1429362&r2=1429363&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/changes/changes.xml (original)
+++ commons/proper/compress/trunk/src/changes/changes.xml Sat Jan  5 19:28:42 2013
@@ -107,6 +107,11 @@ The <action> type attribute can be add,u
         TarArchiveInputStream ignored the encoding for GNU long name
         entries.
       </action>
+      <action type="update" date="2013-01-05" issue="COMPRESS-172"
+              due-to="Thomas Mair">
+        Garbage collection pressure has been reduced by reusing
+        temporary byte arrays in classes.
+      </action>
     </release>
     <release version="1.4.1" date="2012-05-23"
              description="Release 1.4.1">

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java?rev=1429363&r1=1429362&r2=1429363&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java
Sat Jan  5 19:28:42 2013
@@ -53,6 +53,13 @@ public class ArArchiveInputStream extend
      */
     private long entryOffset = -1;
 
+    // cached buffers
+    private final byte[] NAME_BUF = new byte[16];
+    private final byte[] LAST_MODIFIED_BUF = new byte[12];
+    private final byte[] ID_BUF = new byte[6];
+    private final byte[] FILE_MODE_BUF = new byte[8];
+    private final byte[] LENGTH_BUF = new byte[10];
+
     /**
      * Constructs an Ar input stream with the referenced stream
      * 
@@ -108,19 +115,13 @@ public class ArArchiveInputStream extend
             return null;
         }
 
-        final byte[] name = new byte[16];
-        final byte[] lastmodified = new byte[12];
-        final byte[] userid = new byte[6];
-        final byte[] groupid = new byte[6];
-        final byte[] filemode = new byte[8];
-        final byte[] length = new byte[10];
-
-        read(name);
-        read(lastmodified);
-        read(userid);
-        read(groupid);
-        read(filemode);
-        read(length);
+        read(NAME_BUF);
+        read(LAST_MODIFIED_BUF);
+        read(ID_BUF);
+        int userId = asInt(ID_BUF, true);
+        read(ID_BUF);
+        read(FILE_MODE_BUF);
+        read(LENGTH_BUF);
 
         {
             final byte[] expected = ArchiveUtils.toAsciiBytes(ArArchiveEntry.TRAILER);
@@ -141,13 +142,14 @@ public class ArArchiveInputStream extend
 //        GNU ar uses a '/' to mark the end of the filename; this allows for the use of spaces
without the use of an extended filename.
 
         // entry name is stored as ASCII string
-        String temp = ArchiveUtils.toAsciiString(name).trim();
-        long len = asLong(length);
-
+        String temp = ArchiveUtils.toAsciiString(NAME_BUF).trim();
         if (isGNUStringTable(temp)) { // GNU extended filenames entry
-            currentEntry = readGNUStringTable(length);
+            currentEntry = readGNUStringTable(LENGTH_BUF);
             return getNextArEntry();
-        } else if (temp.endsWith("/")) { // GNU terminator
+        }
+
+        long len = asLong(LENGTH_BUF);
+        if (temp.endsWith("/")) { // GNU terminator
             temp = temp.substring(0, temp.length() - 1);
         } else if (isGNULongName(temp)) {
             int offset = Integer.parseInt(temp.substring(1));// get the offset
@@ -162,9 +164,10 @@ public class ArArchiveInputStream extend
             entryOffset += nameLen;
         }
 
-        currentEntry = new ArArchiveEntry(temp, len, asInt(userid, true),
-                                          asInt(groupid, true), asInt(filemode, 8),
-                                          asLong(lastmodified));
+        currentEntry = new ArArchiveEntry(temp, len, userId,
+                                          asInt(ID_BUF, true),
+                                          asInt(FILE_MODE_BUF, 8),
+                                          asLong(LAST_MODIFIED_BUF));
         return currentEntry;
     }
 

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java?rev=1429363&r1=1429362&r2=1429363&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java
Sat Jan  5 19:28:42 2013
@@ -77,6 +77,11 @@ public class CpioArchiveInputStream exte
 
     private final InputStream in;
 
+    // cached buffers
+    private final byte[] TWO_BYTES_BUF = new byte[2];
+    private final byte[] FOUR_BYTES_BUF = new byte[4];
+    private final byte[] SIX_BYTES_BUF = new byte[6];
+
     /**
      * Construct the cpio input stream
      * 
@@ -165,20 +170,18 @@ public class CpioArchiveInputStream exte
         if (this.entry != null) {
             closeEntry();
         }
-        byte magic[] = new byte[2];
-        readFully(magic, 0, magic.length);
-        if (CpioUtil.byteArray2long(magic, false) == MAGIC_OLD_BINARY) {
+        readFully(TWO_BYTES_BUF, 0, TWO_BYTES_BUF.length);
+        if (CpioUtil.byteArray2long(TWO_BYTES_BUF, false) == MAGIC_OLD_BINARY) {
             this.entry = readOldBinaryEntry(false);
-        } else if (CpioUtil.byteArray2long(magic, true) == MAGIC_OLD_BINARY) {
+        } else if (CpioUtil.byteArray2long(TWO_BYTES_BUF, true)
+                   == MAGIC_OLD_BINARY) {
             this.entry = readOldBinaryEntry(true);
         } else {
-            byte more_magic[] = new byte[4];
-            readFully(more_magic, 0, more_magic.length);
-            byte tmp[] = new byte[6];
-            System.arraycopy(magic, 0, tmp, 0, magic.length);
-            System.arraycopy(more_magic, 0, tmp, magic.length,
-                    more_magic.length);
-            String magicString = ArchiveUtils.toAsciiString(tmp);
+            System.arraycopy(TWO_BYTES_BUF, 0, SIX_BYTES_BUF, 0,
+                             TWO_BYTES_BUF.length);
+            readFully(SIX_BYTES_BUF, TWO_BYTES_BUF.length,
+                      FOUR_BYTES_BUF.length);
+            String magicString = ArchiveUtils.toAsciiString(SIX_BYTES_BUF);
             if (magicString.equals(MAGIC_NEW)) {
                 this.entry = readNewEntry(false);
             } else if (magicString.equals(MAGIC_NEW_CRC)) {
@@ -202,9 +205,9 @@ public class CpioArchiveInputStream exte
     }
 
     private void skip(int bytes) throws IOException{
-        final byte[] buff = new byte[4]; // Cannot be more than 3 bytes
+        // bytes cannot be more than 3 bytes
         if (bytes > 0) {
-            readFully(buff, 0, bytes);
+            readFully(FOUR_BYTES_BUF, 0, bytes);
         }
     }
 

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java?rev=1429363&r1=1429362&r2=1429363&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
Sat Jan  5 19:28:42 2013
@@ -48,6 +48,9 @@ public class TarArchiveInputStream exten
     private static final int SMALL_BUFFER_SIZE = 256;
     private static final int BUFFER_SIZE = 8 * 1024;
 
+    private final byte[] SKIP_BUF = new byte[BUFFER_SIZE];
+    private final byte[] SMALL_BUF = new byte[SMALL_BUFFER_SIZE];
+
     private boolean hasHitEOF;
     private long entrySize;
     private long entryOffset;
@@ -175,11 +178,11 @@ public class TarArchiveInputStream exten
         // This is horribly inefficient, but it ensures that we
         // properly skip over bytes via the TarBuffer...
         //
-        byte[] skipBuf = new byte[BUFFER_SIZE];
         long skip = numToSkip;
         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) {
                 break;
             }
@@ -248,10 +251,9 @@ public class TarArchiveInputStream exten
         if (currEntry.isGNULongNameEntry()) {
             // read in the name
             ByteArrayOutputStream longName = new ByteArrayOutputStream();
-            byte[] buf = new byte[SMALL_BUFFER_SIZE];
             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();
             if (currEntry == null) {

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/Zip64ExtendedInformationExtraField.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/Zip64ExtendedInformationExtraField.java?rev=1429363&r1=1429362&r2=1429363&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/Zip64ExtendedInformationExtraField.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/Zip64ExtendedInformationExtraField.java
Sat Jan  5 19:28:42 2013
@@ -80,6 +80,7 @@ public class Zip64ExtendedInformationExt
     private static final String LFH_MUST_HAVE_BOTH_SIZES_MSG =
         "Zip64 extended information must contain"
         + " both size values in the local file header.";
+    private static final byte[] EMPTY = new byte[0];
 
     private ZipEightByteInteger size, compressedSize, relativeHeaderOffset;
     private ZipLong diskStart;
@@ -161,7 +162,7 @@ public class Zip64ExtendedInformationExt
             addSizes(data);
             return data;
         }
-        return new byte[0];
+        return EMPTY;
     }
 
     /** {@inheritDoc} */
@@ -353,4 +354,4 @@ public class Zip64ExtendedInformationExt
         }
         return off;
     }
-}
\ No newline at end of file
+}

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java?rev=1429363&r1=1429362&r2=1429363&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java
Sat Jan  5 19:28:42 2013
@@ -55,6 +55,7 @@ public class ZipArchiveEntry extends jav
     public static final int PLATFORM_FAT  = 0;
     private static final int SHORT_MASK = 0xFFFF;
     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
@@ -475,7 +476,7 @@ public class ZipArchiveEntry extends jav
      */
     public byte[] getLocalFileDataExtra() {
         byte[] extra = getExtra();
-        return extra != null ? extra : new byte[0];
+        return extra != null ? extra : EMPTY;
     }
 
     /**

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java?rev=1429363&r1=1429362&r2=1429363&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
Sat Jan  5 19:28:42 2013
@@ -128,6 +128,12 @@ public class ZipArchiveInputStream exten
 
     private static final long TWO_EXP_32 = ZIP64_MAGIC + 1;
 
+    // cached buffers
+    private final byte[] LFH_BUF = new byte[LFH_LEN];
+    private final byte[] SKIP_BUF = new byte[1024];
+    private final byte[] WORD_BUF = new byte[WORD];
+    private final byte[] TWO_DWORD_BUF = new byte[2 * DWORD];
+
     public ZipArchiveInputStream(InputStream inputStream) {
         this(inputStream, ZipEncodingHelper.UTF8);
     }
@@ -183,22 +189,21 @@ public class ZipArchiveInputStream exten
             firstEntry = false;
         }
 
-        byte[] lfh = new byte[LFH_LEN];
         try {
             if (firstEntry) {
                 // split archives have a special signature before the
                 // first local file header - look for it and fail with
                 // the appropriate error message if this is a split
                 // archive.
-                readFirstLocalFileHeader(lfh);
+                readFirstLocalFileHeader(LFH_BUF);
             } else {
-                readFully(lfh);
+                readFully(LFH_BUF);
             }
         } catch (EOFException e) {
             return null;
         }
             
-        ZipLong sig = new ZipLong(lfh);
+        ZipLong sig = new ZipLong(LFH_BUF);
         if (sig.equals(ZipLong.CFH_SIG)) {
             hitCentralDirectory = true;
             return null;
@@ -210,12 +215,12 @@ public class ZipArchiveInputStream exten
         int off = WORD;
         current = new CurrentEntry();
 
-        int versionMadeBy = ZipShort.getValue(lfh, off);
+        int versionMadeBy = ZipShort.getValue(LFH_BUF, off);
         off += SHORT;
         current.entry.setPlatform((versionMadeBy >> ZipFile.BYTE_SHIFT)
                                   & ZipFile.NIBLET_MASK);
 
-        final GeneralPurposeBit gpFlag = GeneralPurposeBit.parse(lfh, off);
+        final GeneralPurposeBit gpFlag = GeneralPurposeBit.parse(LFH_BUF, off);
         final boolean hasUTF8Flag = gpFlag.usesUTF8ForNames();
         final ZipEncoding entryEncoding =
             hasUTF8Flag ? ZipEncodingHelper.UTF8_ZIP_ENCODING : zipEncoding;
@@ -224,32 +229,32 @@ public class ZipArchiveInputStream exten
 
         off += SHORT;
 
-        current.entry.setMethod(ZipShort.getValue(lfh, off));
+        current.entry.setMethod(ZipShort.getValue(LFH_BUF, off));
         off += SHORT;
 
-        long time = ZipUtil.dosToJavaTime(ZipLong.getValue(lfh, off));
+        long time = ZipUtil.dosToJavaTime(ZipLong.getValue(LFH_BUF, off));
         current.entry.setTime(time);
         off += WORD;
 
         ZipLong size = null, cSize = null;
         if (!current.hasDataDescriptor) {
-            current.entry.setCrc(ZipLong.getValue(lfh, off));
+            current.entry.setCrc(ZipLong.getValue(LFH_BUF, off));
             off += WORD;
 
-            cSize = new ZipLong(lfh, off);
+            cSize = new ZipLong(LFH_BUF, off);
             off += WORD;
 
-            size = new ZipLong(lfh, off);
+            size = new ZipLong(LFH_BUF, off);
             off += WORD;
         } else {
             off += 3 * WORD;
         }
 
-        int fileNameLen = ZipShort.getValue(lfh, off);
+        int fileNameLen = ZipShort.getValue(LFH_BUF, off);
 
         off += SHORT;
 
-        int extraLen = ZipShort.getValue(lfh, off);
+        int extraLen = ZipShort.getValue(LFH_BUF, off);
         off += SHORT;
 
         byte[] fileName = new byte[fileNameLen];
@@ -484,10 +489,11 @@ public class ZipArchiveInputStream exten
     public long skip(long value) throws IOException {
         if (value >= 0) {
             long skipped = 0;
-            byte[] b = new byte[1024];
             while (skipped < value) {
                 long rem = value - skipped;
-                int x = read(b, 0, (int) (b.length > rem ? rem : b.length));
+                int x = read(SKIP_BUF, 0,
+                             (int) (SKIP_BUF.length > rem ? rem
+                                    : SKIP_BUF.length));
                 if (x == -1) {
                     return skipped;
                 }
@@ -655,13 +661,12 @@ public class ZipArchiveInputStream exten
     }
 
     private void readDataDescriptor() throws IOException {
-        byte[] b = new byte[WORD];
-        readFully(b);
-        ZipLong val = new ZipLong(b);
+        readFully(WORD_BUF);
+        ZipLong val = new ZipLong(WORD_BUF);
         if (ZipLong.DD_SIG.equals(val)) {
             // data descriptor with signature, skip sig
-            readFully(b);
-            val = new ZipLong(b);
+            readFully(WORD_BUF);
+            val = new ZipLong(WORD_BUF);
         }
         current.entry.setCrc(val.getValue());
 
@@ -676,18 +681,19 @@ public class ZipArchiveInputStream exten
         // descriptor (ignoring archive decryption headers for now).
         // If so, push back eight bytes and assume sizes are four
         // bytes, otherwise sizes are eight bytes each.
-        b = new byte[2 * DWORD];
-        readFully(b);
-        ZipLong potentialSig = new ZipLong(b, DWORD);
+        readFully(TWO_DWORD_BUF);
+        ZipLong potentialSig = new ZipLong(TWO_DWORD_BUF, DWORD);
         if (potentialSig.equals(ZipLong.CFH_SIG)
             || potentialSig.equals(ZipLong.LFH_SIG)) {
-            pushback(b, DWORD, DWORD);
-            current.entry.setCompressedSize(ZipLong.getValue(b));
-            current.entry.setSize(ZipLong.getValue(b, WORD));
+            pushback(TWO_DWORD_BUF, DWORD, DWORD);
+            current.entry.setCompressedSize(ZipLong.getValue(TWO_DWORD_BUF));
+            current.entry.setSize(ZipLong.getValue(TWO_DWORD_BUF, WORD));
         } else {
             current.entry
-                .setCompressedSize(ZipEightByteInteger.getLongValue(b));
-            current.entry.setSize(ZipEightByteInteger.getLongValue(b, DWORD));
+                .setCompressedSize(ZipEightByteInteger
+                                   .getLongValue(TWO_DWORD_BUF));
+            current.entry.setSize(ZipEightByteInteger
+                                  .getLongValue(TWO_DWORD_BUF, DWORD));
         }
     }
 

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java?rev=1429363&r1=1429362&r2=1429363&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java
Sat Jan  5 19:28:42 2013
@@ -115,6 +115,8 @@ public class ZipArchiveOutputStream exte
     @Deprecated
     public static final int EFS_FLAG = GeneralPurposeBit.UFT8_NAMES_FLAG;
 
+    private static final byte[] EMPTY = new byte[0];
+
     /**
      * Current entry.
      */
@@ -444,7 +446,7 @@ public class ZipArchiveOutputStream exte
         }
 
         if (!entry.hasWritten) {
-            write(new byte[0], 0, 0);
+            write(EMPTY, 0, 0);
         }
 
         flushDeflater();

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java?rev=1429363&r1=1429362&r2=1429363&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
Sat Jan  5 19:28:42 2013
@@ -132,6 +132,12 @@ public class ZipFile {
      */
     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 "UTF8" for file names.
      *
@@ -405,9 +411,8 @@ public class ZipFile {
 
         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()) {
             throw new IOException("central directory is empty, can't expand"
@@ -416,8 +421,8 @@ public class ZipFile {
 
         while (sig == CFH_SIG) {
             readCentralDirectoryEntry(noUTF8Flag);
-            archive.readFully(signatureBytes);
-            sig = ZipLong.getValue(signatureBytes);
+            archive.readFully(WORD_BUF);
+            sig = ZipLong.getValue(WORD_BUF);
         }
         return noUTF8Flag;
     }
@@ -434,19 +439,17 @@ public class ZipFile {
     private void
         readCentralDirectoryEntry(Map<ZipArchiveEntry, NameAndComment> noUTF8Flag)
         throws IOException {
-        byte[] cfh = new byte[CFH_LEN];
-
-        archive.readFully(cfh);
+        archive.readFully(CFH_BUF);
         int off = 0;
         ZipArchiveEntry ze = new ZipArchiveEntry();
 
-        int versionMadeBy = ZipShort.getValue(cfh, off);
+        int versionMadeBy = ZipShort.getValue(CFH_BUF, off);
         off += SHORT;
         ze.setPlatform((versionMadeBy >> BYTE_SHIFT) & NIBLET_MASK);
 
         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 ZipEncoding entryEncoding =
             hasUTF8Flag ? ZipEncodingHelper.UTF8_ZIP_ENCODING : zipEncoding;
@@ -454,38 +457,38 @@ public class ZipFile {
 
         off += SHORT;
 
-        ze.setMethod(ZipShort.getValue(cfh, off));
+        ze.setMethod(ZipShort.getValue(CFH_BUF, off));
         off += SHORT;
 
-        long time = ZipUtil.dosToJavaTime(ZipLong.getValue(cfh, off));
+        long time = ZipUtil.dosToJavaTime(ZipLong.getValue(CFH_BUF, off));
         ze.setTime(time);
         off += WORD;
 
-        ze.setCrc(ZipLong.getValue(cfh, off));
+        ze.setCrc(ZipLong.getValue(CFH_BUF, off));
         off += WORD;
 
-        ze.setCompressedSize(ZipLong.getValue(cfh, off));
+        ze.setCompressedSize(ZipLong.getValue(CFH_BUF, off));
         off += WORD;
 
-        ze.setSize(ZipLong.getValue(cfh, off));
+        ze.setSize(ZipLong.getValue(CFH_BUF, off));
         off += WORD;
 
-        int fileNameLen = ZipShort.getValue(cfh, off);
+        int fileNameLen = ZipShort.getValue(CFH_BUF, off);
         off += SHORT;
 
-        int extraLen = ZipShort.getValue(cfh, off);
+        int extraLen = ZipShort.getValue(CFH_BUF, off);
         off += SHORT;
 
-        int commentLen = ZipShort.getValue(cfh, off);
+        int commentLen = ZipShort.getValue(CFH_BUF, off);
         off += SHORT;
 
-        int diskStart = ZipShort.getValue(cfh, off);
+        int diskStart = ZipShort.getValue(CFH_BUF, off);
         off += SHORT;
 
-        ze.setInternalAttributes(ZipShort.getValue(cfh, off));
+        ze.setInternalAttributes(ZipShort.getValue(CFH_BUF, off));
         off += SHORT;
 
-        ze.setExternalAttributes(ZipLong.getValue(cfh, off));
+        ze.setExternalAttributes(ZipLong.getValue(CFH_BUF, off));
         off += WORD;
 
         byte[] fileName = new byte[fileNameLen];
@@ -494,7 +497,7 @@ public class ZipFile {
 
         // LFH offset,
         OffsetEntry offset = new OffsetEntry();
-        offset.headerOffset = ZipLong.getValue(cfh, off);
+        offset.headerOffset = ZipLong.getValue(CFH_BUF, off);
         // data offset will be filled later
         entries.put(ze, offset);
 
@@ -684,24 +687,17 @@ public class ZipFile {
     private void positionAtCentralDirectory64()
         throws IOException {
         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] != ZipArchiveOutputStream.ZIP64_EOCD_SIG[POS_0]
-            || sig[POS_1] != ZipArchiveOutputStream.ZIP64_EOCD_SIG[POS_1]
-            || sig[POS_2] != ZipArchiveOutputStream.ZIP64_EOCD_SIG[POS_2]
-            || sig[POS_3] != ZipArchiveOutputStream.ZIP64_EOCD_SIG[POS_3]
-            ) {
+        archive.readFully(DWORD_BUF);
+        archive.seek(ZipEightByteInteger.getLongValue(DWORD_BUF));
+        archive.readFully(WORD_BUF);
+        if (!Arrays.equals(WORD_BUF, ZipArchiveOutputStream.ZIP64_EOCD_SIG)) {
             throw new ZipException("archive's ZIP64 end of central "
                                    + "directory locator is corrupt.");
         }
         skipBytes(ZIP64_EOCD_CFD_LOCATOR_OFFSET
                   - 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");
         }
         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();
             long offset = offsetEntry.headerOffset;
             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;
             while (lenToSkip > 0) {
                 int skipped = archive.skipBytes(lenToSkip);
@@ -854,14 +848,8 @@ public class ZipFile {
      */
     private boolean startsWithLocalFileHeader() throws IOException {
         archive.seek(0);
-        final byte[] start = new byte[WORD];
-        archive.readFully(start);
-        for (int i = 0; i < start.length; i++) {
-            if (start[i] != ZipArchiveOutputStream.LFH_SIG[i]) {
-                return false;
-            }
-        }
-        return true;
+        archive.readFully(WORD_BUF);
+        return Arrays.equals(WORD_BUF, ZipArchiveOutputStream.LFH_SIG);
     }
 
     /**

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/gzip/GzipCompressorInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/gzip/GzipCompressorInputStream.java?rev=1429363&r1=1429362&r2=1429363&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/gzip/GzipCompressorInputStream.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/gzip/GzipCompressorInputStream.java
Sat Jan  5 19:28:42 2013
@@ -76,6 +76,9 @@ public class GzipCompressorInputStream e
     // True once everything has been decompressed
     private boolean endReached = false;
 
+    // used in no-arg read method
+    private final byte[] oneByte = new byte[1];
+
     /**
      * Constructs a new input stream that decompresses gzip-compressed data
      * from the specified input stream.
@@ -213,8 +216,7 @@ public class GzipCompressorInputStream e
     /** {@inheritDoc} */
     @Override
     public int read() throws IOException {
-        byte[] buf = new byte[1];
-        return read(buf, 0, 1) == -1 ? -1 : (buf[0] & 0xFF);
+        return read(oneByte, 0, 1) == -1 ? -1 : (oneByte[0] & 0xFF);
     }
 
     /**



Mime
View raw message