Open In App

Java.util.zip.DeflaterInputStream class in Java

Improve
Improve
Like Article
Like
Save
Share
Report

Implements an input stream filter for compressing data in the “deflate” compression format.

Constructor and Description

  • DeflaterInputStream(InputStream in) : Creates a new input stream with a default compressor and buffer size.
  • DeflaterInputStream(InputStream in, Deflater defl) : Creates a new input stream with the specified compressor and a default buffer size.
  • DeflaterInputStream(InputStream in, Deflater defl, int bufLen) :Creates a new input stream with the specified compressor and buffer size.

Methods:

  • int available() : Returns 0 after EOF has been reached, otherwise always return 1.
    Syntax :public int available()
                  throws IOException
    Parameters:
    n - number of bytes to be skipped
    Returns:
    the actual number of bytes skipped
    Throws:
    IOException
  • void close() : Closes this input stream and its underlying input stream, discarding any pending uncompressed data.
    Syntax :public void close()
               throws IOException
    Overrides:
    close in class FilterInputStream
    Throws:
    IOException
  • void mark(int limit) : This operation is not supported.
    Syntax :public void mark(int limit)
    Parameters:
    limit - maximum bytes that can be read before invalidating the position marker
    
  • boolean markSupported() : Always returns false because this input stream does not support the mark() and reset() methods.
    Syntax :public boolean markSupported()
    Returns:
    false, always
  • int read() : Reads a single byte of compressed data from the input stream.
    Syntax :public int read()
             throws IOException
    Returns:
    a single byte of compressed data, or -1 if the end of the uncompressed
    input stream is reached
    Throws:
    IOException
  • int read(byte[] b, int off, int len) : Reads compressed data into a byte array.
    Syntax :public int read(byte[] b,
           int off,
           int len)
             throws IOException
    Parameters:
    b - buffer into which the data is read
    off - starting offset of the data within b
    len - maximum number of compressed bytes to read into b
    Returns:
    the actual number of bytes read, or -1 if the end of the
    uncompressed input stream is reached
    Throws:
    IndexOutOfBoundsException 
    IOException 
  • void reset() : This operation is not supported.
    Syntax :public void reset()
               throws IOException
    Throws:
    IOException
  • long skip(long n) : Skips over and discards data from the input stream.
    Syntax :public long skip(long n)
              throws IOException
    Parameters:
    n - number of bytes to be skipped
    Returns:
    the actual number of bytes skipped
    Throws:
    IOException

Program:




//Java program to illustrate DeflaterInputStream class
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.zip.DeflaterInputStream;
  
class DeflaterInputStreamDemo
{
    public static void main(String[] args) throws IOException
    {
        byte b[] = new byte[10];
        for (byte i = 0; i <10 ; i++)
        {
            b[i] = i;
        }
        ByteArrayInputStream bin = new ByteArrayInputStream(b);
        DeflaterInputStream din = new DeflaterInputStream(bin);
  
        //illustrating markSupported() method
        System.out.println(din.markSupported());
          
        //illustrating skip() method
        din.skip(1);
  
        //illustrating available() method
        System.out.println(din.available());
  
        //illustrating read(byte[] b,int off,int len)
        byte c[] = new byte[10];
          
        din.read(c,0,9);
                for (int i = 0; i < 9; i++)
        {
            System.out.print(c[i]);
        }
        while(din.available() == 1)
        {
            //Reads a single byte of compressed data
            System.out.print(din.read());
        }
          
        System.out.println();
        System.out.println(din.available());
  
        // illustrating close() method
        din.close();
    }
}


Output :

false
1
-1009996100981029710199231224400175046-1
0

The above output represents compressed data.

Next Article: Java.util.zip.DeflaterOutputStream class in Java



Last Updated : 12 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads