Class GZIPInputStream in Java

digitization, transformation, binary

Class GZIPInputStream in Java

GZIPInputStream class is a subclass of InflaterInputStream that reads and uncompresses data compressed in gzip format. To create a GZIPInputStream, we need to simply specify the InputStream it is to read compressed data from, and optionally specify a buffer size for the internal decompression buffer. Once a GZIPInputStream is created, we can use the read() and close() methods as we need with any input stream.

The GZIPInputStream class decompresses data that has been compressed using the GZIP format. To use it, simply construct a GZIPInputStream that wraps a regular input stream and uses the read() methods to read the compressed data.

Ads code goes here

The class structure of Class GZIPInputStream in Java is given as

public class java.util.zip.GZIPInputStream extends java.util.zip.InflaterInputStream{
// Public Constructors
public GZIPInputStream(InputStream in, int size) throws IOException;
public GZIPInputStream(InputStream in) throws IOException;
// Constants
public static final int GZIP_MAGIC;
// Protected Instance Variables
protected CRC32 crc;
protected boolean eos;
// Public Instance Methods
public void close() throws IOException; // Overrides FilterInputStream
public int read(byte[] buf, int off, int len) throws IOException; //Overrides InflaterInputStream
}

The details of the class structure are given as follows:

public static final int GZIP_MAGIC;

public static final int GZIP_MAGIC represents a constant that contains the “magic number” that appears in the header of GZIP files.

protected CRC32 crc;

protected CRC32 crc represents a checksum value of the uncompressed data. When an entire file has been read, this checksum is compared to a value stored in the GZIP trailer. If the values do not match, an exception is thrown from read().

protected boolean eos;

protected boolean eos represents a flag that indicates whether or not the end of the compressed stream has been reached. It is set to true when the compressed data and the GZIP trailer have been read.

public GZIPInputStream(InputStream in);

public GZIPInputStream(InputStream in) constructor creates a GZIPInputStream that inflates data from the given InputStream. The GZIPInputStream uses a decompression buffer with the default size of 512 bytes. The GZIP header is read immediately.

READ  Hire Java Builders With Working experience in Top 10 Java Frameworks

Parameter
in – The underlying input stream.

public GZIPInputStream(InputStream in, int size);

public GZIPInputStream(InputStream in, int size) constructor creates a GZIPInputStream that inflates data from the given InputStream. The GZIPInputStream uses a decompression buffer of the given size. The GZIP header is read
immediately.

Parameter
in – The underlying input stream.
size – The size of the input buffer.

public void close();

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

public int read(byte[] buf, int off, int len);

public int read(byte[] buf, int off, int len) method reads enough data from the underlying InputStream to return len bytes of uncompressed data. The uncompressed data is placed into the given array starting at off. The method blocks until some data are available for decompression.

This method returns the number of bytes read or -1 if the end of the stream is encountered immediately.

Parameter
buf – An array of bytes to be filled from the stream.
off – An offset into the byte array.
len – The number of bytes to read.

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

GZIPInputStream class also has inherited methods from FilterInputStream. They are as follows:

  • available()
  • mark(int)
  • reset()
  • markSupported()
  • read(byte[])

GZIPInputStream class also has inherited methods from InflaterInputStream. They are as follows:

  • read()
  • skip(long)

Share and Enjoy !

Leave a Comment

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