java.lang.Object | |
↳ | java.util.zip.Deflater |
This class compresses data using the DEFLATE algorithm (see specification).
It is usually more convenient to use DeflaterOutputStream
.
To compress an in-memory byte[]
to another in-memory byte[]
manually:
byte[] originalBytes = ... Deflater deflater = new Deflater(); deflater.setInput(originalBytes); deflater.finish(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buf = new byte[8192]; while (!deflater.finished()) { int byteCount = deflater.deflate(buf); baos.write(buf, 0, byteCount); } deflater.end(); byte[] compressedBytes = baos.toByteArray();
In situations where you don't have all the input in one array (or have so much
input that you want to feed it to the deflater in chunks), it's possible to call
setInput
repeatedly, but you're much better off using
DeflaterOutputStream
to handle all this for you. DeflaterOutputStream
also helps
minimize memory requirements — the sample code above is very expensive.
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
int | BEST_COMPRESSION | Upper bound for the compression level range. | |||||||||
int | BEST_SPEED | Lower bound for compression level range. | |||||||||
int | DEFAULT_COMPRESSION | The default compression level. | |||||||||
int | DEFAULT_STRATEGY | The default compression strategy. | |||||||||
int | DEFLATED | The default compression method. | |||||||||
int | FILTERED | A compression strategy. | |||||||||
int | HUFFMAN_ONLY | A compression strategy. | |||||||||
int | NO_COMPRESSION | A compression level. |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Constructs a new
Deflater instance using the default compression
level. | |||||||||||
Constructs a new
Deflater instance using compression
level level . | |||||||||||
Constructs a new
Deflater instance with a specific compression
level. |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Deflates data (previously passed to
setInput ) into a specific
region within the supplied buffer. | |||||||||||
Deflates the data (previously passed to
setInput ) into the
supplied buffer. | |||||||||||
Frees all resources held onto by this deflating algorithm.
| |||||||||||
Indicates to the
Deflater that all uncompressed input has been provided
to it. | |||||||||||
Returns the
Adler32 checksum of the uncompressed data read so far. | |||||||||||
Returns the total number of bytes read by the
Deflater . | |||||||||||
Returns a the total number of bytes written by this
Deflater . | |||||||||||
Returns the total number of bytes of input read by this
Deflater . | |||||||||||
Returns the total number of bytes written to the output buffer by this
Deflater . | |||||||||||
Returns true if
setInput must be called before deflation can continue. | |||||||||||
Resets the
Deflater to accept new input without affecting any
previously made settings for the compression strategy or level. | |||||||||||
Sets the dictionary to be used for compression by this
Deflater . | |||||||||||
Sets the dictionary to be used for compression by this
Deflater . | |||||||||||
Sets the input buffer the
Deflater will use to extract uncompressed bytes
for later compression. | |||||||||||
Sets the input buffer the
Deflater will use to extract uncompressed bytes
for later compression. | |||||||||||
Sets the compression level to be used when compressing data.
| |||||||||||
Sets the compression strategy to be used.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Invoked when the garbage collector has detected that this instance is no longer reachable.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
|
Upper bound for the compression level range.
Lower bound for compression level range.
The default compression level.
The default compression strategy.
The default compression method.
A compression strategy.
A compression strategy.
A compression level.
Constructs a new Deflater
instance using the default compression
level. The strategy can be specified with setStrategy(int)
. A
header is added to the output by default; use Deflater(int, boolean)
if you need to omit the header.
Constructs a new Deflater
instance using compression
level level
. The strategy can be specified with setStrategy(int)
.
A header is added to the output by default; use
Deflater(int, boolean)
if you need to omit the header.
level | the compression level in the range between 0 and 9. |
---|
Constructs a new Deflater
instance with a specific compression
level. If noHeader
is true, no ZLIB header is added to the
output. In a ZIP archive every entry (compressed file) comes with such a
header. The strategy can be specified using setStrategy(int)
.
level | the compression level in the range between 0 and 9. |
---|---|
noHeader | true indicates that no ZLIB header should be written.
|
Deflates data (previously passed to setInput
) into a specific
region within the supplied buffer.
buf
.
Deflates the data (previously passed to setInput
) into the
supplied buffer.
buf
.
Frees all resources held onto by this deflating algorithm. Any unused
input or output is discarded. This method should be called explicitly in
order to free native resources as soon as possible. After end()
is
called, other methods will typically throw IllegalStateException
.
Indicates to the Deflater
that all uncompressed input has been provided
to it.
Returns the Adler32
checksum of the uncompressed data read so far.
Returns the total number of bytes read by the Deflater
. This
method is the same as getTotalIn()
except that it returns a
long
value instead of an integer.
Returns a the total number of bytes written by this Deflater
. This
method is the same as getTotalOut
except it returns a
long
value instead of an integer.
Returns the total number of bytes of input read by this Deflater
. This
method is limited to 32 bits; use getBytesRead()
instead.
Returns the total number of bytes written to the output buffer by this Deflater
. The method is limited to 32 bits; use getBytesWritten()
instead.
Resets the Deflater
to accept new input without affecting any
previously made settings for the compression strategy or level. This
operation must be called after finished()
returns
true if the Deflater
is to be reused.
Sets the dictionary to be used for compression by this Deflater
.
This method can only be called if this Deflater
supports the writing
of ZLIB headers. This is the default, but can be overridden
using Deflater(int, boolean)
.
Sets the dictionary to be used for compression by this Deflater
.
This method can only be called if this Deflater
supports the writing
of ZLIB headers. This is the default, but can be overridden
using Deflater(int, boolean)
.
Sets the input buffer the Deflater
will use to extract uncompressed bytes
for later compression.
Sets the input buffer the Deflater
will use to extract uncompressed bytes
for later compression.
Sets the compression level to be used when compressing data. The
compression level must be a value between 0 and 9. This value must be set
prior to calling setInput
.
IllegalArgumentException | If the compression level is invalid. |
---|
Sets the compression strategy to be used. The strategy must be one of
FILTERED, HUFFMAN_ONLY or DEFAULT_STRATEGY. This value must be set prior
to calling setInput
.
IllegalArgumentException | If the strategy specified is not one of FILTERED, HUFFMAN_ONLY or DEFAULT_STRATEGY. |
---|
Invoked when the garbage collector has detected that this instance is no longer reachable. The default implementation does nothing, but this method can be overridden to free resources.
Note that objects that override finalize
are significantly more expensive than
objects that don't. Finalizers may be run a long time after the object is no longer
reachable, depending on memory pressure, so it's a bad idea to rely on them for cleanup.
Note also that finalizers are run on a single VM-wide finalizer thread,
so doing blocking work in a finalizer is a bad idea. A finalizer is usually only necessary
for a class that has a native peer and needs to call a native method to destroy that peer.
Even then, it's better to provide an explicit close
method (and implement
Closeable
), and insist that callers manually dispose of instances. This
works well for something like files, but less well for something like a BigInteger
where typical calling code would have to deal with lots of temporaries. Unfortunately,
code that creates lots of temporaries is the worst kind of code from the point of view of
the single finalizer thread.
If you must use finalizers, consider at least providing your own
ReferenceQueue
and having your own thread process that queue.
Unlike constructors, finalizers are not automatically chained. You are responsible for
calling super.finalize()
yourself.
Uncaught exceptions thrown by finalizers are ignored and do not terminate the finalizer thread. See Effective Java Item 7, "Avoid finalizers" for more.