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.
    • Constructor Detail

      • BitPackingInputStream

        public BitPackingInputStream​(InputStream in)
    • 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.
      • 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.