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 2014/11/13 15:50:37 UTC

svn commit: r1639351 - /commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/utils/BitInputStream.java

Author: bodewig
Date: Thu Nov 13 14:50:37 2014
New Revision: 1639351

URL: http://svn.apache.org/r1639351
Log:
cache bit masks

Modified:
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/utils/BitInputStream.java

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=1639351&r1=1639350&r2=1639351&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 Thu Nov 13 14:50:37 2014
@@ -29,6 +29,15 @@ 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];
+
+    static {
+        for (int i = 1; i <= MAXIMUM_CACHE_SIZE; i++) {
+            MASKS[i] = (MASKS[i - 1] << 1) + 1;
+        }
+    }
+
     private final InputStream in;
     private final ByteOrder byteOrder;
     private int bitsCached = 0;
@@ -67,8 +76,8 @@ public class BitInputStream implements C
      *         -1 if the end of the underlying stream has been reached
      */
     public int readBits(final int count) throws IOException {
-        if (count < 0 || count > 31) {
-            throw new IllegalArgumentException("count must be between 0 and 32");
+        if (count < 0 || count > MAXIMUM_CACHE_SIZE) {
+            throw new IllegalArgumentException("count must be between 0 and " + MAXIMUM_CACHE_SIZE);
         }
         while (bitsCachedSize < count) {
             final int nextByte = in.read();
@@ -84,13 +93,12 @@ public class BitInputStream implements C
             bitsCachedSize += 8;
         }
         
-        final int mask = (1 << count) - 1;
         final int bitsOut;
         if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
-            bitsOut = (bitsCached & mask);
+            bitsOut = (bitsCached & MASKS[count]);
             bitsCached >>>= count;
         } else {
-            bitsOut = (bitsCached >> (bitsCachedSize - count)) & mask;
+            bitsOut = (bitsCached >> (bitsCachedSize - count)) & MASKS[count];
         }
         bitsCachedSize -= count;
         return bitsOut;