Deflater class in Java

Deflater class in Java

Deflater class implements the general ZLIB data compression algorithm used by the gzip and PKZip compression programs. The constants defined by this class are used to specify the compression strategy to be used and the then compression speed/ strength trade-off level to be used.

The Deflater class provides support for general-purpose data compression. The class uses the ZLIB compression algorithms described in RFC 1950, RFC 1951, and RFC 1952.

Ads code goes here

The Inflater class uncompresses data that has been compressed using Deflater.
The DeflaterOutputStream uses an internal Deflater to compress data. Typically, you do not need to create a Deflater; instead, you can just use an instance of one of the subclasses of DeflaterOutputStream: GZIPOutputStream or ZipOutputStream.

If we set the nowrap argument to the constructor to true, then, the ZLIB header and checksum data are omitted from the compressed output, which is the format that both gzip and PKZip use.

The important methods of this class are setInput(), which specifies input data to be compressed, and deflate(), which compresses the data and returns the compressed output.

The remaining methods exist so that Deflater used for stream-based compression, as it is in higher-level classes such as GZIPOutputStream and ZipOutputStream.

These stream classes are sufficient in most cases. Most applications do not need to use Deflater directly. The Inflater class uncompresses data compressed with a Deflater object.

The structure of Deflater class is given as

public class java.util.zip.Deflater extends java.lang.Object
// Public Constructors
public Deflater(int level,boolean nowrap);
public Deflater(int level);
public Deflater();
//Constants 
public static final int BEST_COMPRESSION;
public static final int BEST_SPEED;
public static final int DEFAULT_COMPRESSION
public static final int DEFAUT_STRATEGY;
public static final int DEFLATED;
public static final int FILTERED;
public static final int HUFFMAN_ONLY;
public static final int NO_COMPRESSION;
// Public Instance Methods 
public synchronized native int deflate(byte [ ] b; int off, int len);
public int deflate(byte [ ] b);
public synchronized native void end();
public synchronized void finish();
public synchronized boolean finished();;
public synchronized native int getAdler();
public synchronized native int getTotalIn();
public synchronized native int getTotalout();
public boolean needsInput();
public synchronized native void reset ();
public synchronized native void setDictionary(byte[ ] b, int off,int len);
public void setDictionary(byte [ ] b);
public synchronized void setInput(byte [ ] b; int off, int len)
public void setInput(byte [ ] b);
public synchronized void setLevel(int Level);
public synchronized void setStrategy(int strategy);
// Protected Instance Methods
protected void finalize ();// Overrides Object
}

The details of the class structure are given as follows:

public static final int BEST_COMPRESSION;

public static final int BEST_COMPRESSION represents a constant that represents a  compression level that sacrifices speed for the smallest compressed data size.

READ  Finally Keyword in Java Simplified

The compression level for a Deflater object can be set with setLevel(), where the level ranges
from 0 to 9. like BEST_COMPRESSION = 9

public static final int BEST_SPEED;

public static final int BEST_SPEED represents a constant that represents a compression level that sacrifices compressed data size for speed.

The compression level for a Deflater object can be set with setLevel(), where the level ranges from 0 to 9. like BEST_SPEED = 1

public static final int DEFAULT_COMPRESSION;

public static final int DEFAULT_COMPRESSION represents a constant that represents the default compression level. like DEFAULT_COMPRESSION = -1

public static final int DEFAULT_STRATEGY;

public static final int DEFAULT_STRATEGY represents a constant that represents the default compression strategy.

public static final int DEFLATED;

public static final int DEFLATED represents a constant that represents a compression method that uses the deflate algorithm.

public static final int FILTERED;

public static final int FILTERED represents a constant that represents a compression strategy that works well for small values with a random distribution.

public static final int HUFFMAN_ONLY;

public static final int HUFFMAN_ONLY represents A constant that represents a compression strategy that uses only Huffman coding.

public static final int NO_COMPRESSION;

public static final int NO_COMPRESSION represents a constant that represents a compression level that does not compress data at all.

The compression level for a Deflater object can be set with setLevel(), where the level ranges from 0 to 9. like NO_COMPRESSION = 0

public Deflater();

public Deflater() constructor creates a Deflater that generates compressed data in the ZLIB format using the DEFAULT_COMPRESSION level.

public Deflater(int level);

public Deflater(int level) constructor creates a Deflater that generates compressed data in the ZLIB format using the given compression level.

Parameter
level – The compression level, from 0 (NO_COMPRESSION) to 9 (BEST_COMPRESSION).

public Deflater(int level, boolean nowrap);

public Deflater(int level, boolean nowrap) constructor creates a Deflater that generates compressed data using the given compression level. If nowrap is true, the ZLIB header and checksum fields are not used, which means that the compressed data is in the format used by GZIP and PKZIP. If the parameter is false, the data is compressed into
ZLIB format.

Parameter
level – The compression level, from 0 (NO_COMPRESSION) to 9 (BEST_COMPRESSION).
nowrap – A boolean value that specifies whether or not the ZLIB header and checksum data are omitted from the compressed data.

public int deflate(byte[] b);

public int deflate(byte[] b) method compresses the data passed to setInput() and fills the given array with the compressed data. If this method returns zero, needsInput() should be called to determine whether the Deflater needs more data in its input buffer.

READ  Class Inflater in Java

This method returns the number of compressed bytes actually written to the array or 0 if more data may be required.

Parameter
b – A byte array to be filled.

public synchronized native int deflate(byte[] b, int off, int len);

public synchronized native int deflate(byte[] b, int off, int len) method compresses the data passed to setInput() and writes len bytes of the compressed data into the given array, starting at off. If this method returns 0, needsInput() should be called to determine whether the Deflater needs more data in its input buffer.

This method returns the number of compressed bytes actually written to the array or 0 if more data may be required.

Parameter
b – A byte array to be filled.
off – An offset into the byte array.
len – The number of bytes to fill.

public synchronized native void end();

public synchronized native void end() method discards any uncompressed input data and frees up internal buffers.

public synchronized void finish();

public synchronized void finish() method tells the Deflater that the compression should end with the data that currently occupies the input buffer.

public synchronized boolean finished();

public synchronized boolean finished() method returns true if the last of the compressed data has been read using deflate(). Otherwise it returns false.

This method returns a boolean value that indicates whether or not the end of the compressed data has been reached.

public synchronized native int getAdler();

public synchronized native int getAdler() method returns an Adler32 checksum value that is calculated on the uncompressed data passed to setInput().

This method returns the Adler-32 checksum value of the uncompressed data.

public synchronized native int getTotalIn();

public synchronized native int getTotalIn() method returns the number of bytes that have been passed to setInput() since this Deflater was created or since reset()was last called.

This method returns the total number of bytes that have been input so far.

public synchronized native int getTotalOut();

public synchronized native int getTotalOut() method returns the number of bytes that have been read from deflate() since this Deflater was created, or since reset() was last called.

This method returns the total number of bytes that have been output so far.

public boolean needsInput();

public boolean needsInput() method returns true if the input buffer is empty. Otherwise it returns false.

READ  How To Format Output in Java?

This method returns a boolean value that indicates whether or not the input buffer is empty.

public synchronized native void reset();

public synchronized native void reset() method resets the Deflater to the state it was in when it was created, which means that a new set of data can be compressed.

public void setDictionary(byte[] b);

public void setDictionary(byte[] b) method sets the preset dictionary for compression using the data in the given array.

Parameter
b – An array of byte values.

public synchronized native void setDictionary(byte[] b, int off, int len);

public synchronized native void setDictionary(byte[] b, int off, int len) method sets the preset dictionary for compression using len bytes from the given array, starting from off.

Parameter
b – An array of byte values.
off – An offset into the byte array.
len – The number of bytes to use.

public void setInput(byte[] b);

public void setInput(byte[] b) method places the contents of the given array into the input buffer of this Deflater. Use the deflate() method to compress the data and retrieve it in compressed form.

Parameter
b – An array of byte values.

public synchronized void setInput(byte[] b, int off, int len);

public synchronized void setInput(byte[] b, int off, int len) method places len bytes from the given array, starting at off, into the input buffer of this Deflater. Use the deflate() method to compress the data and retrieve it in compressed form.

Parameter
b – An array of byte values.
off – An offset into the byte array.
len – The number of bytes to use.

public synchronized void setLevel(int level);

public synchronized void setLevel(int level) method sets the compression level of this Deflater. A value of 0 corresponds to NO_COMPRESSION. A value of 1 indicates the fastest, least space-efficient compression level
(BEST_SPEED). A value of 9 indicates the slowest, most space-efficient compression level (BEST_COMPRESSION).

Parameter
level – The compression level, from 0 (NO_COMPRESSION) to 9 (BEST_COMPRESSION).

public synchronized void setStrategy(int strategy);

public synchronized void setStrategy(int strategy) method sets the compression strategy of this Deflater, which should be one of FILTERED, HUFFMAN_ONLY, or DEFAULT_STRATEGY.

Parameter
strategy – The compression strategy.

protected void finalize();

protected void finalize() method calls end() when this Deflater is garbage collected.

Apart from these Deflater class also has inherited methods from class- Object. They are as follows:

  • clone()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *