org.bolet.jgz
Class DeflaterStateMachine

java.lang.Object
  extended by org.bolet.jgz.DeflaterStateMachine

public final class DeflaterStateMachine
extends java.lang.Object

This class implements a DEFLATE engine as a state machine (deflation process). The machine consumes uncompressed data and outputs compressed data; none of the calls to this class ever blocks.

At any time, the machine has some input data to process, and an optional scheduled flush or terminate action, to be performed when the current input data is completely processed. Only a single action may be scheduled at a time. When the current input data is finished, new data can be input with setInput(). The provided input data is NOT copied internally, hence there is no problem in using large buffers.


Constructor Summary
DeflaterStateMachine(boolean raw)
          Build the state machine instance with default compression parameters.
DeflaterStateMachine(int level, int windowBits, boolean raw)
          Build the state machine instance with the provided compression parameters.
 
Method Summary
 int deflate(byte[] buf)
          Get some compressed data bytes.
 int deflate(byte[] buf, int off, int len)
          Get some compressed data bytes.
 int deflate(java.io.OutputStream out)
          Get some compressed data bytes.
 void finish()
          Finish the stream: the output will be marked as closed at the end of the currently provided input.
 boolean finished()
          Test whether the compression run is finished.
 void flushFull(boolean withData)
          Flush the stream: the output will be flushed (full flush) at the end of the currently provided input.
 void flushPartial()
          Flush the stream: the output will be flushed (partial flush) at the end of the currently provided input.
 void flushSync(boolean withData)
          Flush the stream: the output will be flushed (sync flush) at the end of the currently provided input.
 boolean needsInput()
          Test whether some fresh input data is necessary.
 void reset()
          Reset the machine to its initial state.
 void setDictionary(byte[] dict)
          Set the dictionary.
 void setDictionary(byte[] dict, int off, int len)
          Set the dictionary.
 void setInput(byte[] buf)
          Insert new input data.
 void setInput(byte[] buf, int off, int len)
          Insert new input data.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DeflaterStateMachine

public DeflaterStateMachine(boolean raw)
Build the state machine instance with default compression parameters. If raw is false, then the output data will use the zlib format (RFC 1950); otherwise, it will consist of only the raw sequence of compressed blocks.

Parameters:
raw - true to omit zlib header and footer

DeflaterStateMachine

public DeflaterStateMachine(int level,
                            int windowBits,
                            boolean raw)
Build the state machine instance with the provided compression parameters. If raw is false, then the output data will use the zlib format (RFC 1950); otherwise, it will consist of only the raw sequence of compressed blocks.

Parameters:
level - the compression strategy (1 to 4)
windowBits - the DEFLATE window bit size (9 to 15)
raw - true to omit zlib header and footer
Method Detail

reset

public void reset()
Reset the machine to its initial state.


needsInput

public boolean needsInput()
Test whether some fresh input data is necessary. If this method returns true, then it is guaranteed that deflate() will return 0 until some new input data is added. When true is returned, it is guaranteed that all previously input data has been processed, which means that setInput() can be called safely.

Returns:
true when some input is needed

setDictionary

public void setDictionary(byte[] dict)
Set the dictionary. When using zlib format (RFC 1950), the dictionary must be set before the first call to deflate().

Parameters:
dict - the dictionary

setDictionary

public void setDictionary(byte[] dict,
                          int off,
                          int len)
Set the dictionary. When using zlib format (RFC 1950), the dictionary must be set before the first call to deflate().

Parameters:
dict - the dictionary
off - the dictionary offset
len - the dictionary length

setInput

public void setInput(byte[] buf)
Insert new input data. This call discards any previously inserted data which was not already processed. The provided array is linked in, and must not be modified until either all data was processed, or a new input buffer has been provided.

Parameters:
buf - the new input data

setInput

public void setInput(byte[] buf,
                     int off,
                     int len)
Insert new input data. This call discards any previously inserted data which was not already processed. The provided array is linked in, and must not be modified until either all data was processed, or a new input buffer has been provided.

Parameters:
buf - the new input data
off - the new input data offset
len - the new input data length

finish

public void finish()
Finish the stream: the output will be marked as closed at the end of the currently provided input.


flushPartial

public void flushPartial()
Flush the stream: the output will be flushed (partial flush) at the end of the currently provided input.


flushSync

public void flushSync(boolean withData)
Flush the stream: the output will be flushed (sync flush) at the end of the currently provided input.

Parameters:
withData - false to omit the 00 00 FF FF bytes

flushFull

public void flushFull(boolean withData)
Flush the stream: the output will be flushed (full flush) at the end of the currently provided input.

Parameters:
withData - false to omit the 00 00 FF FF bytes

finished

public boolean finished()
Test whether the compression run is finished. If this method returns true, then the machine will not require any new input and will not output bytes anymore.

Returns:
true on a finished machine

deflate

public int deflate(byte[] buf)
Get some compressed data bytes. The number of actually produced compressed bytes is returned; this is 0 if either the buffer has size 0, or there is no data to return.

Parameters:
buf - the compressed data buffer
Returns:
the number of returned compressed bytes

deflate

public int deflate(byte[] buf,
                   int off,
                   int len)
Get some compressed data bytes. The number of actually produced compressed bytes is returned; this is 0 if either the buffer has size 0, or there is no data to return.

Parameters:
buf - the compressed data buffer
off - the compressed data buffer offset
len - the compressed data buffer length
Returns:
the number of returned compressed bytes

deflate

public int deflate(java.io.OutputStream out)
            throws java.io.IOException
Get some compressed data bytes. The number of actually produced compressed bytes is returned; this is 0 if there is no data to return. When this method is called, all the current input is processed, and the compressed data is written into the out stream.

Parameters:
out - the compressed data destination
Returns:
the number of returned compressed bytes
Throws:
java.io.IOException - on I/O error with the output stream