pt.tumba.ngram.compression
Class ArithCodeOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by pt.tumba.ngram.compression.ArithCodeOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable

public class ArithCodeOutputStream
extends java.io.OutputStream

A filter output stream which uses a statistical model and arithmetic coding for compression of bytes read from an underlying arithmetic encoder. This encoder may be constructed from an output stream or bit output. Given a model and a stream, this class operates in the same way as java.util.zip.GZIPOutputStream.

Author:
Bruno Martins
See Also:
ArithCodeInputStream, ArithCodeModel

Field Summary
private  ArithEncoder _encoder
          The arithmetic encoder used to write coded bytes.
private  int[] _interval
          Interval used for coding ranges.
private  ArithCodeModel _model
          The model on which the output stream is based.
 
Constructor Summary
ArithCodeOutputStream(ArithEncoder encoder, ArithCodeModel model)
          Construct an output stream that writes to the specified output events with the given arithmetic encoder with the given statistical model.
ArithCodeOutputStream(BitOutput bitOut, ArithCodeModel model)
          Construct an output stream that writes to the specified bit output using arithmetic coding with the given statistical model.
ArithCodeOutputStream(java.io.BufferedOutputStream out, ArithCodeModel model)
          Construct an output stream that writes to the specified buffered output stream using arithmetic coding with the given statistical model.
ArithCodeOutputStream(java.io.OutputStream out, ArithCodeModel model)
          Construct an output stream that writes to the specified output stream using arithmetic coding with the given statistical model.
 
Method Summary
 void close()
          Close this output stream.
private  void encode(int symbol)
          Writes encoded symbol after necessary escapes to the underlying encoder.
 void flush()
          Flushes underlying stream.
 void write(byte[] bs)
          Writes array of bytes to the output stream.
 void write(byte[] bs, int off, int len)
          Writes section of array of bytes to the output stream.
 void write(int i)
          Writes the eight low-order bits of argument to the output stream as a byte.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_model

private final ArithCodeModel _model
The model on which the output stream is based.


_encoder

private final ArithEncoder _encoder
The arithmetic encoder used to write coded bytes.


_interval

private final int[] _interval
Interval used for coding ranges.

Constructor Detail

ArithCodeOutputStream

public ArithCodeOutputStream(ArithEncoder encoder,
                             ArithCodeModel model)
Construct an output stream that writes to the specified output events with the given arithmetic encoder with the given statistical model.

Parameters:
encoder - Arithmetic encoder to use for coding output.
model - Statistical model of byte stream.

ArithCodeOutputStream

public ArithCodeOutputStream(BitOutput bitOut,
                             ArithCodeModel model)
Construct an output stream that writes to the specified bit output using arithmetic coding with the given statistical model.

Parameters:
bitOut - Bit output to write coded bits to.
model - Statistical model of byte stream.

ArithCodeOutputStream

public ArithCodeOutputStream(java.io.BufferedOutputStream out,
                             ArithCodeModel model)
Construct an output stream that writes to the specified buffered output stream using arithmetic coding with the given statistical model.

Parameters:
model - Statistical model of byte stream.
out - Buffered output stream to write coded bits to.

ArithCodeOutputStream

public ArithCodeOutputStream(java.io.OutputStream out,
                             ArithCodeModel model)
Construct an output stream that writes to the specified output stream using arithmetic coding with the given statistical model.

Parameters:
output - Output stream to write coded bits to.
model - Statistical model of byte stream.
Method Detail

close

public void close()
           throws java.io.IOException
Close this output stream.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.OutputStream
Throws:
java.io.IOException - If there is an exception in the underlying encoder.

flush

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

Specified by:
flush in interface java.io.Flushable
Overrides:
flush in class java.io.OutputStream
Throws:
java.io.IOException - If there is an exception flushing the underlying stream.

write

public void write(byte[] bs)
           throws java.io.IOException
Writes array of bytes to the output stream.

Overrides:
write in class java.io.OutputStream
Parameters:
bs - Array of bytes to write.
Throws:
java.io.IOException - If there is an exception in writing to the underlying encoder.

write

public void write(byte[] bs,
                  int off,
                  int len)
           throws java.io.IOException
Writes section of array of bytes to the output stream.

Overrides:
write in class java.io.OutputStream
Parameters:
bs - Array of bytes to write.
off - Index from which to start writing.
len - Number of bytes to write.
Throws:
java.io.IOException - If there is an exception in writing to the underlying encoder.

write

public void write(int i)
           throws java.io.IOException
Writes the eight low-order bits of argument to the output stream as a byte.

Specified by:
write in class java.io.OutputStream
Parameters:
i - Bits to write.
Throws:
java.io.IOException - If there is an exception in writing to the underlying encoder.

encode

private void encode(int symbol)
             throws java.io.IOException
Writes encoded symbol after necessary escapes to the underlying encoder.

Parameters:
symbol - Symbol to encode.
Throws:
java.io.IOException - If the underlying encoder throws an IOException.