Use of getBytesRead, getByteWritten and getRemaining method of Inflater class in java.


 

 Use of getBytesRead, getByteWritten and getRemaining method of Inflater class in java.

In this tutorial, you see use of getBytesRead, getByteWritten and getRemaining method of Inflater class in java.

In this tutorial, you see use of getBytesRead, getByteWritten and getRemaining method of Inflater class in java.

Use of getBytesRead, getByteWritten and getRemaining method of Inflater class in java.

In this tutorial, we will discuss about getBytesRead(), getByteWritten() and getRemaining() method of Inflater class. The Inflater class provide support for Decompression using ZLIB library. The getBytesRead() method returns total number of compressed bytes of input stream read by inflater. It works same as getTotalIn(), but returns long instead of integer. The getByteWritten()  method returns total number of Uncompressed bytes.. It works same as getTotalOut(), but returns long instead of integer. The getRemaining() method returns total number of bytes remains in associated input buffer.. 
            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.

About  Inflater API:

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

Return Type Method Description 
long getBytesRead() Function getBytesRead() returns number of compress bytes from associated input stream.
long getTotalWritten() Function getBytesWritten() returns number of Uncompress bytes from associated output stream.
int getRemaining() The getRemaining() method returns total number of bytes remains in associated input buffer.

Code

import java.io.*;
import java.io.FileInputStream;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;
import java.io.IOException;

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

  public static void getBytes() throws IOException {
    FileInputStream inStream = new FileInputStream(infile);
    Inflater infl = new Inflater();
    InflaterInputStream inflInstream = new InflaterInputStream(inStream,
        infl);
    boolean completed = false;
    byte b[] new byte[1024];
    try {

      while (true) {
        int len = inflInstream.read(b, 01024);

        if (len == -1) {
          break;
        }
        System.out.println("Compressed file : " + infile);
        long inBytes = infl.getBytesRead();
        System.out.println("Number of bytes of Compressed data : "
            + inBytes + "bytes");

      }
      int inRemain = infl.getRemaining();
      System.out.println("Remaining bytes after decompression data : "
          + inRemain + "bytes");

      long outBytes = infl.getBytesWritten();
      System.out.println("Number of bytes of Uncompressed data : "
          + outBytes + "bytes");

      completed = true;
      inflInstream.close();
    catch (IOException ioe) {
      System.out.println("IOException : " + ioe);
    }
  }
}

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

Following is the output if you run the application:

C:\>java InflaterGetBytes
Compressed file : testfile.txt
Number of bytes of Compressed data : 19bytes
Remaining bytes after decompression data : 0bytes
Number of bytes of Uncompressed data : 59bytes

Download this code

Ads