Class BitPackingInputStream

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public class BitPackingInputStream
    extends InputStream
    An input stream that reads individual bits in groups of up to 31 bits at a time. The 31 limit is to allow for negative numbers to still represent EOF. The bits themselves will be read from most to least significant per byte.
    
     |---|---|---|---|---|---|---|---|
     | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 |
     |---|---|---|---|---|---|---|---|
     | a | b | c | d | e | f | g | h |
     | i | j | k | l | m | n | o | p |
     |---|---|---|---|---|---|---|---|
     
    • Constructor Detail

      • BitPackingInputStream

        public BitPackingInputStream​(InputStream in)
        Create a bit packing input stream.
        Parameters:
        in - Input stream to read bits from.
    • Method Detail

      • readBits

        public int readBits​(int bits)
                     throws IOException
        Read a number of bits return as a bit endian integer.
        Parameters:
        bits - Number of bits to read. 1 is same as boolean, 8 same as a byte. Maximum value of 31.
        Returns:
        The read bits. -1 if not enough bit data to read. Will consume bits regardless.
        Throws:
        IOException - If unable to read from stream.
        IllegalArgumentException - If bad number of bits requested.
      • getReadBits

        public int getReadBits()
        Returns:
        The number of bits that have been read from the stream.
      • available

        public int available​(int bits)
                      throws IOException
        Return the number of available chunks of given number of bits.
        Parameters:
        bits - The number of bits per chunk.
        Returns:
        The number of readable chunks.
        Throws:
        IOException - If unable to get available bytes.
      • align

        public void align()
        Ignore the remainder of any partially read byte (octet), and continue to read from the beginning of next byte.