jwo.utils.gifutils
Class BitInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by java.io.FilterInputStream
          extended by jwo.utils.gifutils.BitInputStream
All Implemented Interfaces:
Closeable
Direct Known Subclasses:
GIFInputStream

public class BitInputStream
extends FilterInputStream

Allows the reading of 1 to 32 bits at a time, on any bit boundary. Bits are read from bytes, least significant bit first.

Version:
2.3, 22nd August, 1998, modified 11th April, 2006.
Author:
Benjamin E. Norman, Minor modificiations, Jo Wood.

Field Summary
protected  long cachedBits
          Stores any cached bits which have been read in from the underlying stream but not yet returned to the user
protected  int numCachedBits
          Number of valid bits in 'cachedBits'.
 
Fields inherited from class java.io.FilterInputStream
in
 
Constructor Summary
BitInputStream(InputStream is)
          Creates a BitInputStream, assuming that at least 5 bytes are available on the underlying stream.
 
Method Summary
 int read()
          Reads a byte at the current bit boundary.
 int readBit()
          Reads the next bit from the underlying input stream.
 int readBits(int numBits)
          Reads the given number of bits (1 to 32) from the underlying input stream.
 
Methods inherited from class java.io.FilterInputStream
available, close, mark, markSupported, read, read, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

cachedBits

protected long cachedBits
Stores any cached bits which have been read in from the underlying stream but not yet returned to the user


numCachedBits

protected int numCachedBits
Number of valid bits in 'cachedBits'. This should always be >= 32 for efficiency, unless EOF has been reached

Constructor Detail

BitInputStream

public BitInputStream(InputStream is)
Creates a BitInputStream, assuming that at least 5 bytes are available on the underlying stream.

Parameters:
is - Underlying input stream upon which to base this one.
Method Detail

read

public int read()
         throws IOException
Reads a byte at the current bit boundary.

Overrides:
read in class FilterInputStream
Returns:
The byte that has been read or -1 if 8 bits are not available before EOF is reached.
Throws:
IOException - If problem reading from input stream.

readBits

public int readBits(int numBits)
             throws EOFException,
                    IllegalArgumentException,
                    IOException
Reads the given number of bits (1 to 32) from the underlying input stream.

Parameters:
numBits - Number of bits to read.
Returns:
Data read from input stream.
Throws:
EOFException - if the given number of bits are not available before EOF.
IllegalArgumentException - if numBits is not between 1 and 32.
IOException - if problem reading bits from input stream.

readBit

public int readBit()
            throws IOException
Reads the next bit from the underlying input stream.

Returns:
The read data or -1 on EOF.
Throws:
IOException - if problem reading bits from input stream.


Copyright Jo Wood, 1996-2009, last modified, 17th April, 2009