Class BitInputStream

  • All Implemented Interfaces:
    BitConstants, java.io.Closeable, java.lang.AutoCloseable

    public class BitInputStream
    extends java.io.InputStream
    implements BitConstants
    The BitInputStream allows varying bit sizes to be pulled out of the wrapped InputStream. This is useful for LZW type compression algorithms where 9-12 bit codes are used instead of the 8-bit byte.

    Warning: The read(byte[]) and read(byte[], int, int) methods of InputStream will not work appropriately with any bit size > 8 bits.

    • Constructor Summary

      Constructors 
      Constructor Description
      BitInputStream​(java.io.InputStream is, int startingNumberOfBits)
      Create a BitInputStream wrapping the given InputStream and reading the number of bits specified.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clearRemainingBitsOfData()
      When shifting from buffer to buffer, the input stream also should be reset.
      int getBitMask()
      Answer with the current bit mask for the current bit size.
      void increaseRequestedNumberOfBits()
      Increase the requested number of bits by one.
      int read()
      Read a number of bits off of the wrapped InputStream.
      void setRequestedNumberOfBits​(int numberOfBits)
      Set the number of bits to be read with each call to read().
      • Methods inherited from class java.io.InputStream

        available, close, mark, markSupported, nullInputStream, read, read, readAllBytes, readNBytes, readNBytes, reset, skip, transferTo
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • BitInputStream

        public BitInputStream​(java.io.InputStream is,
                              int startingNumberOfBits)
        Create a BitInputStream wrapping the given InputStream and reading the number of bits specified.
    • Method Detail

      • setRequestedNumberOfBits

        public void setRequestedNumberOfBits​(int numberOfBits)
        Set the number of bits to be read with each call to read().
      • increaseRequestedNumberOfBits

        public void increaseRequestedNumberOfBits()
        Increase the requested number of bits by one. This is the general usage and prevents client from needing to track the requested number of bits or from making various method calls.
      • getBitMask

        public int getBitMask()
        Answer with the current bit mask for the current bit size.
      • read

        public int read()
                 throws java.io.IOException
        Read a number of bits off of the wrapped InputStream.
        Specified by:
        read in class java.io.InputStream
        Throws:
        java.io.IOException
      • clearRemainingBitsOfData

        public void clearRemainingBitsOfData()
        When shifting from buffer to buffer, the input stream also should be reset. This allows the "left over" bits to be cleared.