Class DeflaterOutputStream in Java

digitization, transformation, binary

Class DeflaterOutputStream in Java

DeflaterOutputStream class is a subclass of java.io.FilterOutputStream; it “filters” a stream of data by compressing (“deflating”) it and then writes the compressed data to another output stream. To create a DeflaterOutputStream, we need to specify the stream that it is to write to, and also a Deflater object that is to perform the compression.

We can set various options on the Deflater object to specify just what type of compression is to be performed. Once a DeflaterOutputStream is created, its write() and close() methods are the same as those of other output streams.

Ads code goes here

The InflaterInputStream class can be used to read data written with the DeflaterOutputStream.

The DeflaterOutputStream class represents an OutputStream with an associated Deflater. In
other words, a DeflaterOutputStream wraps a regular output stream, so that data written to the stream is compressed and written to the underlying stream. Two subclasses, GZIPOutputStream and ZipOutputStream, write compressed data in widely-recognized formats.

Note

A DeflaterOutputStream writes raw compressed data; applications often prefer one of its subclasses, GZIPOutputStream or ZipOutputStream, which wraps the raw compressed data within a standard file format.

The class structure of Class DeflaterOutputStream in Java is given as

public class java.util.zip.DeflaterOutputStream extends java.io.FilterOutputStream{
// Public Constructors
public DeflaterOutputStream(OutputStream out, Deflater def, int size);
public DeflaterOutputStream(OutputStream out, Deflater def);
public DeflaterOutputStream(OutputStream out);
// Protected Instance Variables
protected byte[] buf;
protected Deflater def;
// Public Instance Methods
public void close() throws IOException; // Overrides FilterOutputStream
public void finish() throws IOException;
public void write(int b) throws IOException; // Overrides FilterOutputStream
public void write(byte[] b, int off, int len) throws IOException; //Overrides FilterOutputStream
// Protected Instance Methods
protected void deflate() throws IOException;
}

The details of the class structure are given as follows:

protected byte[] buf;

protected byte[] buf represents a buffer that holds the compressed data that is written to the underlying stream.

protected Deflater def;

protected Deflater def represents the Deflater object that is used internally.

public DeflaterOutputStream(OutputStream out);

public DeflaterOutputStream(OutputStream out) constructor creates a DeflaterOutputStream that writes data to the given OutputStream. Before being written, the data is compressed by a default Deflater. The DeflaterOutputStream uses a compression buffer with the default size of 512 bytes.

READ  Class InstantiationError in Java

Parameter
out – The underlying output stream.

public DeflaterOutputStream(OutputStream out, Deflater def);

public DeflaterOutputStream(OutputStream out, Deflater def) constructor creates a DeflaterOutputStream that writes data to the given OutputStream. Before being written, the data is compressed by the given Deflater. The DeflaterOutputStream uses a compression buffer with the default size of 512 bytes.

Parameter
out – The underlying output stream.
def – The Deflater object.

public DeflaterOutputStream(OutputStream out, Deflater def, int size);

public DeflaterOutputStream(OutputStream out, Deflater def, int size) constructor creates a DeflaterOutputStream that writes data to the given OutputStream. Before being written, the data is compressed by the given Deflater. The
DeflaterOutputStream uses a compression buffer of the given size.

Parameter
out – The underlying output stream.
def – The Deflater object.
size – The size of the output buffer.

public void close();

public void close() method closes the stream and releases any system resources that are associated with it.

public void finish();

public void finish() method finishes writing compressed data to the underlying stream without closing it.

public void write(int b);

public void write(int b) method compresses a byte that contains the lowest eight bits of the given integer value and
writes it to the underlying OutputStream. The method blocks until the byte is written.

Parameter
b – The value to write.

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

public void write(byte[] b, int off, int len) method takes len bytes from the given buffer, starting at off, and compresses them. The method then writes the compressed data to the underlying OutputStream. The method blocks
until all of the bytes have been written.

READ  Class ByteArrayOutputStream in Java

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

protected void deflate();

protected void deflate() method asks the internal Deflater to compress the data in the buffer for this DeflaterOutputStream. The method then writes the compressed contents of the buffer to the underlying stream. The method is called by both write() methods.

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

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

DeflaterOutputStream class also has inherited methods from FilterOutputStream. They are as follows:

  • write(byte[])
  • flush()

Share and Enjoy !

Leave a Comment

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