pt.tumba.ngram.compression
Class ArithCodeInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by pt.tumba.ngram.compression.ArithCodeInputStream
All Implemented Interfaces:
java.io.Closeable

public final class ArithCodeInputStream
extends java.io.InputStream

An input stream which uses a statistical model and arithmetic coding for decompression of encoded bytes read from an underlying input stream. Given a statistical model of a byte sequence, it operates in the same way as java.util.zip.GZIPInputStream.

Author:
Bruno Martins
See Also:
ArithCodeOutputStream, ArithCodeModel

Field Summary
private  ArithDecoder _decoder
          The arithmetic decoder used to read bytes.
private  int[] _interval
          Interval used for coding ranges.
private  ArithCodeModel _model
          The statistical model model on which the input stream is based.
private  int _nextByte
          The buffered next byte to write.
 
Constructor Summary
ArithCodeInputStream(ArithDecoder decoder, ArithCodeModel model)
          Construct an arithmetic coded input stream from a specified arithmetic decoder and a statistical model.
ArithCodeInputStream(BitInput in, ArithCodeModel model)
          Construct an arithmetic coded input stream from a specified bit input and a statistical model.
ArithCodeInputStream(java.io.BufferedInputStream in, ArithCodeModel model)
          Construct an arithmetic coded input stream from a specified buffered input stream and a statistical model.
ArithCodeInputStream(java.io.InputStream in, ArithCodeModel model)
          Construct an arithmetic coded input stream from a specified input stream and a statistical model.
 
Method Summary
 int available()
          Returns 1 if there is at least one byte available to be read and returns 0 otherwise.
 void close()
          Closes this input stream.
private  void decodeNextByte()
          Buffers the next byte into _nextByte.
 void mark(int readLimit)
          Not supported.
 boolean markSupported()
          Returns false because marking is not supported.
 int read()
          Reads the next byte from the input stream.
 int read(byte[] bs)
          Read an array of bytes into the specified byte array, returning number of bytes read.
 int read(byte[] bs, int off, int len)
          Read the specified number of bytes into the array, beginning from the position specified by the offset.
 void reset()
          Not supported.
 long skip(long n)
          Skips the given number of bytes from the input.
 
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 statistical model model on which the input stream is based.


_decoder

private final ArithDecoder _decoder
The arithmetic decoder used to read bytes.


_nextByte

private int _nextByte
The buffered next byte to write. If it's equal to -1, the end of stream has been reached, otherwise next byte is the low order bits.


_interval

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

Constructor Detail

ArithCodeInputStream

public ArithCodeInputStream(ArithDecoder decoder,
                            ArithCodeModel model)
                     throws java.io.IOException
Construct an arithmetic coded input stream from a specified arithmetic decoder and a statistical model.

Parameters:
decoder - Arithmetic decoder from which to read input events.
model - Statistical model for arithmetic coding.
Throws:
java.io.IOException - If there is an I/O exception in the underlying input stream.

ArithCodeInputStream

public ArithCodeInputStream(BitInput in,
                            ArithCodeModel model)
                     throws java.io.IOException
Construct an arithmetic coded input stream from a specified bit input and a statistical model.

Parameters:
bitIn - Bit input from which to read bits.
model - Statistical model for arithmetic coding.
Throws:
java.io.IOException - If there is an I/O exception in the underlying input stream.

ArithCodeInputStream

public ArithCodeInputStream(java.io.BufferedInputStream in,
                            ArithCodeModel model)
                     throws java.io.IOException
Construct an arithmetic coded input stream from a specified buffered input stream and a statistical model.

Parameters:
in - Buffered input stream from which to read coded bits.
model - Statistical model for arithmetic coding.
Throws:
java.io.IOException - If there is an I/O exception in the underlying input stream.

ArithCodeInputStream

public ArithCodeInputStream(java.io.InputStream in,
                            ArithCodeModel model)
                     throws java.io.IOException
Construct an arithmetic coded input stream from a specified input stream and a statistical model.

Parameters:
in - Input stream from which to read coded bits.
model - Statistical model for arithmetic coding.
Throws:
java.io.IOException - If there is an I/O exception in the underlying input stream.
Method Detail

available

public int available()
Returns 1 if there is at least one byte available to be read and returns 0 otherwise.

Overrides:
available in class java.io.InputStream
Returns:
1 if at least one byte is available and 0 otherwise.

close

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

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.InputStream
Throws:
java.io.IOException - If there is an exception closing the underlying input stream.

mark

public void mark(int readLimit)
Not supported.

Overrides:
mark in class java.io.InputStream

markSupported

public boolean markSupported()
Returns false because marking is not supported.

Overrides:
markSupported in class java.io.InputStream
Returns:
false.

read

public int read(byte[] bs)
         throws java.io.IOException
Read an array of bytes into the specified byte array, returning number of bytes read.

Overrides:
read in class java.io.InputStream
Parameters:
bs - Byte array into which to read the bytes.
Returns:
Number of bytes read.
Throws:
java.io.IOException - If there is an I/O exception reading from the underlying stream.

read

public int read(byte[] bs,
                int off,
                int len)
         throws java.io.IOException
Read the specified number of bytes into the array, beginning from the position specified by the offset. Return the total number of bytes read. Will be less than array length if the end of stream was encountered.

Overrides:
read in class java.io.InputStream
Parameters:
bs - Byte array into which to read the bytes.
off - Offset into byte array from which to begin writing output.
len - Maximum number of bytes to read.
Returns:
Number of bytes read.
Throws:
java.io.IOException - If there is an I/O exception reading from the underlying stream.

read

public int read()
         throws java.io.IOException
Reads the next byte from the input stream. Returns -1 if end-of-stream is encountered; otherwise result is given in the low order 8 bits of the return value.

Specified by:
read in class java.io.InputStream
Returns:
The next byte from the input stream or -1 if end of stream is encountered.
Throws:
java.io.IOException - If there is an I/O exception reading from the underlying stream.

reset

public void reset()
           throws java.io.IOException
Not supported. Throws an IOException if called.

Overrides:
reset in class java.io.InputStream
Throws:
java.io.IOException - whenever called.

skip

public long skip(long n)
          throws java.io.IOException
Skips the given number of bytes from the input.

Overrides:
skip in class java.io.InputStream
Parameters:
n - Number of bytes to skip.
Returns:
Number of bytes skipped.
Throws:
java.io.IOException - If there is an I/O exception reading from the underlying stream.

decodeNextByte

private void decodeNextByte()
                     throws java.io.IOException
Buffers the next byte into _nextByte.

Throws:
java.io.IOException