pt.tumba.ngram.compression
Class BitOutput

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

public final class BitOutput
extends java.lang.Object

Writes to an underlying output stream a bit at a time. A bit is can be input as a boolean, with true=1 and false=0, or as a number, in which case any non-zero input will be converted to 1. If the number of bits written before closing the output does not land on a byte boundary, the remaining fractional byte is filled with 0 bits.

Author:
Bruno Martins
See Also:
BitInput

Field Summary
private  int _nextBitIndex
          The indexof the next bit to write into the next byte.
private  int _nextByte
          Buffering for output.
private  java.io.OutputStream _out
          Underlying output stream.
 
Constructor Summary
BitOutput(java.io.OutputStream out)
          Construct a bit output from the specified output stream.
 
Method Summary
 void close()
          Closes underlying output stream after filling to a byte boundary with 0 bits.
 void flush()
          Flushes the underlying output stream.
private  void reset()
          Resets the bit buffer.
 void writeBit(boolean bit)
          Writes the single specified bit to the underlying output stream, 1 for true and 0 for false.
 void writeBitFalse()
          Writes a single false (0) bit.
 void writeBitTrue()
          Writes a single true (1) bit.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_nextByte

private int _nextByte
Buffering for output. Bytes are represented as integers, primarily for efficiency of bit fiddling and for compatibility with underlying output stream.


_nextBitIndex

private int _nextBitIndex
The indexof the next bit to write into the next byte.


_out

private final java.io.OutputStream _out
Underlying output stream.

Constructor Detail

BitOutput

public BitOutput(java.io.OutputStream out)
Construct a bit output from the specified output stream.

Parameters:
out - Underlying output stream.
Method Detail

close

public void close()
           throws java.io.IOException
Closes underlying output stream after filling to a byte boundary with 0 bits.

Throws:
java.io.IOException - If there is an I/O exception writing the next byte or closing the underlying output stream.

flush

public void flush()
           throws java.io.IOException
Flushes the underlying output stream.

Throws:
java.io.IOException - If there is an exception flushing the underlying output stream.

writeBit

public void writeBit(boolean bit)
              throws java.io.IOException
Writes the single specified bit to the underlying output stream, 1 for true and 0 for false.

Parameters:
bit - Value to write.
Throws:
java.io.IOException - If there is an exception in the underlying output stream.

writeBitTrue

public void writeBitTrue()
                  throws java.io.IOException
Writes a single true (1) bit.

Throws:
java.io.IOException - If there is an exception in the underlying output stream.

writeBitFalse

public void writeBitFalse()
                   throws java.io.IOException
Writes a single false (0) bit.

Throws:
java.io.IOException - If there is an exception in the underlying output stream.

reset

private void reset()
Resets the bit buffer.