This is an automated email from the ASF dual-hosted git repository.
garydgregory 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 9481ee23f Add BitInputStream.getByteOrder() (#720).
9481ee23f is described below
commit 9481ee23f47fad694f42d272c0d2a030c09ec85c
Author: Gary Gregory <[email protected]>
AuthorDate: Fri Jul 31 18:38:00 2026 -0400
Add BitInputStream.getByteOrder() (#720).
---
src/changes/changes.xml | 1 +
.../commons/compress/utils/BitInputStream.java | 47 ++++++++++++++++++++--
.../commons/compress/utils/BitInputStreamTest.java | 14 +++++++
3 files changed, 59 insertions(+), 3 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 50a165d97..50f6383d1 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -182,6 +182,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="pkarwasz" due-to="Piotr P. Karwasz">Add a
configurable maxEntryNameLength option to all archivers.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add
UnsupportedZipFeatureException.Feature.toString().</action>
<action type="add" dev="ggregory" due-to="Fredrik Kjellberg, Gary
Gregory, Piotr P. Karwasz" issue="COMPRESS-706">Add support for reading LHA
archive format (#690).</action>
+ <action type="add" dev="ggregory" due-to="Piotr P. Karwasz, Gary
Gregory">Add BitInputStream.getByteOrder() (#720).</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump
org.apache.commons:commons-parent from 85 to 103 #707, #752.</action>
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump
org.apache.commons:commons-lang3 from 3.18.0 to 3.20.0.</action>
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 bbe789066..3bf3e9683 100644
--- a/src/main/java/org/apache/commons/compress/utils/BitInputStream.java
+++ b/src/main/java/org/apache/commons/compress/utils/BitInputStream.java
@@ -45,10 +45,40 @@ public class BitInputStream implements Closeable {
private int bitsCachedSize;
/**
- * Constructor taking an InputStream and its bit arrangement.
+ * Constructs a {@code BitInputStream} that reads individual bits from the
given {@link InputStream}, interpreting them according to the specified bit
+ * ordering.
+ * <p>
+ * The bit ordering determines how consecutive bits are packed into bytes:
+ * </p>
+ * <ul>
+ * <li>{@link ByteOrder#BIG_ENDIAN} (most significant bit first):
+ * <p>
+ * Bits are read from the high (bit 7) to the low (bit 0)
position of each byte.
+ * </p>
+ *
+ * <pre>{@code
+ * byte 0 byte 1
+ * bit 7 6 5 4 3 2 1 0 | 7 6 5 4 3 2 1 0
+ * a4 a3 a2 a1 a0 b4 b3 b2 b1 b0 0 0 0 0 0 0
+ * }</pre>
+ *
+ * </li>
+ * <li>{@link ByteOrder#LITTLE_ENDIAN} (least significant bit first):
+ * <p>
+ * Bits are read from the low (bit 0) to the high (bit 7)
position of each byte.
+ * </p>
*
- * @param in The InputStream.
- * @param byteOrder The bit arrangement across byte boundaries, either
BIG_ENDIAN (aaaaabbb bb000000) or LITTLE_ENDIAN (bbbaaaaa 000000bb).
+ * <pre>{@code
+ * byte 0 byte 1
+ * bit 7 6 5 4 3 2 1 0 | 7 6 5 4 3 2 1 0
+ * b2 b1 b0 a4 a3 a2 a1 a0 0 0 0 0 0 b4 b3 b2
+ * }</pre>
+ *
+ * </li>
+ * </ul>
+ *
+ * @param in the underlying input stream providing the bytes
+ * @param byteOrder determines whether bits are read MSB-first ({@link
ByteOrder#BIG_ENDIAN}) or LSB-first ({@link ByteOrder#LITTLE_ENDIAN}).
*/
public BitInputStream(final InputStream in, final ByteOrder byteOrder) {
this.in =
org.apache.commons.io.input.BoundedInputStream.builder().setInputStream(in).asSupplier().get();
@@ -126,6 +156,17 @@ private boolean ensureCache(final int count) throws
IOException {
return false;
}
+ /**
+ * Gets the bit order used by this stream.
+ *
+ * @return the bit ordering: {@link ByteOrder#BIG_ENDIAN} (MSB-first) or
{@link ByteOrder#LITTLE_ENDIAN} (LSB-first).
+ * @see #BitInputStream(InputStream, ByteOrder)
+ * @since 1.29.0
+ */
+ public ByteOrder getByteOrder() {
+ return byteOrder;
+ }
+
/**
* Gets the number of bytes read from the underlying stream.
* <p>
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 513412e42..caf07de0b 100644
--- a/src/test/java/org/apache/commons/compress/utils/BitInputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/utils/BitInputStreamTest.java
@@ -27,9 +27,15 @@
import org.apache.commons.lang3.ArrayUtils;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
class BitInputStreamTest {
+ static ByteOrder[] byteOrderProvider() {
+ return new ByteOrder[] { ByteOrder.BIG_ENDIAN,
ByteOrder.LITTLE_ENDIAN, ByteOrder.nativeOrder() };
+ }
+
private ByteArrayInputStream getStream() {
return new ByteArrayInputStream(new byte[] { (byte) 0xF8, // 11111000
0x40, // 01000000
@@ -113,6 +119,14 @@ void testEOF() throws IOException {
}
}
+ @ParameterizedTest
+ @MethodSource("byteOrderProvider")
+ void testGetByteOrder(final ByteOrder byteOrder) throws IOException {
+ try (BitInputStream bis = new BitInputStream(getStream(), byteOrder)) {
+ assertEquals(byteOrder, bis.getByteOrder());
+ }
+ }
+
/**
* @see "https://issues.apache.org/jira/browse/COMPRESS-363"
*/