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


 

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

In this tutorial you will find the use of getTotalIn and getTotalOut method of Deflater class in java.

In this tutorial you will find the use of getTotalIn and getTotalOut method of Deflater class in java.

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

In this tutorial, we will discuss the use of getTotalIn() and getTotalout() method of  Deflater. The Deflater class is used to compressed data by using ZLIB library. The Deflater class is available in java.util.zip package.The getTotalIn() method  returns total number of uncompressed bytes of input stream. The getTotalOut() method  returns total number of compressed bytes of  output stream. The finish() method of Deflater class is used to indicate that compression of data is finished.   

                      In this Example, we will see the getTotalIn() method and getTotalOut() method. The Defleter class create a compressor object for compressing data.  The FileInputStream class creates a input stream for reading data from a file. The read() method of  FileInputStream class read data from input stream. The deflater(byte[] buff, int offset, int length) method of  Deflater class fills buffer with compressed data. The finish() method of Deflater class is used to indicate that compression of data is finished. The finished() method of Deflater class returns true if output stream reached at the end of compressed data and release the system resource. 

About Deflater API:

The java.util.zip.Deflater class extends java.lang.Object class. It provides the following method:

Return Type Method Description 
int getTotalIn() The getTotalIn() method return total number of uncompressed  byte from input stream.   
int getTotalOut() The getTotalOut() method return total number compressed  byte after compression.   
int deflater(byte[] b, int offset, int length) The deflater(.....) method store compressed data into  associated buffer.
void finished() The finished() method  returns true if output stream reached at the end of compressed data.
void setInput() The setInput() method set data for compression.
void finish() Thefinish() method define that compression will be finished .

Code

import java.io.*;
import java.util.zip.*;
import java.util.zip.Deflater;
import java.io.FileOutputStream;
import java.io.FileInputStream;

public class DefGetTotal {
  public final static String SUFFIX = "testfile.txt";

  public static void main(String[] argsthrows IOException {

    Deflater defl1;
    FileInputStream finStream;
    FileOutputStream foutStream;
    byte[] inBuffer = new byte[1024];
    byte[] outBuffer = new byte[1024];
    int compBytes;
    for (int i = 0; i < args.length; i++) {
      defl1 = new Deflater();
      finStream = new FileInputStream(args[i]);
      foutStream = new FileOutputStream(args[i+ SUFFIX);
      while (true) {
        int readNum = finStream.read(inBuffer);
        if (readNum == -1) {
          int inSize = defl1.getTotalIn();
          System.out.println("Size of fille before Compression : "
              + inSize);
          defl1.finish();
          while (!defl1.finished()) {
            compBytes = defl1.deflate(outBuffer, 0,
                outBuffer.length);
            if (compBytes > 0) {
              foutStream.write(outBuffer, 0, compBytes);
            }
          }
          int outSize = defl1.getTotalOut();
          System.out.println("Size of fille after Compression : "
              + outSize);
          break;
        else {
          defl1.setInput(inBuffer, 0, readNum);

          while (!defl1.needsInput()) {
            compBytes = defl1.deflate(outBuffer, 0,
                outBuffer.length);
            if (compBytes > 0) {
              foutStream.write(outBuffer, 0, compBytes);
            }
          }
        }
      }
      finStream.close();
      foutStream.flush();
      foutStream.close();
      defl1.reset();
    }
    System.out.println("The given file's data Successfully Compressed ");
  }
}

Following is the output if you run the application:

C:\>java DefGetTotal C:\Work\Bharat\today\Deflater2\bharat.txt
Size of fille before Compression : 59
Size of fille after Compression : 19
The given file's data Successfully Compressed

Download this code

Ads