APIdia Launch Partner
Top Description Inners Fields Constructors Methods
java.util.zip

public Class Deflater

extends Object
implements AutoCloseable
Class Inheritance
All Implemented Interfaces
java.lang.AutoCloseable
Imports
java.lang.ref.Cleaner.Cleanable, java.nio.ByteBuffer, .ReadOnlyBufferException, java.util.Objects, jdk.internal.ref.CleanerFactory, jdk.internal.util.Preconditions

This class provides support for general purpose compression using the popular ZLIB compression library. The ZLIB compression library was initially developed as part of the PNG graphics standard and is not protected by patents. It is fully described in the specifications at the java.util.zip package description.

Unless otherwise noted, passing a null argument to a method in this class will cause a NullPointerException to be thrown.

This class deflates sequences of bytes into ZLIB compressed data format. The input byte sequence is provided in either a byte array or a ByteBuffer, via one of the setInput() methods. The output byte sequence is written to the output byte array or ByteBuffer passed to the deflate() methods.

To release the resources used by a Deflater, an application must close it by invoking its end() or close() method.

API Note

This class implements AutoCloseable to facilitate its usage with try-with-resources statement. The close() method simply calls end().

The following code fragment demonstrates a trivial compression and decompression of a string using Deflater and Inflater.

// Encode a String into bytes String inputString = "blahblahblah€€"; byte[] input = inputString.getBytes(StandardCharsets.UTF_8); // Compress the bytes ByteArrayOutputStream compressedBaos = new ByteArrayOutputStream(); try (Deflater compressor = new Deflater()) { compressor.setInput(input); // Let the compressor know that the complete input // has been made available compressor.finish(); // Keep compressing the input till the compressor // is finished compressing while (!compressor.finished()) { // Use some reasonable size for the temporary buffer // based on the data being compressed byte[] tmpBuffer = new byte[100]; int numCompressed = compressor.deflate(tmpBuffer); // Copy over the compressed bytes from the temporary // buffer into the final byte array compressedBaos.write(tmpBuffer, 0, numCompressed); } } // Decompress the bytes ByteArrayOutputStream decompressedBaos = new ByteArrayOutputStream(); try (Inflater decompressor = new Inflater()) { byte[] compressed = compressedBaos.toByteArray(); decompressor.setInput(compressed, 0, compressed.length); while (!decompressor.finished()) { // Use some reasonable size for the temporary buffer, // based on the data being decompressed; in this example, // we use a small buffer size byte[] tmpBuffer = new byte[100]; int numDecompressed = 0; try { numDecompressed = decompressor.inflate(tmpBuffer); } catch (DataFormatException dfe) { // Handle the exception suitably, in this example // we just rethrow it throw new RuntimeException(dfe); } // Copy over the decompressed bytes from the temporary // buffer into the final byte array decompressedBaos.write(tmpBuffer, 0, numDecompressed); } } // Decode the bytes into a String String outputString = decompressedBaos.toString(StandardCharsets.UTF_8);
// Encode a String into bytes
String inputString = "blahblahblah\u20AC\u20AC";
byte[] input = inputString.getBytes(StandardCharsets.UTF_8);

// Compress the bytes
ByteArrayOutputStream compressedBaos = new ByteArrayOutputStream();
try (Deflater compressor = new Deflater()) {
    compressor.setInput(input);
    // Let the compressor know that the complete input
    // has been made available
    compressor.finish();
    // Keep compressing the input till the compressor
    // is finished compressing
    while (!compressor.finished()) {
        // Use some reasonable size for the temporary buffer
        // based on the data being compressed
        byte[] tmpBuffer = new byte[100];
        int numCompressed = compressor.deflate(tmpBuffer);
        // Copy over the compressed bytes from the temporary
        // buffer into the final byte array
        compressedBaos.write(tmpBuffer, 0, numCompressed);
    }
}

// Decompress the bytes
ByteArrayOutputStream decompressedBaos = new ByteArrayOutputStream();
try (Inflater decompressor = new Inflater()) {
    byte[] compressed = compressedBaos.toByteArray();
    decompressor.setInput(compressed, 0, compressed.length);
    while (!decompressor.finished()) {
        // Use some reasonable size for the temporary buffer,
        // based on the data being decompressed; in this example,
        // we use a small buffer size
        byte[] tmpBuffer = new byte[100];
        int numDecompressed = 0;
        try {
            numDecompressed = decompressor.inflate(tmpBuffer);
        } catch (DataFormatException dfe) {
            // Handle the exception suitably, in this example
            // we just rethrow it
            throw new RuntimeException(dfe);
        }
        // Copy over the decompressed bytes from the temporary
        // buffer into the final byte array
        decompressedBaos.write(tmpBuffer, 0, numDecompressed);
    }
}
// Decode the bytes into a String
String outputString = decompressedBaos.toString(StandardCharsets.UTF_8);
Author
David Connelly
Since
1.1
See Also
Inflater

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
Deflater.DeflaterZStreamRef

A reference to the native zlib's z_stream structure.

Field Summary

Modifier and TypeField and Description
public static final int
BEST_COMPRESSION

Compression level for best compression.

public static final int
BEST_SPEED

Compression level for fastest compression.

private long
private long
public static final int
DEFAULT_COMPRESSION

Default compression level.

public static final int
DEFAULT_STRATEGY

Default compression strategy.

public static final int
DEFLATED

Compression method for the deflate algorithm (the only one currently supported).

public static final int
FILTERED

Compression strategy best used for data consisting mostly of small values with a somewhat random distribution.

private boolean
private static final int
FINISH

Flush mode to use at the end of output.

private boolean
public static final int
FULL_FLUSH

Compression flush mode used to flush out all pending output and reset the deflater.

public static final int
HUFFMAN_ONLY

Compression strategy for Huffman coding only.

private ByteBuffer
private byte[]
private int
private int
private int
public static final int
NO_COMPRESSION

Compression level for no compression.

public static final int
NO_FLUSH

Compression flush mode used to achieve best compression result.

private boolean
private int
public static final int
SYNC_FLUSH

Compression flush mode used to flush out all pending output; may degrade compression for some compression algorithms.

private final Deflater.DeflaterZStreamRef

Constructor Summary

AccessConstructor and Description
public
Deflater(int
the compression level (0-9)
level
,
boolean
if true then use GZIP compatible compression
nowrap
)

Creates a new compressor using the specified compression level.

public
Deflater(int
the compression level (0-9)
level
)

Creates a new compressor using the specified compression level.

public
Deflater()

Creates a new compressor with the default compression level.

Method Summary

Modifier and TypeMethod and Description
public void
close()

Implements java.lang.AutoCloseable.close.

Closes and releases the resources held by this Deflater and discards any unprocessed input.

public int

Returns:

the actual number of bytes of compressed data written to the output buffer
deflate
(byte[]
the buffer for the compressed data
output
,
int
the start offset of the data
off
,
int
the maximum number of bytes of compressed data
len
)

Compresses the input data and fills specified buffer with compressed data.

public int

Returns:

the actual number of bytes of compressed data written to the output buffer
deflate
(byte[]
the buffer for the compressed data
output
)

Compresses the input data and fills specified buffer with compressed data.

public int

Returns:

the actual number of bytes of compressed data written to the output buffer
deflate
(ByteBuffer
the buffer for the compressed data
output
)

Compresses the input data and fills specified buffer with compressed data.

public int

Returns:

the actual number of bytes of compressed data written to the output buffer
deflate
(byte[]
the buffer for the compressed data
output
,
int
the start offset of the data
off
,
int
the maximum number of bytes of compressed data
len
,
int
the compression flush mode
flush
)

Compresses the input data and fills the specified buffer with compressed data.

public int

Returns:

the actual number of bytes of compressed data written to the output buffer
deflate
(ByteBuffer
the buffer for the compressed data
output
,
int
the compression flush mode
flush
)

Compresses the input data and fills the specified buffer with compressed data.

private native long
deflateBufferBuffer(long addr, long inputAddress, int inputLen, long outputAddress, int outputLen, int flush, int params)

private native long
deflateBufferBytes(long addr, long inputAddress, int inputLen, byte[] outputArray, int outputOff, int outputLen, int flush, int params)

private native long
deflateBytesBuffer(long addr, byte[] inputArray, int inputOff, int inputLen, long outputAddress, int outputLen, int flush, int params)

private native long
deflateBytesBytes(long addr, byte[] inputArray, int inputOff, int inputLen, byte[] outputArray, int outputOff, int outputLen, int flush, int params)

public void
end()

Closes and releases the resources held by this Deflater and discards any unprocessed input.

private static native void
end(long addr)

private void
public void
finish()

When called, indicates that compression should end with the current contents of the input buffer.

public boolean

Returns:

true if the end of the compressed data output stream has been reached
finished
()

Returns true if the end of the compressed data output stream has been reached.

public int

Returns:

the ADLER-32 value of the uncompressed data
getAdler
()

Returns the ADLER-32 value of the uncompressed data.

private static native int
getAdler(long addr)

public long

Returns:

the total (non-negative) number of uncompressed bytes input so far
getBytesRead
()

Returns the total number of uncompressed bytes input so far.

public long

Returns:

the total (non-negative) number of compressed bytes output so far
getBytesWritten
()

Returns the total number of compressed bytes output so far.

public int

Returns:

the total number of uncompressed bytes input so far
getTotalIn
()
Deprecated since 23. Use getBytesRead() instead

Returns the total number of uncompressed bytes input so far.

public int

Returns:

the total number of compressed bytes output so far
getTotalOut
()
Deprecated since 23. Use getBytesWritten() instead

Returns the total number of compressed bytes output so far.

private static native long
init(int level, int strategy, boolean nowrap)

public boolean

Returns:

true if the input data buffer is empty and setInput() should be called in order to provide more input
needsInput
()

Returns true if no data remains in the input buffer.

public void
reset()

Resets deflater so that a new set of input data can be processed.

private static native void
reset(long addr)

public void
setDictionary(byte[]
the dictionary data bytes
dictionary
,
int
the start offset of the data
off
,
int
the length of the data
len
)

Sets preset dictionary for compression.

public void
setDictionary(byte[]
the dictionary data bytes
dictionary
)

Sets preset dictionary for compression.

public void
setDictionary(ByteBuffer
the dictionary data bytes
dictionary
)

Sets preset dictionary for compression.

private static native void
setDictionary(long addr, byte[] b, int off, int len)

private static native void
setDictionaryBuffer(long addr, long bufAddress, int len)

public void
setInput(byte[]
the input data bytes
input
,
int
the start offset of the data
off
,
int
the length of the data
len
)

Sets input data for compression.

public void
setInput(byte[]
the input data bytes
input
)

Sets input data for compression.

public void
setInput(ByteBuffer
the input data bytes
input
)

Sets input data for compression.

public void
setLevel(int
the new compression level (0-9)
level
)

Sets the compression level to the specified value.

public void
setStrategy(int
the new compression strategy
strategy
)

Sets the compression strategy to the specified value.

pack-priv boolean
shouldFinish()

Returns the value of 'finish' flag.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

BEST_COMPRESSIONback to summary
public static final int BEST_COMPRESSION

Compression level for best compression.

BEST_SPEEDback to summary
public static final int BEST_SPEED

Compression level for fastest compression.

bytesReadback to summary
private long bytesRead
bytesWrittenback to summary
private long bytesWritten
DEFAULT_COMPRESSIONback to summary
public static final int DEFAULT_COMPRESSION

Default compression level.

DEFAULT_STRATEGYback to summary
public static final int DEFAULT_STRATEGY

Default compression strategy.

DEFLATEDback to summary
public static final int DEFLATED

Compression method for the deflate algorithm (the only one currently supported).

FILTEREDback to summary
public static final int FILTERED

Compression strategy best used for data consisting mostly of small values with a somewhat random distribution. Forces more Huffman coding and less string matching.

finishback to summary
private boolean finish
FINISHback to summary
private static final int FINISH

Flush mode to use at the end of output. Can only be provided by the user by way of finish().

finishedback to summary
private boolean finished
FULL_FLUSHback to summary
public static final int FULL_FLUSH

Compression flush mode used to flush out all pending output and reset the deflater. Using this mode too often can seriously degrade compression.

Since
1.7
See Also
Deflater#deflate(byte[], int, int, int)
HUFFMAN_ONLYback to summary
public static final int HUFFMAN_ONLY

Compression strategy for Huffman coding only.

inputback to summary
private ByteBuffer input
inputArrayback to summary
private byte[] inputArray
inputLimback to summary
private int inputLim
inputPosback to summary
private int inputPos
levelback to summary
private int level
NO_COMPRESSIONback to summary
public static final int NO_COMPRESSION

Compression level for no compression.

NO_FLUSHback to summary
public static final int NO_FLUSH

Compression flush mode used to achieve best compression result.

Since
1.7
See Also
Deflater#deflate(byte[], int, int, int)
setParamsback to summary
private boolean setParams
strategyback to summary
private int strategy
SYNC_FLUSHback to summary
public static final int SYNC_FLUSH

Compression flush mode used to flush out all pending output; may degrade compression for some compression algorithms.

Since
1.7
See Also
Deflater#deflate(byte[], int, int, int)
zsRefback to summary
private final Deflater.DeflaterZStreamRef zsRef

Constructor Detail

Deflaterback to summary
public Deflater(int level, boolean nowrap)

Creates a new compressor using the specified compression level. If 'nowrap' is true then the ZLIB header and checksum fields will not be used in order to support the compression format used in both GZIP and PKZIP.

Parameters
level:int

the compression level (0-9)

nowrap:boolean

if true then use GZIP compatible compression

Annotations
@SuppressWarnings:this-escape
Deflaterback to summary
public Deflater(int level)

Creates a new compressor using the specified compression level. Compressed data will be generated in ZLIB format.

Parameters
level:int

the compression level (0-9)

Deflaterback to summary
public Deflater()

Creates a new compressor with the default compression level. Compressed data will be generated in ZLIB format.

Method Detail

closeback to summary
public void close()

Implements java.lang.AutoCloseable.close.

Closes and releases the resources held by this Deflater and discards any unprocessed input.

Implementation Specification

This method calls the end() method.

Annotations
@Override
Since
25
deflateback to summary
public int deflate(byte[] output, int off, int len)

Compresses the input data and fills specified buffer with compressed data. Returns actual number of bytes of compressed data. A return value of 0 indicates that needsInput should be called in order to determine if more input data is required.

This method uses NO_FLUSH as its compression flush mode. An invocation of this method of the form deflater.deflate(b, off, len) yields the same result as the invocation of deflater.deflate(b, off, len, Deflater.NO_FLUSH).

Parameters
output:byte[]

the buffer for the compressed data

off:int

the start offset of the data

len:int

the maximum number of bytes of compressed data

Returns:int

the actual number of bytes of compressed data written to the output buffer

Exceptions
IllegalStateException:
if the Deflater is closed
deflateback to summary
public int deflate(byte[] output)

Compresses the input data and fills specified buffer with compressed data. Returns actual number of bytes of compressed data. A return value of 0 indicates that needsInput should be called in order to determine if more input data is required.

This method uses NO_FLUSH as its compression flush mode. An invocation of this method of the form deflater.deflate(b) yields the same result as the invocation of deflater.deflate(b, 0, b.length, Deflater.NO_FLUSH).

Parameters
output:byte[]

the buffer for the compressed data

Returns:int

the actual number of bytes of compressed data written to the output buffer

Exceptions
IllegalStateException:
if the Deflater is closed
deflateback to summary
public int deflate(ByteBuffer output)

Compresses the input data and fills specified buffer with compressed data. Returns actual number of bytes of compressed data. A return value of 0 indicates that needsInput should be called in order to determine if more input data is required.

This method uses NO_FLUSH as its compression flush mode. An invocation of this method of the form deflater.deflate(output) yields the same result as the invocation of deflater.deflate(output, Deflater.NO_FLUSH).

Parameters
output:ByteBuffer

the buffer for the compressed data

Returns:int

the actual number of bytes of compressed data written to the output buffer

Exceptions
ReadOnlyBufferException:
if the given output buffer is read-only
IllegalStateException:
if the Deflater is closed
Since
11
deflateback to summary
public int deflate(byte[] output, int off, int len, int flush)

Compresses the input data and fills the specified buffer with compressed data. Returns actual number of bytes of data compressed.

Compression flush mode is one of the following three modes:

  • NO_FLUSH: allows the deflater to decide how much data to accumulate, before producing output, in order to achieve the best compression (should be used in normal use scenario). A return value of 0 in this flush mode indicates that needsInput() should be called in order to determine if more input data is required.
  • SYNC_FLUSH: all pending output in the deflater is flushed, to the specified output buffer, so that an inflater that works on compressed data can get all input data available so far (In particular the needsInput() returns true after this invocation if enough output space is provided). Flushing with SYNC_FLUSH may degrade compression for some compression algorithms and so it should be used only when necessary.
  • FULL_FLUSH: all pending output is flushed out as with SYNC_FLUSH. The compression state is reset so that the inflater that works on the compressed output data can restart from this point if previous compressed data has been damaged or if random access is desired. Using FULL_FLUSH too often can seriously degrade compression.

In the case of FULL_FLUSH or SYNC_FLUSH, if the return value is len, the space available in output buffer b, this method should be invoked again with the same flush parameter and more output space. Make sure that len is greater than 6 to avoid flush marker (5 bytes) being repeatedly output to the output buffer every time this method is invoked.

If the setInput(ByteBuffer) method was called to provide a buffer for input, the input buffer's position will be advanced by the number of bytes consumed by this operation.

Parameters
output:byte[]

the buffer for the compressed data

off:int

the start offset of the data

len:int

the maximum number of bytes of compressed data

flush:int

the compression flush mode

Returns:int

the actual number of bytes of compressed data written to the output buffer

Exceptions
IllegalArgumentException:
if the flush mode is invalid
IllegalStateException:
if the Deflater is closed
Since
1.7
deflateback to summary
public int deflate(ByteBuffer output, int flush)

Compresses the input data and fills the specified buffer with compressed data. Returns actual number of bytes of data compressed.

Compression flush mode is one of the following three modes:

  • NO_FLUSH: allows the deflater to decide how much data to accumulate, before producing output, in order to achieve the best compression (should be used in normal use scenario). A return value of 0 in this flush mode indicates that needsInput() should be called in order to determine if more input data is required.
  • SYNC_FLUSH: all pending output in the deflater is flushed, to the specified output buffer, so that an inflater that works on compressed data can get all input data available so far (In particular the needsInput() returns true after this invocation if enough output space is provided). Flushing with SYNC_FLUSH may degrade compression for some compression algorithms and so it should be used only when necessary.
  • FULL_FLUSH: all pending output is flushed out as with SYNC_FLUSH. The compression state is reset so that the inflater that works on the compressed output data can restart from this point if previous compressed data has been damaged or if random access is desired. Using FULL_FLUSH too often can seriously degrade compression.

In the case of FULL_FLUSH or SYNC_FLUSH, if the return value is equal to the remaining space of the buffer, this method should be invoked again with the same flush parameter and more output space. Make sure that the buffer has at least 6 bytes of remaining space to avoid the flush marker (5 bytes) being repeatedly output to the output buffer every time this method is invoked.

On success, the position of the given output byte buffer will be advanced by as many bytes as were produced by the operation, which is equal to the number returned by this method.

If the setInput(ByteBuffer) method was called to provide a buffer for input, the input buffer's position will be advanced by the number of bytes consumed by this operation.

Parameters
output:ByteBuffer

the buffer for the compressed data

flush:int

the compression flush mode

Returns:int

the actual number of bytes of compressed data written to the output buffer

Exceptions
IllegalArgumentException:
if the flush mode is invalid
ReadOnlyBufferException:
if the given output buffer is read-only
IllegalStateException:
if the Deflater is closed
Since
11
deflateBufferBufferback to summary
private native long deflateBufferBuffer(long addr, long inputAddress, int inputLen, long outputAddress, int outputLen, int flush, int params)
deflateBufferBytesback to summary
private native long deflateBufferBytes(long addr, long inputAddress, int inputLen, byte[] outputArray, int outputOff, int outputLen, int flush, int params)
deflateBytesBufferback to summary
private native long deflateBytesBuffer(long addr, byte[] inputArray, int inputOff, int inputLen, long outputAddress, int outputLen, int flush, int params)
deflateBytesBytesback to summary
private native long deflateBytesBytes(long addr, byte[] inputArray, int inputOff, int inputLen, byte[] outputArray, int outputOff, int outputLen, int flush, int params)
endback to summary
public void end()

Closes and releases the resources held by this Deflater and discards any unprocessed input.

If the Deflater is already closed then invoking this method has no effect.

Implementation Specification

Subclasses should override this method to clean up the resources acquired by the subclass.

See Also
close()
endback to summary
private static native void end(long addr)
ensureOpenback to summary
private void ensureOpen()
finishback to summary
public void finish()

When called, indicates that compression should end with the current contents of the input buffer.

finishedback to summary
public boolean finished()

Returns true if the end of the compressed data output stream has been reached.

Returns:boolean

true if the end of the compressed data output stream has been reached

getAdlerback to summary
public int getAdler()

Returns the ADLER-32 value of the uncompressed data.

Returns:int

the ADLER-32 value of the uncompressed data

Exceptions
IllegalStateException:
if the Deflater is closed
getAdlerback to summary
private static native int getAdler(long addr)
getBytesReadback to summary
public long getBytesRead()

Returns the total number of uncompressed bytes input so far.

Returns:long

the total (non-negative) number of uncompressed bytes input so far

Exceptions
IllegalStateException:
if the Deflater is closed
Since
1.5
getBytesWrittenback to summary
public long getBytesWritten()

Returns the total number of compressed bytes output so far.

Returns:long

the total (non-negative) number of compressed bytes output so far

Exceptions
IllegalStateException:
if the Deflater is closed
Since
1.5
getTotalInback to summary
public int getTotalIn()

Deprecated

since 23.

Use getBytesRead() instead

Returns the total number of uncompressed bytes input so far.

Implementation Specification

This method returns the equivalent of (int) getBytesRead() and therefore cannot return the correct value when it is greater than Integer#MAX_VALUE.

Returns:int

the total number of uncompressed bytes input so far

Annotations
@Deprecated
since:23
Exceptions
IllegalStateException:
if the Deflater is closed
getTotalOutback to summary
public int getTotalOut()

Deprecated

since 23.

Use getBytesWritten() instead

Returns the total number of compressed bytes output so far.

Implementation Specification

This method returns the equivalent of (int) getBytesWritten() and therefore cannot return the correct value when it is greater than Integer#MAX_VALUE.

Returns:int

the total number of compressed bytes output so far

Annotations
@Deprecated
since:23
Exceptions
IllegalStateException:
if the Deflater is closed
initback to summary
private static native long init(int level, int strategy, boolean nowrap)
needsInputback to summary
public boolean needsInput()

Returns true if no data remains in the input buffer. This can be used to determine if one of the setInput() methods should be called in order to provide more input.

Returns:boolean

true if the input data buffer is empty and setInput() should be called in order to provide more input

resetback to summary
public void reset()

Resets deflater so that a new set of input data can be processed. Keeps current compression level and strategy settings.

Exceptions
IllegalStateException:
if the Deflater is closed
resetback to summary
private static native void reset(long addr)
setDictionaryback to summary
public void setDictionary(byte[] dictionary, int off, int len)

Sets preset dictionary for compression. A preset dictionary is used when the history buffer can be predetermined. When the data is later uncompressed with Inflater.inflate(), Inflater.getAdler() can be called in order to get the Adler-32 value of the dictionary required for decompression.

Parameters
dictionary:byte[]

the dictionary data bytes

off:int

the start offset of the data

len:int

the length of the data

Exceptions
IllegalStateException:
if the Deflater is closed
See Also
Inflater#inflate, Inflater#getAdler()
setDictionaryback to summary
public void setDictionary(byte[] dictionary)

Sets preset dictionary for compression. A preset dictionary is used when the history buffer can be predetermined. When the data is later uncompressed with Inflater.inflate(), Inflater.getAdler() can be called in order to get the Adler-32 value of the dictionary required for decompression.

Parameters
dictionary:byte[]

the dictionary data bytes

Exceptions
IllegalStateException:
if the Deflater is closed
See Also
Inflater#inflate, Inflater#getAdler()
setDictionaryback to summary
public void setDictionary(ByteBuffer dictionary)

Sets preset dictionary for compression. A preset dictionary is used when the history buffer can be predetermined. When the data is later uncompressed with Inflater.inflate(), Inflater.getAdler() can be called in order to get the Adler-32 value of the dictionary required for decompression.

The bytes in given byte buffer will be fully consumed by this method. On return, its position will equal its limit.

Parameters
dictionary:ByteBuffer

the dictionary data bytes

Exceptions
IllegalStateException:
if the Deflater is closed
Since
11
See Also
Inflater#inflate, Inflater#getAdler()
setDictionaryback to summary
private static native void setDictionary(long addr, byte[] b, int off, int len)
setDictionaryBufferback to summary
private static native void setDictionaryBuffer(long addr, long bufAddress, int len)
setInputback to summary
public void setInput(byte[] input, int off, int len)

Sets input data for compression.

One of the setInput() methods should be called whenever needsInput() returns true indicating that more input data is required.

Parameters
input:byte[]

the input data bytes

off:int

the start offset of the data

len:int

the length of the data

See Also
Deflater#needsInput
setInputback to summary
public void setInput(byte[] input)

Sets input data for compression.

One of the setInput() methods should be called whenever needsInput() returns true indicating that more input data is required.

Parameters
input:byte[]

the input data bytes

See Also
Deflater#needsInput
setInputback to summary
public void setInput(ByteBuffer input)

Sets input data for compression.

One of the setInput() methods should be called whenever needsInput() returns true indicating that more input data is required.

The given buffer's position will be advanced as deflate operations are performed, up to the buffer's limit. The input buffer may be modified (refilled) between deflate operations; doing so is equivalent to creating a new buffer and setting it with this method.

Modifying the input buffer's contents, position, or limit concurrently with an deflate operation will result in undefined behavior, which may include incorrect operation results or operation failure.

Parameters
input:ByteBuffer

the input data bytes

Since
11
See Also
Deflater#needsInput
setLevelback to summary
public void setLevel(int level)

Sets the compression level to the specified value.

If the compression level is changed, the next invocation of deflate will compress the input available so far with the old level (and may be flushed); the new level will take effect only after that invocation.

Parameters
level:int

the new compression level (0-9)

Exceptions
IllegalArgumentException:
if the compression level is invalid
setStrategyback to summary
public void setStrategy(int strategy)

Sets the compression strategy to the specified value.

If the compression strategy is changed, the next invocation of deflate will compress the input available so far with the old strategy (and may be flushed); the new strategy will take effect only after that invocation.

Parameters
strategy:int

the new compression strategy

Exceptions
IllegalArgumentException:
if the compression strategy is invalid
shouldFinishback to summary
pack-priv boolean shouldFinish()

Returns the value of 'finish' flag. 'finish' will be set to true if def.finish() method is called.

java.util.zip back to summary

pack-priv Class Deflater.DeflaterZStreamRef

extends Object
implements Runnable
Class Inheritance
All Implemented Interfaces
java.lang.Runnable

A reference to the native zlib's z_stream structure. It also serves as the "cleaner" to clean up the native resource when the Deflater is ended, closed or cleaned.

Field Summary

Modifier and TypeField and Description
private long
private final Cleaner.Cleanable

Constructor Summary

AccessConstructor and Description
private
DeflaterZStreamRef(Deflater owner, long addr)

Method Summary

Modifier and TypeMethod and Description
pack-priv long
pack-priv void
public synchronized void
run()

Implements java.lang.Runnable.run.

Runs this operation.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

addressback to summary
private long address
cleanableback to summary
private final Cleaner.Cleanable cleanable

Constructor Detail

DeflaterZStreamRefback to summary
private DeflaterZStreamRef(Deflater owner, long addr)

Method Detail

addressback to summary
pack-priv long address()
cleanback to summary
pack-priv void clean()
runback to summary
public synchronized void run()

Implements java.lang.Runnable.run.

Doc from java.lang.Runnable.run.

Runs this operation.