Use of getTotalIn and getTotalOut method of Inflater class in java.


 

Use of getTotalIn and getTotalOut method of Inflater class in java.

In this tutorial you will see the Use of getTotalIn and getTotalOut method of Inflater class in java.

In this tutorial you will see the Use of getTotalIn and getTotalOut method of Inflater class in java.

Use of getTotalIn and getTotalOut method of Inflater class in java.

In this tutorial, we will discuss about getTotalIn() and getTotalOut() method of Inflater class. The Inflater class provide support for Decompression using ZLIB library.  The Inflater class is available in java.util.zip package.The getTotalIn() method returns total number of compressed bytes of input stream. The getTotalOut() method returns total number of Uncompressed bytes of output stream.
            In the given Example, we will discuss about the Inflater class which is used for decompression. The FileInputStream class creates a input stream and read  bytes from file. The InflaterInputStream class creates a input stream with a given Inflater. It reads data from stream and decompress. The getTotalIn() method returns total number of compressed bytes of input stream. The read() method of InflaterInputStream class read uncompressed  data into array of bytes. The getTotalOut() method returns total number of Uncompressed bytes of output stream.

About  Inflater API:

The java.util.zip.Inflater class extends java.lang.Object class. It provid following method:

Return Type Method Description 
int getTotalIn() Function getTotalIn() returns number of compress bytes from associated input stream.
int getTotalOut() Function getTotalOut() returns number of Uncompress bytes from associated output stream.
void close() The close() method close input stream and releases system resources. 

Code

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;
import java.io.IOException;

class Demo {
  public static String infile = "testfile.txt";
  public static String outfilename = "zip.txt";

  public static void getTotal() throws IOException {
    FileInputStream inStream = new FileInputStream(infile);
    Inflater infl = new Inflater();
    InflaterInputStream inflInstream = new InflaterInputStream(inStream,
        infl);
    FileOutputStream outstream = new FileOutputStream(outfilename);
    boolean completed = false;
    try {
      byte b[] new byte[1024];
      while (true) {
        int len = inflInstream.read(b, 01024);
        if (len == -1) {
          break;
        }
        System.out.println("Compressed file : " + infile);
        int inSize = infl.getTotalIn();
        System.out.println("Actual Size of compressed data :" + inSize);
        outstream.write(b, 0, len);
      }
      System.out.println("Uncompressed file : " + outfilename);
      int outSize = infl.getTotalOut();
      System.out.println("Actual Size of Uncompressed data :" + outSize);
      completed = true;
      inflInstream.close();
      outstream.close();
    catch (IOException e) {
      System.out.println("IOException : " + e);
    }
  }
}

public class InflaterGetTotal {
  public static void main(String[] argsthrows IOException {
    Demo.getTotal();
  }
}

Following is the output if you run the application:

C:\l>java InflaterGetTotal
Compressed file : testfile.txt
Actual Size of compressed data :19
Uncompressed file : zip.txt
Actual Size of Uncompressed data :59

Download this code

Ads