public class BitInputStream
extends java.lang.Object
implements java.io.Closeable
Modifier and Type | Field and 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 and Description |
---|
BitInputStream(java.io.InputStream in,
java.nio.ByteOrder byteOrder)
Constructor taking an InputStream and its bit arrangement.
|
Modifier and Type | Method and 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 via
readBits(int) . |
void |
close() |
private boolean |
ensureCache(int count)
Fills the cache up to 56 bits
|
long |
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) |
private static final int MAXIMUM_CACHE_SIZE
private static final long[] MASKS
private final CountingInputStream in
private final java.nio.ByteOrder byteOrder
private long bitsCached
private int bitsCachedSize
public BitInputStream(java.io.InputStream in, java.nio.ByteOrder byteOrder)
in
- the InputStreambyteOrder
- the bit arrangement across byte boundaries,
either BIG_ENDIAN (aaaaabbb bb000000) or LITTLE_ENDIAN (bbbaaaaa 000000bb)public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
java.io.IOException
public void clearBitCache()
readBits(int)
.public long readBits(int count) throws java.io.IOException
count
- the number of bits to read, must be a positive
number not bigger than 63.java.io.IOException
- on errorpublic int bitsCached()
public long bitsAvailable() throws java.io.IOException
java.io.IOException
- if the underlying stream throws one when calling availablepublic void alignWithByteBoundary()
public long getBytesRead()
This includes the bytes read to fill the current cache and not read as bits so far.
private long processBitsGreater57(int count) throws java.io.IOException
java.io.IOException
private long readCachedBits(int count)
private boolean ensureCache(int count) throws java.io.IOException
count
- java.io.IOException