Class BitwiseInputStream

java.lang.Object
java.io.InputStream
java.io.FilterInputStream
org.dellroad.stuff.io.BitwiseInputStream
All Implemented Interfaces:
Closeable, AutoCloseable

public class BitwiseInputStream extends FilterInputStream
A bit-oriented InputStream.

Instances support reading arbitrary numbers of individual bits. Bits are read from the underlying InputStream in groups of eight (i.e., whole bytes, obviously), where the bits in each byte are assumed to be ordered from least significant to most significant bit.

Of course, instances also support reading traditional byte-oriented data: any bytes read are handled as if each of the eight bits were read individually, in order from least significant to most significant.

Because the underlying input is byte-oriented, the total number of bits read will always be a multiple of eight. Attempting to read one or more whole bytes when less than eight bits remain will result in EOF being returnd.

See Also:
  • Constructor Details

    • BitwiseInputStream

      public BitwiseInputStream(InputStream in)
      Constructor.
      Parameters:
      in - underlying input
  • Method Details

    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class FilterInputStream
      Throws:
      IOException
    • mark

      public void mark(int readlimit)
      Overrides:
      mark in class FilterInputStream
    • reset

      public void reset() throws IOException
      Overrides:
      reset in class FilterInputStream
      Throws:
      IOException
    • read

      public int read() throws IOException
      Overrides:
      read in class FilterInputStream
      Throws:
      IOException
    • read

      public int read(byte[] buf, int off, int len) throws IOException
      Overrides:
      read in class FilterInputStream
      Throws:
      IOException
    • skip

      public long skip(long remain) throws IOException
      Overrides:
      skip in class FilterInputStream
      Throws:
      IOException
    • readBits

      public BitSet readBits(int len) throws IOException
      Read some number of bits and return them in a BitSet.

      If EOF is encountered before reading len bits, an EOFException is thrown.

      Parameters:
      len - the number of bits to read
      Throws:
      IOException - if an I/O error occurs
      EOFException - if EOF is encountered before len bits can be read
      IllegalArgumentException - if len is negative
    • readBits

      public int readBits(AtomicLong result, int len) throws IOException
      Read up to 64 bits.

      Up to len bits will be read and stored in result; the first bit read will be at index zero, etc. The actual number of bits read is returned, and all higher bits in result will be set to zero.

      If EOF is encountered before reading any bits, -1 is returned.

      Parameters:
      result - value in which to set the bits read (low-order bit first)
      len - the desired number of bits to read
      Returns:
      the number of bits actually read
      Throws:
      IOException - if an I/O error occurs
      IllegalArgumentException - if len is negative or greater than 64
    • bits

      public long bits(int len) throws IOException
      Read up to 64 bits that are expected to be there.

      This will read len bits and return them in a long value. The first bit read will be at index zero, etc. All higher bits will be zero.

      Parameters:
      len - the number of bits to read
      Returns:
      the len bits that were read, starting at bit index zero
      Throws:
      EOFException - if EOF is encountered
      IOException - if an I/O error occurs
      IllegalArgumentException - if len is negative or greater than 64
    • bit

      public boolean bit() throws IOException
      Read a single bit that is expected to be there.
      Returns:
      the bit read
      Throws:
      EOFException - if EOF is encountered
      IOException - if an I/O error occurs
    • readBit

      public int readBit() throws IOException
      Read a single bit, or detect EOF.
      Returns:
      a bit value of 0 or 1, or -1 on EOF
      Throws:
      IOException - if an I/O error occurs
    • bitOffset

      public int bitOffset()
      Get the current bit offset.
      Returns:
      current bit offset (from zero to seven)
    • skipToByteBoundary

      public int skipToByteBoundary() throws IOException
      Discard bits from this input stream up to the next byte boundary.

      If the number of bits read so far is a multiple of eight, this method does nothing. Otherwise it skips up to seven bits so that the next read operation will be byte-aligned.

      Returns:
      the number of bits skipped (from zero to seven)
      Throws:
      IOException - if an I/O error occurs