commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ebo...@apache.org
Subject svn commit: r1552156 - /commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
Date Wed, 18 Dec 2013 23:14:26 GMT
Author: ebourg
Date: Wed Dec 18 23:14:26 2013
New Revision: 1552156

URL: http://svn.apache.org/r1552156
Log:
Replaced ZipArchiveInputStream.Buffer with a ByteBuffer

Modified:
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java

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=1552156&r1=1552155&r2=1552156&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
Wed Dec 18 23:14:26 2013
@@ -24,6 +24,7 @@ import java.io.EOFException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.PushbackInputStream;
+import java.nio.ByteBuffer;
 import java.util.zip.CRC32;
 import java.util.zip.DataFormatException;
 import java.util.zip.Inflater;
@@ -85,7 +86,7 @@ public class ZipArchiveInputStream exten
     /**
      * Buffer used to read from the wrapped stream.
      */
-    private final Buffer buf = new Buffer();
+    private final ByteBuffer buf = ByteBuffer.allocate(ZipArchiveOutputStream.BUFFER_SIZE);
     /**
      * The entry that is currently being read.
      */
@@ -423,13 +424,16 @@ public class ZipArchiveInputStream exten
             return -1;
         }
 
-        if (buf.position >= buf.limit) {
-            buf.position = 0;
-            if ((buf.limit = in.read(buf.array)) == -1) {
+        if (buf.position() >= buf.limit()) {
+            buf.position(0);
+            int l = in.read(buf.array());
+            if (l == -1) {
                 return -1;
             }
-            count(buf.limit);
-            current.bytesReadFromStream += buf.limit;
+            buf.limit(l);
+
+            count(buf.limit());
+            current.bytesReadFromStream += buf.limit();
         }
 
         int toRead = Math.min(buf.remaining(), length);
@@ -437,8 +441,7 @@ public class ZipArchiveInputStream exten
             // if it is smaller than toRead then it fits into an int
             toRead = (int) (csize - current.bytesRead);
         }
-        System.arraycopy(buf.array, buf.position, buffer, start, toRead);
-        buf.position += toRead;
+        buf.get(buffer, start, toRead);
         current.bytesRead += toRead;
         crc.update(buffer, start, toRead);
         return toRead;
@@ -450,14 +453,14 @@ public class ZipArchiveInputStream exten
     private int readDeflated(byte[] buffer, int start, int length)
         throws IOException {
         int read = readFromInflater(buffer, start, length);
-        if (read == 0) {
+        if (read <= 0) {
             if (inf.finished()) {
                 return -1;
             } else if (inf.needsDictionary()) {
                 throw new ZipException("This archive needs a preset dictionary"
                                        + " which is not supported by Commons"
                                        + " Compress.");
-            } else if (buf.limit == -1) {
+            } else if (read == -1) {
                 throw new IOException("Truncated ZIP file");
             }
         }
@@ -474,9 +477,11 @@ public class ZipArchiveInputStream exten
         int read = 0;
         do {
             if (inf.needsInput()) {
-                fill();
-                if (buf.limit > 0) {
-                    current.bytesReadFromStream += buf.limit;
+                int l = fill();
+                if (l > 0) {
+                    current.bytesReadFromStream += buf.limit();
+                } else if (l == -1) {
+                    return -1;
                 } else {
                     break;
                 }
@@ -608,7 +613,7 @@ public class ZipArchiveInputStream exten
 
             // Pushback any required bytes
             if (diff > 0) {
-                pushback(buf.array, buf.limit - diff, diff);
+                pushback(buf.array(), buf.limit() - diff, diff);
             }
         }
 
@@ -617,7 +622,7 @@ public class ZipArchiveInputStream exten
         }
 
         inf.reset();
-        buf.reset();
+        buf.clear().flip();
         crc.reset();
         current = null;
         lastStoredEntry = null;
@@ -631,7 +636,7 @@ public class ZipArchiveInputStream exten
         long remaining = current.entry.getCompressedSize()
             - current.bytesReadFromStream;
         while (remaining > 0) {
-            long n = in.read(buf.array, 0, (int) Math.min(buf.capacity(), remaining));
+            long n = in.read(buf.array(), 0, (int) Math.min(buf.capacity(), remaining));
             if (n < 0) {
                 throw new EOFException(
                                        "Truncated ZIP entry: " + current.entry.getName());
@@ -667,14 +672,17 @@ public class ZipArchiveInputStream exten
         return inB;
     }
 
-    private void fill() throws IOException {
+    private int fill() throws IOException {
         if (closed) {
             throw new IOException("The stream is closed");
         }
-        if ((buf.limit = in.read(buf.array)) > 0) {
-            count(buf.limit);
-            inf.setInput(buf.array, 0, buf.limit);
+        int length = in.read(buf.array());
+        if (length > 0) {
+            buf.limit(length);
+            count(buf.limit());
+            inf.setInput(buf.array(), 0, buf.limit());
         }
+        return length;
     }
 
     private void readFully(byte[] b) throws IOException {
@@ -761,8 +769,7 @@ public class ZipArchiveInputStream exten
         int ddLen = current.usesZip64 ? WORD + 2 * DWORD : 3 * WORD;
 
         while (!done) {
-            int r = in.read(buf.array, off,
-                            ZipArchiveOutputStream.BUFFER_SIZE - off);
+            int r = in.read(buf.array(), off, ZipArchiveOutputStream.BUFFER_SIZE - off);
             if (r <= 0) {
                 // read the whole archive without ever finding a
                 // central directory
@@ -803,14 +810,14 @@ public class ZipArchiveInputStream exten
         boolean done = false;
         int readTooMuch = 0;
         for (int i = 0; !done && i < lastRead - 4; i++) {
-            if (buf.array[i] == LFH[0] && buf.array[i + 1] == LFH[1]) {
-                if ((buf.array[i + 2] == LFH[2] && buf.array[i + 3] == LFH[3])
-                    || (buf.array[i] == CFH[2] && buf.array[i + 3] == CFH[3])) {
+            if (buf.array()[i] == LFH[0] && buf.array()[i + 1] == LFH[1]) {
+                if ((buf.array()[i + 2] == LFH[2] && buf.array()[i + 3] == LFH[3])
+                    || (buf.array()[i] == CFH[2] && buf.array()[i + 3] == CFH[3]))
{
                     // found a LFH or CFH:
                     readTooMuch = offset + lastRead - i - expectedDDLen;
                     done = true;
                 }
-                else if (buf.array[i + 2] == DD[2] && buf.array[i + 3] == DD[3])
{
+                else if (buf.array()[i + 2] == DD[2] && buf.array()[i + 3] == DD[3])
{
                     // found DD:
                     readTooMuch = offset + lastRead - i;
                     done = true;
@@ -820,9 +827,8 @@ public class ZipArchiveInputStream exten
                     //   descriptor
                     // * copy the remaining bytes to cache
                     // * read data descriptor
-                    pushback(buf.array, offset + lastRead - readTooMuch,
-                             readTooMuch);
-                    bos.write(buf.array, 0, i);
+                    pushback(buf.array(), offset + lastRead - readTooMuch, readTooMuch);
+                    bos.write(buf.array(), 0, i);
                     readDataDescriptor();
                 }
             }
@@ -843,9 +849,8 @@ public class ZipArchiveInputStream exten
                                int lastRead, int expecteDDLen) {
         final int cacheable = offset + lastRead - expecteDDLen - 3;
         if (cacheable > 0) {
-            bos.write(buf.array, 0, cacheable);
-            System.arraycopy(buf.array, cacheable, buf.array, 0,
-                             expecteDDLen + 3);
+            bos.write(buf.array(), 0, cacheable);
+            System.arraycopy(buf.array(), cacheable, buf.array(), 0, expecteDDLen + 3);
             offset = expecteDDLen + 3;
         } else {
             offset += lastRead;
@@ -1006,54 +1011,4 @@ public class ZipArchiveInputStream exten
          */
         private long bytesReadFromStream;
     }
-
-    /**
-     * Contains a temporary buffer used to read from the wrapped
-     * stream together with some information needed for internal
-     * housekeeping.
-     * 
-     * This class is similar to a java.nio.ByteBuffer but can't be replaced,
-     * because the limit contains the length of the last read operation and
-     * when the end of the stream is reached the limit is thus set to -1.
-     * This is not allowed for a ByteBuffer.
-     */
-    private static final class Buffer {
-        /**
-         * Buffer used as temporary buffer when reading from the stream.
-         */
-        private final byte[] array = new byte[ZipArchiveOutputStream.BUFFER_SIZE];
-
-        /**
-         * {@link #array array} may contain data the client hasnt read, yet,
-         * this is the first byte that hasn't been read so far.
-         */
-        private int position = 0;
-
-        /**
-         * Number of bytes read from the wrapped stream into {@link #array
-         * array} with the last read operation.
-         */
-        private int limit = 0;
-
-        /**
-         * Reset internal housekeeping.
-         */
-        private void reset() {
-            position = limit = 0;
-        }
-
-        /**
-         * Returns the number of bytes left to read.
-         */
-        private int remaining() {
-            return limit - position;
-        }
-
-        /**
-         * Returns the length of the buffer.
-         */
-        private int capacity() {
-            return array.length;
-        }
-    }
 }



Mime
View raw message