Class BitInputStream

java.lang.Object
org.apache.commons.compress.utils.BitInputStream
All Implemented Interfaces:
Closeable, AutoCloseable

public class BitInputStream extends Object implements Closeable
Reads bits from an InputStream.
Since:
1.10
This class is not thread-safe
  • Constructor Summary Link icon

    Constructors
    Constructor
    Description
    Constructor taking an InputStream and its bit arrangement.
  • Method Summary Link icon

    Modifier and Type
    Method
    Description
    void
    Drops bits until the next bits will be read from a byte boundary.
    long
    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
    Returns the number of bits that can be read from this input stream without reading from the underlying input stream at all.
    void
    Clears the cache of bits that have been read from the underlying stream but not yet provided via readBits(int).
    void
     
    long
    Gets the number of bytes read from the underlying stream.
    int
    Reads and returns the next bit read from the underlying stream.
    long
    readBits(int count)
    Reads and returns at most 63 bits read from the underlying stream.

    Methods inherited from class java.lang.Object Link icon

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details Link icon

    • BitInputStream Link icon

      public BitInputStream(InputStream in, ByteOrder byteOrder)
      Constructor taking an InputStream and its bit arrangement.
      Parameters:
      in - the InputStream
      byteOrder - the bit arrangement across byte boundaries, either BIG_ENDIAN (aaaaabbb bb000000) or LITTLE_ENDIAN (bbbaaaaa 000000bb)
  • Method Details Link icon

    • alignWithByteBoundary Link icon

      public void alignWithByteBoundary()
      Drops bits until the next bits will be read from a byte boundary.
      Since:
      1.16
    • bitsAvailable Link icon

      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:
      estimate of the number of bits that can be read without blocking
      Throws:
      IOException - if the underlying stream throws one when calling available
      Since:
      1.16
    • bitsCached Link icon

      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
    • clearBitCache Link icon

      public void clearBitCache()
      Clears the cache of bits that have been read from the underlying stream but not yet provided via readBits(int).
    • close Link icon

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • getBytesRead Link icon

      public long getBytesRead()
      Gets 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
    • readBit Link icon

      public int readBit() throws IOException
      Reads and returns the next bit read from the underlying stream.
      Returns:
      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
    • readBits Link icon

      public long readBits(int count) throws IOException
      Reads and 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:
      IOException - if an I/O error occurs.