Package net.sf.jazzlib
Class Deflater
java.lang.Object
net.sf.jazzlib.Deflater
This is the Deflater class. The deflater class compresses input with the
deflate algorithm described in RFC 1951. It has several compression levels
and three different strategies described below.
This class is not thread safe. This is inherent in the API, due to the
split of deflate and setInput.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe best and slowest compression level.static final intThe worst but fastest compression level.private static final intprivate static final intstatic final intThe default compression level.static final intThe default strategy.static final intThe compression method.private DeflaterEngineThe deflater engine.static final intThis strategy will only allow longer string repetitions.private static final intprivate static final intprivate static final intstatic final intThis strategy will not look for string repetitions at all.private static final intprivate static final intprivate static final intprivate static final intprivate intCompression level.static final intThis level won't compress at all but output uncompressed blocks.private final booleanshould we include a header.private DeflaterPendingThe pending output.private static final intprivate intThe current state.private intThe total bytes of output written. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintdeflate(byte[] output) Deflates the current input block to the given array.intdeflate(byte[] output, int offset, int length) Deflates the current input block to the given array.voidend()Deprecated.Just clear all references to deflater instead.protected voidfinalize()Finalizes this object.voidfinish()Finishes the deflater with the current input block.booleanfinished()Returns true iff the stream was finished and no more output bytes are available.(package private) voidflush()Flushes the current input block.intgetAdler()Gets the current adler checksum of the data that was processed so far.intGets the number of input bytes processed so far.intGets the number of output bytes so far.booleanReturns true, if the input buffer is empty.voidreset()Resets the deflater.voidsetDictionary(byte[] dict) Sets the dictionary which should be used in the deflate process.voidsetDictionary(byte[] dict, int offset, int length) Sets the dictionary which should be used in the deflate process.voidsetInput(byte[] input) Sets the data which should be compressed next.voidsetInput(byte[] input, int off, int len) Sets the data which should be compressed next.voidsetLevel(int lvl) Sets the compression level.voidsetStrategy(int stgy) Sets the compression strategy.
-
Field Details
-
BEST_COMPRESSION
public static final int BEST_COMPRESSIONThe best and slowest compression level. This tries to find very long and distant string repetitions.- See Also:
-
BEST_SPEED
public static final int BEST_SPEEDThe worst but fastest compression level.- See Also:
-
DEFAULT_COMPRESSION
public static final int DEFAULT_COMPRESSIONThe default compression level.- See Also:
-
NO_COMPRESSION
public static final int NO_COMPRESSIONThis level won't compress at all but output uncompressed blocks.- See Also:
-
DEFAULT_STRATEGY
public static final int DEFAULT_STRATEGYThe default strategy.- See Also:
-
FILTERED
public static final int FILTEREDThis strategy will only allow longer string repetitions. It is useful for random data with a small character set.- See Also:
-
HUFFMAN_ONLY
public static final int HUFFMAN_ONLYThis strategy will not look for string repetitions at all. It only encodes with Huffman trees (which means, that more common characters get a smaller encoding.- See Also:
-
DEFLATED
public static final int DEFLATEDThe compression method. This is the only method supported so far. There is no need to use this constant at all.- See Also:
-
IS_SETDICT
private static final int IS_SETDICT- See Also:
-
IS_FLUSHING
private static final int IS_FLUSHING- See Also:
-
IS_FINISHING
private static final int IS_FINISHING- See Also:
-
INIT_STATE
private static final int INIT_STATE- See Also:
-
SETDICT_STATE
private static final int SETDICT_STATE- See Also:
-
BUSY_STATE
private static final int BUSY_STATE- See Also:
-
FLUSHING_STATE
private static final int FLUSHING_STATE- See Also:
-
FINISHING_STATE
private static final int FINISHING_STATE- See Also:
-
FINISHED_STATE
private static final int FINISHED_STATE- See Also:
-
CLOSED_STATE
private static final int CLOSED_STATE- See Also:
-
level
private int levelCompression level. -
noHeader
private final boolean noHeadershould we include a header. -
state
private int stateThe current state. -
totalOut
private int totalOutThe total bytes of output written. -
pending
The pending output. -
engine
The deflater engine.
-
-
Constructor Details
-
Deflater
public Deflater()Creates a new deflater with default compression level. -
Deflater
public Deflater(int lvl) Creates a new deflater with given compression level.- Parameters:
lvl- the compression level, a value between NO_COMPRESSION and BEST_COMPRESSION, or DEFAULT_COMPRESSION.- Throws:
IllegalArgumentException- if lvl is out of range.
-
Deflater
public Deflater(int lvl, boolean nowrap) Creates a new deflater with given compression level.- Parameters:
lvl- the compression level, a value between NO_COMPRESSION and BEST_COMPRESSION.nowrap- true, iff we should suppress the deflate header at the beginning and the adler checksum at the end of the output. This is useful for the GZIP format.- Throws:
IllegalArgumentException- if lvl is out of range.
-
-
Method Details
-
reset
public void reset()Resets the deflater. The deflater acts afterwards as if it was just created with the same compression level and strategy as it had before. -
end
Deprecated.Just clear all references to deflater instead.Frees all objects allocated by the compressor. There's no reason to call this, since you can just rely on garbage collection. Exists only for compatibility against Sun's JDK, where the compressor allocates native memory. If you call any method (even reset) afterwards the behaviour is undefined. -
getAdler
public int getAdler()Gets the current adler checksum of the data that was processed so far. -
getTotalIn
public int getTotalIn()Gets the number of input bytes processed so far. -
getTotalOut
public int getTotalOut()Gets the number of output bytes so far. -
finalize
protected void finalize()Finalizes this object. -
flush
void flush()Flushes the current input block. Further calls to deflate() will produce enough output to inflate everything in the current input block. This is not part of Sun's JDK so I have made it package private. It is used by DeflaterOutputStream to implement flush(). -
finish
public void finish()Finishes the deflater with the current input block. It is an error to give more input after this method was called. This method must be called to force all bytes to be flushed. -
finished
public boolean finished()Returns true iff the stream was finished and no more output bytes are available. -
needsInput
public boolean needsInput()Returns true, if the input buffer is empty. You should then call setInput().
NOTE: This method can also return true when the stream was finished. -
setInput
public void setInput(byte[] input) Sets the data which should be compressed next. This should be only called when needsInput indicates that more input is needed. If you call setInput when needsInput() returns false, the previous input that is still pending will be thrown away. The given byte array should not be changed, before needsInput() returns true again. This call is equivalent tosetInput(input, 0, input.length).- Parameters:
input- the buffer containing the input data.- Throws:
IllegalStateException- if the buffer was finished() or ended().
-
setInput
public void setInput(byte[] input, int off, int len) Sets the data which should be compressed next. This should be only called when needsInput indicates that more input is needed. The given byte array should not be changed, before needsInput() returns true again.- Parameters:
input- the buffer containing the input data.off- the start of the data.len- the length of the data.- Throws:
IllegalStateException- if the buffer was finished() or ended() or if previous input is still pending.
-
setLevel
public void setLevel(int lvl) Sets the compression level. There is no guarantee of the exact position of the change, but if you call this when needsInput is true the change of compression level will occur somewhere near before the end of the so far given input.- Parameters:
lvl- the new compression level.
-
setStrategy
public void setStrategy(int stgy) Sets the compression strategy. Strategy is one of DEFAULT_STRATEGY, HUFFMAN_ONLY and FILTERED. For the exact position where the strategy is changed, the same as for setLevel() applies.- Parameters:
stgy- the new compression strategy.
-
deflate
public int deflate(byte[] output) Deflates the current input block to the given array. It returns the number of bytes compressed, or 0 if either needsInput() or finished() returns true or length is zero.- Parameters:
output- the buffer where to write the compressed data.
-
deflate
public int deflate(byte[] output, int offset, int length) Deflates the current input block to the given array. It returns the number of bytes compressed, or 0 if either needsInput() or finished() returns true or length is zero.- Parameters:
output- the buffer where to write the compressed data.offset- the offset into the output array.length- the maximum number of bytes that may be written.- Throws:
IllegalStateException- if end() was called.IndexOutOfBoundsException- if offset and/or length don't match the array length.
-
setDictionary
public void setDictionary(byte[] dict) Sets the dictionary which should be used in the deflate process. This call is equivalent tosetDictionary(dict, 0, dict.length).- Parameters:
dict- the dictionary.- Throws:
IllegalStateException- if setInput () or deflate () were already called or another dictionary was already set.
-
setDictionary
public void setDictionary(byte[] dict, int offset, int length) Sets the dictionary which should be used in the deflate process. The dictionary should be a byte array containing strings that are likely to occur in the data which should be compressed. The dictionary is not stored in the compressed output, only a checksum. To decompress the output you need to supply the same dictionary again.- Parameters:
dict- the dictionary.offset- an offset into the dictionary.length- the length of the dictionary.- Throws:
IllegalStateException- if setInput () or deflate () were already called or another dictionary was already set.
-