Top Description Fields Constructors Methods
org.apache.commons.compress.utils

public Class BitInputStream

extends Object
implements Closeable
Class Inheritance
All Implemented Interfaces
java.io.Closeable, java.lang.AutoCloseable
Known Direct Subclasses
org.apache.commons.compress.archivers.zip.BitStream
Imports
java.io.Closeable, .IOException, .InputStream, java.nio.ByteOrder

Reads bits from an InputStream.
Since
1.10

Field Summary

Modifier and TypeField and Description
private long
private int
private final ByteOrder
private final CountingInputStream
private static final long[]
private static final int

Constructor Summary

AccessConstructor and Description
public
BitInputStream(final InputStream
the InputStream
in
,
final ByteOrder
the bit arrangement across byte boundaries, either BIG_ENDIAN (aaaaabbb bb000000) or LITTLE_ENDIAN (bbbaaaaa 000000bb)
byteOrder
)

Constructor taking an InputStream and its bit arrangement.

Method Summary

Modifier and TypeMethod and Description
public void
alignWithByteBoundary()

Drops bits until the next bits will be read from a byte boundary.

public long

Returns:

estimate of the number of bits that can be read without blocking
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.

public int

Returns:

estimate of the number of bits that can be read without reading from the underlying stream
bitsCached
()

Returns the number of bits that can be read from this input stream without reading from the underlying input stream at all.

public void
clearBitCache()

Clears the cache of bits that have been read from the underlying stream but not yet provided via readBits.

public void
close()

Implements java.io.Closeable.close.

Closes this stream and releases any system resources associated with it.
private boolean

Returns:

return true, when EOF
ensureCache
(final int count)

Fills the cache up to 56 bits

public long

Returns:

the number of bytes read from the underlying stream
getBytesRead
()

Returns the number of bytes read from the underlying stream.

private long
processBitsGreater57(final int count)

public long

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
readBits
(final int
the number of bits to read, must be a positive number not bigger than 63.
count
)

Returns at most 63 bits read from the underlying stream.

private long
readCachedBits(final int count)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

bitsCachedback to summary
private long bitsCached
bitsCachedSizeback to summary
private int bitsCachedSize
byteOrderback to summary
private final ByteOrder byteOrder
inback to summary
private final CountingInputStream in
MASKSback to summary
private static final long[] MASKS
MAXIMUM_CACHE_SIZEback to summary
private static final int MAXIMUM_CACHE_SIZE

Constructor Detail

BitInputStreamback to summary
public BitInputStream(final InputStream in, final ByteOrder byteOrder)

Constructor taking an InputStream and its bit arrangement.

Parameters
in:InputStream

the InputStream

byteOrder:ByteOrder

the bit arrangement across byte boundaries, either BIG_ENDIAN (aaaaabbb bb000000) or LITTLE_ENDIAN (bbbaaaaa 000000bb)

Method Detail

alignWithByteBoundaryback to summary
public void alignWithByteBoundary()

Drops bits until the next bits will be read from a byte boundary.

Since
1.16
bitsAvailableback to summary
public long bitsAvailable() throws 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:long

estimate of the number of bits that can be read without blocking

Exceptions
IOException:
if the underlying stream throws one when calling available
Since
1.16
bitsCachedback to summary
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:int

estimate of the number of bits that can be read without reading from the underlying stream

Since
1.16
clearBitCacheback to summary
public void clearBitCache()

Clears the cache of bits that have been read from the underlying stream but not yet provided via readBits.

closeback to summary
public void close() throws IOException

Implements java.io.Closeable.close.

Doc from java.io.Closeable.close.

Closes this stream and releases any system resources associated with it. If the stream is already closed then invoking this method has no effect.

As noted in AutoCloseable#close(), cases where the close may fail require careful attention. It is strongly advised to relinquish the underlying resources and to internally mark the Closeable as closed, prior to throwing the IOException.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs
ensureCacheback to summary
private boolean ensureCache(final int count) throws IOException

Fills the cache up to 56 bits

Returns:boolean

return true, when EOF

getBytesReadback to summary
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:long

the number of bytes read from the underlying stream

Since
1.17
processBitsGreater57back to summary
private long processBitsGreater57(final int count) throws IOException
readBitsback to summary
public long readBits(final int count) throws IOException

Returns at most 63 bits read from the underlying stream.

Parameters
count:int

the number of bits to read, must be a positive number not bigger than 63.

Returns:long

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

Exceptions
IOException:
on error
readCachedBitsback to summary
private long readCachedBits(final int count)