You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by bo...@apache.org on 2013/11/10 11:14:50 UTC

svn commit: r1540459 - in /commons/proper/compress/trunk/src: main/java/org/apache/commons/compress/compressors/snappy/ test/java/org/apache/commons/compress/compressors/

Author: bodewig
Date: Sun Nov 10 10:14:50 2013
New Revision: 1540459

URL: http://svn.apache.org/r1540459
Log:
COMPRESS-147 some cleanup, fix counting, simple available implementation

Modified:
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorInputStream.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/snappy/SnappyCompressorInputStream.java
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/FramedSnappyTestCase.java

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorInputStream.java?rev=1540459&r1=1540458&r2=1540459&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorInputStream.java (original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorInputStream.java Sun Nov 10 10:14:50 2013
@@ -90,12 +90,24 @@ public class FramedSnappyCompressorInput
         return read;
     }
 
+    /** {@inheritDoc} */
+    @Override
+    public int available() throws IOException {
+        if (inUncompressedChunk) {
+            return Math.min(uncompressedBytesRemaining,
+                            in.available());
+        } else if (currentCompressedChunk != null) {
+            return currentCompressedChunk.available();
+        }
+        return 0;
+    }
+
     /**
      * Read from the current chunk into the given array.
      *
      * @return -1 if there is no current chunk or the number of bytes
-     * read from the current chunk (which may be -1 if the end od the
-     * chunk is reached.
+     * read from the current chunk (which may be -1 if the end of the
+     * chunk is reached).
      */
     private int readOnce(byte[] b, int off, int len) throws IOException {
         int read = -1;
@@ -142,6 +154,8 @@ public class FramedSnappyCompressorInput
             readCrc();
             currentCompressedChunk =
                 new SnappyCompressorInputStream(new BoundedInputStream(in, size));
+            // constructor reads uncompressed size
+            count(currentCompressedChunk.getBytesRead());
         } else {
             // impossible as all potential byte values have been covered
             throw new IOException("unknown chunk type " + type
@@ -151,10 +165,13 @@ public class FramedSnappyCompressorInput
 
     private void readCrc() throws IOException {
         byte[] b = new byte[4];
-        if (IOUtils.readFully(in, b) != 4) {
+        int read = IOUtils.readFully(in, b);
+        if (read > 0) {
+            count(read);
+        }
+        if (read != 4) {
             throw new IOException("premature end of stream");
         }
-        count(4);
     }
 
     private int readSize() throws IOException {
@@ -172,18 +189,24 @@ public class FramedSnappyCompressorInput
 
     private void skipBlock() throws IOException {
         int size = readSize();
-        if (IOUtils.skip(in, size) != size) {
+        long read = IOUtils.skip(in, size);
+        if (read > 0) {
+            count(read);
+        }
+        if (read != size) {
             throw new IOException("premature end of stream");
         }
-        count(size);
     }
 
     private void readStreamIdentifier() throws IOException {
         byte[] b = new byte[10];
-        if (10 != IOUtils.readFully(in, b) || !matches(b, 10)) {
+        int read = IOUtils.readFully(in, b);
+        if (read > 0) {
+            count(read);
+        }
+        if (10 != read || !matches(b, 10)) {
             throw new IOException("Not a framed Snappy stream");
         }
-        count(10);
     }
 
     private int readOneByte() throws IOException {

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/snappy/SnappyCompressorInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/snappy/SnappyCompressorInputStream.java?rev=1540459&r1=1540458&r2=1540459&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/snappy/SnappyCompressorInputStream.java (original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/snappy/SnappyCompressorInputStream.java Sun Nov 10 10:14:50 2013
@@ -135,8 +135,9 @@ public class SnappyCompressorInputStream
         if (endReached) {
             return -1;
         }
-        if (len > available()) {
-            fill(len - available());
+        final int avail = available();
+        if (len > avail) {
+            fill(len - avail);
         }
 
         int readable = Math.min(len, available());
@@ -149,7 +150,8 @@ public class SnappyCompressorInputStream
     }
 
     /**
-     * Try to fill the buffer with enoug bytes to satisfy the current read request.
+     * Try to fill the buffer with enough bytes to satisfy the current
+     * read request.
      *
      * @param len the number of uncompressed bytes to read
      */
@@ -245,12 +247,12 @@ public class SnappyCompressorInputStream
     /**
      * Slide buffer.
      *
-     * <p>Move all bytes of the buffer after the first block down
-     * tothe beginning of the buffer.</p>
+     * <p>Move all bytes of the buffer after the first block down to
+     * the beginning of the buffer.</p>
      */
     private void slideBuffer() {
         System.arraycopy(decompressBuf, blockSize, decompressBuf, 0,
-                blockSize);
+                         blockSize * 2);
         writeIndex -= blockSize;
         readIndex -= blockSize;
     }
@@ -309,7 +311,9 @@ public class SnappyCompressorInputStream
      */
     private boolean expandLiteral(final int length) throws IOException {
         int bytesRead = in.read(decompressBuf, writeIndex, length);
-        count(bytesRead);
+        if (bytesRead != -1) {
+            count(bytesRead);
+        }
         if (length != bytesRead) {
             throw new IOException("Premature end of stream");
         }

Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/FramedSnappyTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/FramedSnappyTestCase.java?rev=1540459&r1=1540458&r2=1540459&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/FramedSnappyTestCase.java (original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/FramedSnappyTestCase.java Sun Nov 10 10:14:50 2013
@@ -57,6 +57,7 @@ public final class FramedSnappyTestCase
             try {
                 out = new FileOutputStream(output);
                 IOUtils.copy(in, out);
+                assertEquals(995, in.getBytesRead());
             } finally {
                 if (out != null) {
                     out.close();