Author: bodewig
Date: Sun Jan 25 10:00:53 2015
New Revision: 1654638

URL: http://svn.apache.org/r1654638
Log:
expand BitInputStream to allow reading of up to 63 bits at once

Modified:
    
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/BitStream.java
    
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ExplodingInputStream.java
    
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/lzw/LZWInputStream.java
    
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/z/ZCompressorInputStream.java
    
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/utils/BitInputStream.java
    
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/utils/BitInputStreamTest.java

Modified: 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/BitStream.java
URL: 
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/BitStream.java?rev=1654638&r1=1654637&r2=1654638&view=diff
==============================================================================
--- 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/BitStream.java
 (original)
+++ 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/BitStream.java
 Sun Jan 25 10:00:53 2015
@@ -42,7 +42,7 @@ class BitStream extends BitInputStream {
      * @return The next bit (0 or 1) or -1 if the end of the stream has been 
reached
      */
     int nextBit() throws IOException {
-        return readBits(1);
+        return (int) readBits(1);
     }
 
     /**
@@ -51,11 +51,11 @@ class BitStream extends BitInputStream {
      * @param n the number of bits read (up to 8)
      * @return The value formed by the n bits, or -1 if the end of the stream 
has been reached
      */
-    int nextBits(final int n) throws IOException {
+    long nextBits(final int n) throws IOException {
         return readBits(n);
     }
 
     int nextByte() throws IOException {
-        return readBits(8);
+        return (int) readBits(8);
     }
 }

Modified: 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ExplodingInputStream.java
URL: 
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ExplodingInputStream.java?rev=1654638&r1=1654637&r2=1654638&view=diff
==============================================================================
--- 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ExplodingInputStream.java
 (original)
+++ 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ExplodingInputStream.java
 Sun Jan 25 10:00:53 2015
@@ -124,7 +124,7 @@ class ExplodingInputStream extends Input
             if (literalTree != null) {
                 literal = literalTree.read(bits);
             } else {
-                literal = bits.nextBits(8);
+                literal = bits.nextByte();
             }
 
             if (literal == -1) {
@@ -137,7 +137,7 @@ class ExplodingInputStream extends Input
         } else if (bit == 0) {
             // back reference
             int distanceLowSize = dictionarySize == 4096 ? 6 : 7;
-            int distanceLow = bits.nextBits(distanceLowSize);
+            int distanceLow = (int) bits.nextBits(distanceLowSize);
             int distanceHigh = distanceTree.read(bits);
             if (distanceHigh == -1 && distanceLow <= 0) {
                 // end of stream reached, nothing left to decode

Modified: 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/lzw/LZWInputStream.java
URL: 
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/lzw/LZWInputStream.java?rev=1654638&r1=1654637&r2=1654638&view=diff
==============================================================================
--- 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/lzw/LZWInputStream.java
 (original)
+++ 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/lzw/LZWInputStream.java
 Sun Jan 25 10:00:53 2015
@@ -121,7 +121,10 @@ public abstract class LZWInputStream ext
      * Reads the next code from the stream.
      */
     protected int readNextCode() throws IOException {
-        return in.readBits(codeSize);
+        if (codeSize > 31) {
+            throw new IllegalArgumentException("code size must not be bigger 
than 31");
+        }
+        return (int) in.readBits(codeSize);
     }
     
     /**

Modified: 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/z/ZCompressorInputStream.java
URL: 
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/z/ZCompressorInputStream.java?rev=1654638&r1=1654637&r2=1654638&view=diff
==============================================================================
--- 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/z/ZCompressorInputStream.java
 (original)
+++ 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/z/ZCompressorInputStream.java
 Sun Jan 25 10:00:53 2015
@@ -40,9 +40,9 @@ public class ZCompressorInputStream exte
     
     public ZCompressorInputStream(InputStream inputStream) throws IOException {
         super(inputStream, ByteOrder.LITTLE_ENDIAN);
-        int firstByte = in.readBits(8);
-        int secondByte = in.readBits(8);
-        int thirdByte = in.readBits(8);
+        int firstByte = (int) in.readBits(8);
+        int secondByte = (int) in.readBits(8);
+        int thirdByte = (int) in.readBits(8);
         if (firstByte != MAGIC_1 || secondByte != MAGIC_2 || thirdByte < 0) {
             throw new IOException("Input is not in .Z format");
         }

Modified: 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/utils/BitInputStream.java
URL: 
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/utils/BitInputStream.java?rev=1654638&r1=1654637&r2=1654638&view=diff
==============================================================================
--- 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/utils/BitInputStream.java
 (original)
+++ 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/utils/BitInputStream.java
 Sun Jan 25 10:00:53 2015
@@ -29,8 +29,8 @@ import java.nio.ByteOrder;
  * @NotThreadSafe
  */
 public class BitInputStream implements Closeable {
-    private static final int MAXIMUM_CACHE_SIZE = 31; // bits in int minus 
sign bit
-    private static final int[] MASKS = new int[MAXIMUM_CACHE_SIZE + 1];
+    private static final int MAXIMUM_CACHE_SIZE = 63; // bits in long minus 
sign bit
+    private static final long[] MASKS = new long[MAXIMUM_CACHE_SIZE + 1];
 
     static {
         for (int i = 1; i <= MAXIMUM_CACHE_SIZE; i++) {
@@ -40,7 +40,7 @@ public class BitInputStream implements C
 
     private final InputStream in;
     private final ByteOrder byteOrder;
-    private int bitsCached = 0;
+    private long bitsCached = 0;
     private int bitsCachedSize = 0;
 
     /**
@@ -68,20 +68,20 @@ public class BitInputStream implements C
     }
     
     /**
-     * Returns at most 31 bits read from the underlying stream.
+     * Returns at most 63 bits read from the underlying stream.
      *
      * @param count the number of bits to read, must be a positive
-     * number not bigger than 31.
-     * @return the bits concatenated as an integer using the stream's byte 
order.
+     * number not bigger than 63.
+     * @return the bits concatenated as a long using the stream's byte order.
      *         -1 if the end of the underlying stream has been reached before 
reading
      *         the requested number of bits
      */
-    public int readBits(final int count) throws IOException {
+    public long readBits(final int count) throws IOException {
         if (count < 0 || count > MAXIMUM_CACHE_SIZE) {
             throw new IllegalArgumentException("count must not be negative or 
greater than " + MAXIMUM_CACHE_SIZE);
         }
         while (bitsCachedSize < count) {
-            final int nextByte = in.read();
+            final long nextByte = in.read();
             if (nextByte < 0) {
                 return nextByte;
             }
@@ -94,7 +94,7 @@ public class BitInputStream implements C
             bitsCachedSize += 8;
         }
         
-        final int bitsOut;
+        final long bitsOut;
         if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
             bitsOut = (bitsCached & MASKS[count]);
             bitsCached >>>= count;

Modified: 
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/utils/BitInputStreamTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/utils/BitInputStreamTest.java?rev=1654638&r1=1654637&r2=1654638&view=diff
==============================================================================
--- 
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/utils/BitInputStreamTest.java
 (original)
+++ 
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/utils/BitInputStreamTest.java
 Sun Jan 25 10:00:53 2015
@@ -35,9 +35,9 @@ public class BitInputStreamTest {
     }
 
     @Test(expected = IllegalArgumentException.class)
-    public void shouldNotAllowReadingOfMoreThan31BitsAtATime() throws 
IOException {
+    public void shouldNotAllowReadingOfMoreThan63BitsAtATime() throws 
IOException {
         BitInputStream bis = new BitInputStream(getStream(), 
ByteOrder.LITTLE_ENDIAN);
-        bis.readBits(32);
+        bis.readBits(64);
     }
 
     @Test


Reply via email to