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 2018/01/21 12:34:27 UTC

[1/2] commons-compress git commit: explicit tests for new BitInputStream methods

Repository: commons-compress
Updated Branches:
  refs/heads/master d8e4e255d -> d5f61b439


explicit tests for new BitInputStream methods


Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/271e0a76
Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/271e0a76
Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/271e0a76

Branch: refs/heads/master
Commit: 271e0a76d4e41171ace14bb802c990d4998dd621
Parents: d8e4e25
Author: Stefan Bodewig <bo...@apache.org>
Authored: Sun Jan 21 13:28:47 2018 +0100
Committer: Stefan Bodewig <bo...@apache.org>
Committed: Sun Jan 21 13:28:47 2018 +0100

----------------------------------------------------------------------
 .../compress/utils/BitInputStreamTest.java      | 35 ++++++++++++++++++++
 1 file changed, 35 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-compress/blob/271e0a76/src/test/java/org/apache/commons/compress/utils/BitInputStreamTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/compress/utils/BitInputStreamTest.java b/src/test/java/org/apache/commons/compress/utils/BitInputStreamTest.java
index e39a0d7..92de71b 100644
--- a/src/test/java/org/apache/commons/compress/utils/BitInputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/utils/BitInputStreamTest.java
@@ -171,6 +171,41 @@ public class BitInputStreamTest {
         }
     }
 
+    @Test
+    public void alignWithByteBoundaryWhenAtBoundary() throws Exception {
+        try (final BitInputStream bis = new BitInputStream(getStream(), ByteOrder.LITTLE_ENDIAN)) {
+            assertEquals(0xF8, bis.readBits(8));
+            bis.alignWithByteBoundary();
+            assertEquals(0, bis.readBits(4));
+        }
+    }
+
+    @Test
+    public void alignWithByteBoundaryWhenNotAtBoundary() throws Exception {
+        try (final BitInputStream bis = new BitInputStream(getStream(), ByteOrder.LITTLE_ENDIAN)) {
+            assertEquals(0x08, bis.readBits(4));
+            assertEquals(4, bis.bitsCached());
+            bis.alignWithByteBoundary();
+            assertEquals(0, bis.bitsCached());
+            assertEquals(0, bis.readBits(4));
+        }
+    }
+
+    @Test
+    public void availableWithoutCache() throws Exception {
+        try (final BitInputStream bis = new BitInputStream(getStream(), ByteOrder.LITTLE_ENDIAN)) {
+            assertEquals(32, bis.bitsAvailable());
+        }
+    }
+
+    @Test
+    public void availableWithCache() throws Exception {
+        try (final BitInputStream bis = new BitInputStream(getStream(), ByteOrder.LITTLE_ENDIAN)) {
+            assertEquals(0x08, bis.readBits(4));
+            assertEquals(28, bis.bitsAvailable());
+        }
+    }
+
     private ByteArrayInputStream getStream() {
         return new ByteArrayInputStream(new byte[] {
                 (byte) 0xF8,  // 11111000


[2/2] commons-compress git commit: reading from the cache doesn't need to throw an exception

Posted by bo...@apache.org.
reading from the cache doesn't need to throw an exception


Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/d5f61b43
Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/d5f61b43
Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/d5f61b43

Branch: refs/heads/master
Commit: d5f61b4398e6572d974ee13d01f47f82744fb495
Parents: 271e0a7
Author: Stefan Bodewig <bo...@apache.org>
Authored: Sun Jan 21 13:33:39 2018 +0100
Committer: Stefan Bodewig <bo...@apache.org>
Committed: Sun Jan 21 13:33:39 2018 +0100

----------------------------------------------------------------------
 .../commons/compress/utils/BitInputStream.java  | 27 +++++++++++---------
 1 file changed, 15 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-compress/blob/d5f61b43/src/main/java/org/apache/commons/compress/utils/BitInputStream.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/utils/BitInputStream.java b/src/main/java/org/apache/commons/compress/utils/BitInputStream.java
index 770101d..e0645b3 100644
--- a/src/main/java/org/apache/commons/compress/utils/BitInputStream.java
+++ b/src/main/java/org/apache/commons/compress/utils/BitInputStream.java
@@ -89,15 +89,7 @@ public class BitInputStream implements Closeable {
         if (bitsCachedSize < count) {
             return processBitsGreater57(count);
         }
-        final long bitsOut;
-        if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
-            bitsOut = (bitsCached & MASKS[count]);
-            bitsCached >>>= count;
-        } else {
-            bitsOut = (bitsCached >> (bitsCachedSize - count)) & MASKS[count];
-        }
-        bitsCachedSize -= count;
-        return bitsOut;
+        return readCachedBits(count);
     }
 
     /**
@@ -124,13 +116,12 @@ public class BitInputStream implements Closeable {
 
     /**
      * Drops bits until the next bits will be read from a byte boundary.
-     * @throws IOException if reading the remaining bits to the next byte boundary fails
      * @since 1.16
      */
-    public void alignWithByteBoundary() throws IOException {
+    public void alignWithByteBoundary() {
         int toSkip = bitsCachedSize % 8;
         if (toSkip > 0) {
-            readBits(toSkip);
+            readCachedBits(toSkip);
         }
     }
 
@@ -162,6 +153,18 @@ public class BitInputStream implements Closeable {
         return bitsOut;
     }
 
+    private long readCachedBits(int count) {
+        final long bitsOut;
+        if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
+            bitsOut = (bitsCached & MASKS[count]);
+            bitsCached >>>= count;
+        } else {
+            bitsOut = (bitsCached >> (bitsCachedSize - count)) & MASKS[count];
+        }
+        bitsCachedSize -= count;
+        return bitsOut;
+    }
+
     /**
      * Fills the cache up to 56 bits
      * @param count