pt.tumba.ngram.compression
Class BitInput

java.lang.Object
  extended by pt.tumba.ngram.compression.BitInput

public final class BitInput
extends java.lang.Object

Reads input from an underlying input stream a bit at a time. Bits are returned as booleans, with true=1 and false=0.

Author:
Bruno Martins
See Also:
BitOutput

Field Summary
private  boolean _endOfStream
          Set to true when all bits have been read.
private  java.io.InputStream _in
          Underlying input stream.
private  int _nextBitIndex
          Position of next bit in the buffered byte.
private  int _nextByte
          Buffered byte from which bits are read.
 
Constructor Summary
BitInput(java.io.InputStream in)
          Constructs a bit input from an underlying input stream.
 
Method Summary
 long available()
          Returns number of bits available for reading.
 void close()
          Closes the underlying input stream.
 boolean endOfStream()
          Returns true if all of the available bits have been read.
private  void readAhead()
          Reads the next byte from the input stream into _nextByte.
 boolean readBit()
          Reads the next bit from the input stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_in

private final java.io.InputStream _in
Underlying input stream.


_nextByte

private int _nextByte
Buffered byte from which bits are read.


_nextBitIndex

private int _nextBitIndex
Position of next bit in the buffered byte.


_endOfStream

private boolean _endOfStream
Set to true when all bits have been read.

Constructor Detail

BitInput

public BitInput(java.io.InputStream in)
         throws java.io.IOException
Constructs a bit input from an underlying input stream.

Parameters:
in - Input stream backing this bit input.
Throws:
java.io.IOException - If there is an exception reading from the specified input stream.
Method Detail

available

public long available()
               throws java.io.IOException
Returns number of bits available for reading. Will always be 0 or 1.

Returns:
Number of bits available for reading.
Throws:
java.io.IOException - If there is an exception checking available bytes in the underlying input stream.

close

public void close()
           throws java.io.IOException
Closes the underlying input stream.

Throws:
java.io.IOException - If there is an exception closing the underlying input stream.

endOfStream

public boolean endOfStream()
Returns true if all of the available bits have been read.

Returns:
true if all of the available bits have been read.

readBit

public boolean readBit()
                throws java.io.IOException
Reads the next bit from the input stream. Returns garbage if reading while available() is false.

Returns:
The boolean value of the next bit, true=1, false=0.
Throws:
java.io.IOException - If there is an exception reading a byte from the underlying stream.

readAhead

private void readAhead()
                throws java.io.IOException
Reads the next byte from the input stream into _nextByte.

Throws:
java.io.IOException - If there is an IOException reading from the stream.