Class BitInputStream
- java.lang.Object
-
- org.apache.commons.compress.utils.BitInputStream
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
- Direct Known Subclasses:
BitStream
public class BitInputStream extends java.lang.Object implements java.io.Closeable
Reads bits from an InputStream.- Since:
- 1.10
-
-
Field Summary
Fields Modifier and Type Field Description private long
bitsCached
private int
bitsCachedSize
private java.nio.ByteOrder
byteOrder
private CountingInputStream
in
private static long[]
MASKS
private static int
MAXIMUM_CACHE_SIZE
-
Constructor Summary
Constructors Constructor Description BitInputStream(java.io.InputStream in, java.nio.ByteOrder byteOrder)
Constructor taking an InputStream and its bit arrangement.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
alignWithByteBoundary()
Drops bits until the next bits will be read from a byte boundary.long
bitsAvailable()
Returns an estimate of the number of bits that can be read from this input stream without blocking by the next invocation of a method for this input stream.int
bitsCached()
Returns the number of bits that can be read from this input stream without reading from the underlying input stream at all.void
clearBitCache()
Clears the cache of bits that have been read from the underlying stream but not yet provided viareadBits(int)
.void
close()
private boolean
ensureCache(int count)
Fills the cache up to 56 bitslong
getBytesRead()
Returns the number of bytes read from the underlying stream.private long
processBitsGreater57(int count)
long
readBits(int count)
Returns at most 63 bits read from the underlying stream.private long
readCachedBits(int count)
-
-
-
Field Detail
-
MAXIMUM_CACHE_SIZE
private static final int MAXIMUM_CACHE_SIZE
- See Also:
- Constant Field Values
-
MASKS
private static final long[] MASKS
-
in
private final CountingInputStream in
-
byteOrder
private final java.nio.ByteOrder byteOrder
-
bitsCached
private long bitsCached
-
bitsCachedSize
private int bitsCachedSize
-
-
Constructor Detail
-
BitInputStream
public BitInputStream(java.io.InputStream in, java.nio.ByteOrder byteOrder)
Constructor taking an InputStream and its bit arrangement.- Parameters:
in
- the InputStreambyteOrder
- the bit arrangement across byte boundaries, either BIG_ENDIAN (aaaaabbb bb000000) or LITTLE_ENDIAN (bbbaaaaa 000000bb)
-
-
Method Detail
-
close
public void close() throws java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Throws:
java.io.IOException
-
clearBitCache
public void clearBitCache()
Clears the cache of bits that have been read from the underlying stream but not yet provided viareadBits(int)
.
-
readBits
public long readBits(int count) throws java.io.IOException
Returns at most 63 bits read from the underlying stream.- Parameters:
count
- the number of bits to read, must be a positive number not bigger than 63.- Returns:
- 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
- Throws:
java.io.IOException
- on error
-
bitsCached
public int bitsCached()
Returns the number of bits that can be read from this input stream without reading from the underlying input stream at all.- Returns:
- estimate of the number of bits that can be read without reading from the underlying stream
- Since:
- 1.16
-
bitsAvailable
public long bitsAvailable() throws java.io.IOException
Returns an estimate of the number of bits that can be read from this input stream without blocking by the next invocation of a method for this input stream.- Returns:
- estimate of the number of bits that can be read without blocking
- Throws:
java.io.IOException
- if the underlying stream throws one when calling available- Since:
- 1.16
-
alignWithByteBoundary
public void alignWithByteBoundary()
Drops bits until the next bits will be read from a byte boundary.- Since:
- 1.16
-
getBytesRead
public long getBytesRead()
Returns the number of bytes read from the underlying stream.This includes the bytes read to fill the current cache and not read as bits so far.
- Returns:
- the number of bytes read from the underlying stream
- Since:
- 1.17
-
processBitsGreater57
private long processBitsGreater57(int count) throws java.io.IOException
- Throws:
java.io.IOException
-
readCachedBits
private long readCachedBits(int count)
-
ensureCache
private boolean ensureCache(int count) throws java.io.IOException
Fills the cache up to 56 bits- Parameters:
count
-- Returns:
- return true, when EOF
- Throws:
java.io.IOException
-
-