Class GZIPOutputStream in Java

digitization, transformation, binary

Class GZIPOutputStream in Java

GZIPOutputStream class is a subclass of DeflaterOutputStream that compresses and writes data using the gzip file format. To create a GZIPOutputStream, we need to specify the OutputStream that it is to write to, and may optionally specify a size for the internal compression buffer. Once the GZIPOutputStream is created, we can use the write() and close() methods as we need any other output stream.

The GZIPOutputStream class compresses data using the GZIP format. To use it, simply construct a GZIPOutputStream that wraps a regular output stream and uses the write() methods to write compressed data.

Ads code goes here

The class structure of Class GZIPOutputStream in Java is given as

public class java.util.zip.GZIPOutputStream extends java.util.zip.DeflaterOutputStream{
// Public Constructors
public GZIPOutputStream(OutputStream out, int size) throws IOException;
public GZIPOutputStream(OutputStream out) throws IOException;
// Protected Instance Variables
protected CRC32 crc;
// Public Instance Methods
public void close() throws IOException; // Overrides DeflaterOutputStream
public void finish() throws IOException; // Overrides DeflaterOutputStream
public synchronized void write(byte[] buf, int off, int len) throws IOException; // Overrides DeflaterOutputStream
}

The details of the class structure are given as follows:

protected CRC32 crc;

protected CRC32 crc represents a checksum that is updated with the uncompressed stream data. The checksum value is written into the GZIP trailer.

public GZIPOutputStream(OutputStream out);

public GZIPOutputStream(OutputStream out) constructor creates a GZIPOutputStream that writes compressed data to the given OutputStream. The GZIPOutputStream uses a compression buffer with the default size of 512 bytes.

READ  Abstract Interface ItemSelectable in Java

Parameter
out – The underlying output stream.

public GZIPOutputStream(OutputStream out, int size);

public GZIPOutputStream(OutputStream out, int size) constructor creates a GZIPOutputStream that writes compressed data to the given OutputStream. The GZIPOutputStream uses a compression buffer of the given size.

Parameter
out – The underlying output stream.
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 synchronized void write(byte[] buf, int off, int len);

public synchronized void write(byte[] buf, 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.

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

Apart from these GZIPOutputStream 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)

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

  • flush()
  • write(byte[])

GZIPOutputStream class also has inherited single method from DeflaterOutputStream. That is as follows:

  • write(int)

Share and Enjoy !

Leave a Comment

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