Class NonBlockingBitInputStream

  • All Implemented Interfaces:
    Closeable, AutoCloseable
    Direct Known Subclasses:
    BitInputStream

    public class NonBlockingBitInputStream
    extends Object
    implements Closeable
    The NonBlockingBitInputStream allows reading individual bits from a general Java InputStream. Like the various Stream-classes from Java, the BitInputStream has to be created based on another Input stream. It provides a function to read the next bit from the stream, as well as to read multiple bits at once and write the resulting data into an integer value.
    For a thread-safe version see BitInputStream
    Author:
    Andreas Jakl, Philip Helger
    • Constructor Detail

      • NonBlockingBitInputStream

        public NonBlockingBitInputStream​(@Nonnull
                                         InputStream aIS,
                                         @Nonnull
                                         ByteOrder aByteOrder)
        Create a new bit input stream based on an existing Java InputStream.
        Parameters:
        aIS - the input stream this class should read the bits from. May not be null.
        aByteOrder - The non-null byte order to use.
    • Method Detail

      • getByteOrder

        @Nonnull
        public ByteOrder getByteOrder()
        Returns:
        The byte order used. Never null.
      • readBits

        public int readBits​(@Nonnegative
                            int aNumberOfBits)
                     throws IOException
        Read a specified number of bits and return them combined as an integer value. The bits are written to the integer starting at the highest bit ( << aNumberOfBits ), going down to the lowest bit ( << 0 ), so the returned ByteOrder is always LITTLE_ENDIAN!
        Parameters:
        aNumberOfBits - defines how many bits to read from the stream.
        Returns:
        integer value containing the bits read from the stream.
        Throws:
        IOException - In case EOF is reached
      • readBit

        public int readBit()
                    throws IOException
        Read the next bit from the stream.
        Returns:
        0 if the bit is 0, 1 if the bit is 1.
        Throws:
        IOException - In case EOF is reached