This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-compress.git
The following commit(s) were added to refs/heads/master by this push: new 1b37a8025 [COMPRESS-697] Move BitStream.nextBit() method to BitInputStream (#663) 1b37a8025 is described below commit 1b37a80256f4f2cecfa35a78adfc8d208d06b217 Author: Fredrik Kjellberg <fredrik.kjellb...@gmail.com> AuthorDate: Sun Apr 13 21:47:55 2025 +0200 [COMPRESS-697] Move BitStream.nextBit() method to BitInputStream (#663) --- .../commons/compress/archivers/zip/BinaryTree.java | 2 +- .../commons/compress/archivers/zip/BitStream.java | 10 ---- .../archivers/zip/ExplodingInputStream.java | 2 +- .../commons/compress/utils/BitInputStream.java | 11 ++++ .../compress/archivers/zip/BitStreamTest.java | 43 ++------------- .../commons/compress/utils/BitInputStreamTest.java | 61 ++++++++++++++++++++++ 6 files changed, 78 insertions(+), 51 deletions(-) diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/BinaryTree.java b/src/main/java/org/apache/commons/compress/archivers/zip/BinaryTree.java index 0c8e24932..480410a84 100644 --- a/src/main/java/org/apache/commons/compress/archivers/zip/BinaryTree.java +++ b/src/main/java/org/apache/commons/compress/archivers/zip/BinaryTree.java @@ -179,7 +179,7 @@ public int read(final BitStream stream) throws IOException { int currentIndex = 0; while (true) { - final int bit = stream.nextBit(); + final int bit = stream.readBit(); if (bit == -1) { return -1; } diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/BitStream.java b/src/main/java/org/apache/commons/compress/archivers/zip/BitStream.java index 1bb1ae235..088f7fb5e 100644 --- a/src/main/java/org/apache/commons/compress/archivers/zip/BitStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/zip/BitStream.java @@ -36,16 +36,6 @@ final class BitStream extends BitInputStream { super(in, ByteOrder.LITTLE_ENDIAN); } - /** - * Returns the next bit. - * - * @return The next bit (0 or 1) or -1 if the end of the stream has been reached - * @throws IOException on error. - */ - int nextBit() throws IOException { - return (int) readBits(1); - } - /** * Returns the integer value formed by the n next bits (up to 8 bits). * diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ExplodingInputStream.java b/src/main/java/org/apache/commons/compress/archivers/zip/ExplodingInputStream.java index 80c20fadd..c779ceaba 100644 --- a/src/main/java/org/apache/commons/compress/archivers/zip/ExplodingInputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/zip/ExplodingInputStream.java @@ -103,7 +103,7 @@ public void close() throws IOException { private void fillBuffer() throws IOException { init(); - final int bit = bits.nextBit(); + final int bit = bits.readBit(); if (bit == -1) { // EOF return; 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 a71d9a58d..c89b33441 100644 --- a/src/main/java/org/apache/commons/compress/utils/BitInputStream.java +++ b/src/main/java/org/apache/commons/compress/utils/BitInputStream.java @@ -167,6 +167,17 @@ private long processBitsGreater57(final int count) throws IOException { return bitsOut; } + /** + * Returns the next bit read from the underlying stream. + * + * @return the next bit (0 or 1) or -1 if the end of the stream has been reached + * @throws IOException if an I/O error occurs. + * @since 1.28 + */ + public int readBit() throws IOException { + return (int) readBits(1); + } + /** * Returns at most 63 bits read from the underlying stream. * diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/BitStreamTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/BitStreamTest.java index 229e0111f..e8d0e1565 100644 --- a/src/test/java/org/apache/commons/compress/archivers/zip/BitStreamTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/zip/BitStreamTest.java @@ -28,22 +28,13 @@ public class BitStreamTest { - @Test - public void testEmptyStream() throws Exception { - try (BitStream stream = new BitStream(new ByteArrayInputStream(ByteUtils.EMPTY_BYTE_ARRAY))) { - assertEquals(-1, stream.nextBit(), "next bit"); - assertEquals(-1, stream.nextBit(), "next bit"); - assertEquals(-1, stream.nextBit(), "next bit"); - } - } - @Test public void testNextByte() throws Exception { try (BitStream stream = new BitStream(new ByteArrayInputStream(new byte[] { (byte) 0xEA, 0x35 }))) { - assertEquals(0, stream.nextBit(), "bit 0"); - assertEquals(1, stream.nextBit(), "bit 1"); - assertEquals(0, stream.nextBit(), "bit 2"); - assertEquals(1, stream.nextBit(), "bit 3"); + assertEquals(0, stream.readBit(), "bit 0"); + assertEquals(1, stream.readBit(), "bit 1"); + assertEquals(0, stream.readBit(), "bit 2"); + assertEquals(1, stream.readBit(), "bit 3"); assertEquals(0x5E, stream.nextByte(), "next byte"); assertEquals(-1, stream.nextByte(), "next byte"); // not enough bits left to read a byte @@ -66,30 +57,4 @@ public void testReadAlignedBytes() throws Exception { assertEquals(-1, stream.nextByte(), "next byte"); } } - - @Test - public void testStream() throws Exception { - try (BitStream stream = new BitStream(new ByteArrayInputStream(new byte[] { (byte) 0xEA, 0x03 }))) { - - assertEquals(0, stream.nextBit(), "bit 0"); - assertEquals(1, stream.nextBit(), "bit 1"); - assertEquals(0, stream.nextBit(), "bit 2"); - assertEquals(1, stream.nextBit(), "bit 3"); - assertEquals(0, stream.nextBit(), "bit 4"); - assertEquals(1, stream.nextBit(), "bit 5"); - assertEquals(1, stream.nextBit(), "bit 6"); - assertEquals(1, stream.nextBit(), "bit 7"); - - assertEquals(1, stream.nextBit(), "bit 8"); - assertEquals(1, stream.nextBit(), "bit 9"); - assertEquals(0, stream.nextBit(), "bit 10"); - assertEquals(0, stream.nextBit(), "bit 11"); - assertEquals(0, stream.nextBit(), "bit 12"); - assertEquals(0, stream.nextBit(), "bit 13"); - assertEquals(0, stream.nextBit(), "bit 14"); - assertEquals(0, stream.nextBit(), "bit 15"); - - assertEquals(-1, stream.nextBit(), "next bit"); - } - } } 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 c43c68f57..269868c72 100644 --- a/src/test/java/org/apache/commons/compress/utils/BitInputStreamTest.java +++ b/src/test/java/org/apache/commons/compress/utils/BitInputStreamTest.java @@ -212,4 +212,65 @@ public void testShouldNotAllowReadingOfMoreThan63BitsAtATime() throws IOExceptio } } + @Test + public void testReadingOneBitInLittleEndian() throws Exception { + try (BitInputStream bis = new BitInputStream(new ByteArrayInputStream(new byte[] { (byte) 0xEA, 0x03 }), ByteOrder.LITTLE_ENDIAN)) { + + assertEquals(0, bis.readBit(), "bit 0"); + assertEquals(1, bis.readBit(), "bit 1"); + assertEquals(0, bis.readBit(), "bit 2"); + assertEquals(1, bis.readBit(), "bit 3"); + assertEquals(0, bis.readBit(), "bit 4"); + assertEquals(1, bis.readBit(), "bit 5"); + assertEquals(1, bis.readBit(), "bit 6"); + assertEquals(1, bis.readBit(), "bit 7"); + + assertEquals(1, bis.readBit(), "bit 8"); + assertEquals(1, bis.readBit(), "bit 9"); + assertEquals(0, bis.readBit(), "bit 10"); + assertEquals(0, bis.readBit(), "bit 11"); + assertEquals(0, bis.readBit(), "bit 12"); + assertEquals(0, bis.readBit(), "bit 13"); + assertEquals(0, bis.readBit(), "bit 14"); + assertEquals(0, bis.readBit(), "bit 15"); + + assertEquals(-1, bis.readBit(), "next bit"); + } + } + + @Test + public void testReadingOneBitInBigEndian() throws Exception { + try (BitInputStream bis = new BitInputStream(new ByteArrayInputStream(new byte[] { (byte) 0xEA, 0x03 }), ByteOrder.BIG_ENDIAN)) { + + assertEquals(1, bis.readBit(), "bit 0"); + assertEquals(1, bis.readBit(), "bit 1"); + assertEquals(1, bis.readBit(), "bit 2"); + assertEquals(0, bis.readBit(), "bit 3"); + assertEquals(1, bis.readBit(), "bit 4"); + assertEquals(0, bis.readBit(), "bit 5"); + assertEquals(1, bis.readBit(), "bit 6"); + assertEquals(0, bis.readBit(), "bit 7"); + + assertEquals(0, bis.readBit(), "bit 8"); + assertEquals(0, bis.readBit(), "bit 9"); + assertEquals(0, bis.readBit(), "bit 10"); + assertEquals(0, bis.readBit(), "bit 11"); + assertEquals(0, bis.readBit(), "bit 12"); + assertEquals(0, bis.readBit(), "bit 13"); + assertEquals(1, bis.readBit(), "bit 14"); + assertEquals(1, bis.readBit(), "bit 15"); + + assertEquals(-1, bis.readBit(), "next bit"); + } + } + + @Test + public void testReadingOneBitFromEmptyStream() throws Exception { + try (BitInputStream bis = new BitInputStream(new ByteArrayInputStream(ByteUtils.EMPTY_BYTE_ARRAY), ByteOrder.LITTLE_ENDIAN)) { + assertEquals(-1, bis.readBit(), "next bit"); + assertEquals(-1, bis.readBit(), "next bit"); + assertEquals(-1, bis.readBit(), "next bit"); + } + } + }