org.bolet.jgz
Class Deflater

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

public final class Deflater
extends java.lang.Object

This class implements the core DEFLATE process, i.e. the compression of data into DEFLATE blocks.


Field Summary
static int COMPACT
          Compression level 4: try to achieve best compression ratio, possibly at the expense of compression speed.
static int HUFF
          Compression level 1: do not apply LZ77; only Huffman codes are computed.
static int MEDIUM
          Compression level 3: compromise between speed and compression ratio.
static int SPEED
          Compression level 2: optimize for speed.
 
Constructor Summary
Deflater()
          Build a deflater with the default parameters (MEDIUM level, 15-bit window).
Deflater(int level)
          Build a deflater with the provided compression strategy (either SPEED, MEDIUM or COMPACT).
Deflater(int level, int windowBits)
          Build a deflater with the provided compression strategy (either SPEED, MEDIUM or COMPACT) and the provided window size.
 
Method Summary
 void flush()
          If there is pending buffered data, compress it now, and write the block on the transport stream.
 void flushFull(boolean withData)
          Perform a "full flush".
 void flushPartial()
          Perform a "partial flush" in a way similar to what is done by zlib with option Z_PARTIAL_FLUSH.
 void flushSync(boolean withData)
          Perform a "sync flush" in a way similar to what is done by zlib with option Z_SYNC_FLUSH.
 java.io.OutputStream getOut()
          Get the current output transport stream.
 void process(byte[] buf, int off, int len)
          Process some more data.
 void processDictionary(byte[] dict)
          Process a dictionary.
 void processDictionary(byte[] dict, int off, int len)
          Process a dictionary.
 void processDictionary(java.io.InputStream dict)
          Process a dictionary.
 void reset()
          Reset the internal tables.
 void setOut(java.io.OutputStream out)
          Set the current output transport stream.
 void terminate()
          Terminate the current compression run.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HUFF

public static final int HUFF
Compression level 1: do not apply LZ77; only Huffman codes are computed. This is faster than SPEED but with a degraded compression ratio.

See Also:
Constant Field Values

SPEED

public static final int SPEED
Compression level 2: optimize for speed.

See Also:
Constant Field Values

MEDIUM

public static final int MEDIUM
Compression level 3: compromise between speed and compression ratio. This is the default level.

See Also:
Constant Field Values

COMPACT

public static final int COMPACT
Compression level 4: try to achieve best compression ratio, possibly at the expense of compression speed. This level may occasionaly yield slightly worse results than the MEDIUM level.

See Also:
Constant Field Values
Constructor Detail

Deflater

public Deflater()
Build a deflater with the default parameters (MEDIUM level, 15-bit window).


Deflater

public Deflater(int level)
Build a deflater with the provided compression strategy (either SPEED, MEDIUM or COMPACT). A standard 15-bit window is used. If the compression level is 0, then the default compression level is selected.

Parameters:
level - the compression strategy

Deflater

public Deflater(int level,
                int windowBits)
Build a deflater with the provided compression strategy (either SPEED, MEDIUM or COMPACT) and the provided window size. The window size is expressed in bits and may range from 9 to 15 (inclusive). The DEFLATE format uses a 15-bit window; by using a smaller window, the produced stream can be inflated with less memory (but compression ratio is lowered). If the compression level is 0, then the default compression level is selected.

Parameters:
level - the compression strategy
windowBits - the window bit size (9 to 15)
Method Detail

reset

public void reset()
Reset the internal tables. This shall be called before reusing a Deflater instance for a new compression run. This method aborts the current compression, if any, without flushing any buffered data.


getOut

public java.io.OutputStream getOut()
Get the current output transport stream.

Returns:
the current output stream

setOut

public void setOut(java.io.OutputStream out)
Set the current output transport stream. This method must be called before compressing data.

Parameters:
out - the new transport stream

processDictionary

public void processDictionary(java.io.InputStream dict)
                       throws java.io.IOException

Process a dictionary. The provided stream is read, and all bytes are used for subsequent LZ77 compression. The dictionary stream is read until its end.

A dictionary may be input only before processing any real data (or immediately after a reset()).

Parameters:
dict - the dictionary stream
Throws:
java.io.IOException - on I/O error with the dictionary stream

processDictionary

public void processDictionary(byte[] dict)

Process a dictionary. The dictionary is a bunch of bytes which are considered as previously encountered input data bytes, and can be used for further LZ77 compression.

A dictionary may be input only before processing any real data (or immediately after a reset()).

Parameters:
dict - the dictionary

processDictionary

public void processDictionary(byte[] dict,
                              int off,
                              int len)

Process a dictionary. The dictionary is a bunch of bytes which are considered as previously encountered input data bytes, and can be used for further LZ77 compression.

A dictionary may be input only before processing any real data (or immediately after a reset()).

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

process

public void process(byte[] buf,
                    int off,
                    int len)
             throws java.io.IOException
Process some more data. When the internal buffer is full, or when the compressor code deems it appropriate, the data is compressed and sent on the transport stream. This method is most efficient when data is input by big enough chunks (at least a dozen bytes per call).

Parameters:
buf - the data buffer
off - the data offset
len - the data length (in bytes)
Throws:
java.io.IOException - on I/O error with the transport stream

flush

public void flush()
           throws java.io.IOException
If there is pending buffered data, compress it now, and write the block on the transport stream. Note that a partial byte may still remain unwritten. The transport stream is NOT flushed.

Throws:
java.io.IOException - on I/O error with the transport stream

terminate

public void terminate()
               throws java.io.IOException
Terminate the current compression run. Pending buffered data, if any, is compressed as a final block, and written out on the transport stream. If there is no pending buffered data, then an empty, final block is added. Either way, any remaining partial byte is padded with zeroes and written. The transport stream is NOT flushed.

Throws:
java.io.IOException - on I/O error with the transport stream

flushPartial

public void flushPartial()
                  throws java.io.IOException
Perform a "partial flush" in a way similar to what is done by zlib with option Z_PARTIAL_FLUSH. The current block, if any, is closed, and one or two empty type 1 blocks are added. All complete bytes are sent on the transport stream. The transport stream is NOT flushed.

Throws:
java.io.IOException - on I/O error with the transport stream

flushSync

public void flushSync(boolean withData)
               throws java.io.IOException
Perform a "sync flush" in a way similar to what is done by zlib with option Z_SYNC_FLUSH. The current block, if any, is closed, and one empty type 0 block is added. After this call, the stream is byte-aligned. The type 0 block ends with the aligned four-byte sequence 00 00 FF FF; these four bytes are omitted if withData is false. The transport stream is NOT flushed.

Parameters:
withData - false to omit the 00 00 FF FF bytes
Throws:
java.io.IOException - on I/O error with the transport stream

flushFull

public void flushFull(boolean withData)
               throws java.io.IOException
Perform a "full flush". This is a "sync flush" where the current dictionary is emptied. This corresponds to zlib's option Z_FULL_FLUSH. The transport stream is NOT flushed.

Parameters:
withData - false to omit the 00 00 FF FF bytes
Throws:
java.io.IOException - on I/O error with the transport stream