* @throws IOException If there is an error decompressing the stream.
*/
public void decode( InputStream compressedData, OutputStream result, COSDictionary options ) throws IOException
{
//log.debug("decode( )");
NBitInputStream in = null;
in = new NBitInputStream( compressedData );
in.setBitsInChunk( 9 );
LZWDictionary dic = new LZWDictionary();
byte firstByte = 0;
long nextCommand = 0;
while( (nextCommand = in.read() ) != EOD )
{
// log.debug( "decode - nextCommand=" + nextCommand + ", bitsInChunk: " + in.getBitsInChunk());
if( nextCommand == CLEAR_TABLE )
{
in.setBitsInChunk( 9 );
dic = new LZWDictionary();
}
else
{
byte[] data = dic.getData( nextCommand );
if( data == null )
{
dic.visit( firstByte );
data = dic.getData( nextCommand );
dic.clear();
}
if( data == null )
{
throw new StreamCorruptedException( "Error: data is null" );
}
dic.visit(data);
//log.debug( "decode - dic.getNextCode(): " + dic.getNextCode());
if( dic.getNextCode() >= 2047 )
{
in.setBitsInChunk( 12 );
}
else if( dic.getNextCode() >= 1023 )
{
in.setBitsInChunk( 11 );
}
else if( dic.getNextCode() >= 511 )
{
in.setBitsInChunk( 10 );
}
else
{
in.setBitsInChunk( 9 );
}
/**
if( in.getBitsInChunk() != dic.getCodeSize() )
{
in.unread( nextCommand );