Ejemplo de método java.util.zip.Inflater.inflate ()

Descripción

los java.util.zip.Inflater.inflate(byte[] b, int off, int len)El método descomprime bytes en el búfer especificado. Devuelve el número real de bytes sin comprimir. Un valor de retorno de 0 indica que se debe llamar a needInput () o needsDictionary () para determinar si se requieren más datos de entrada o un diccionario preestablecido. En el último caso, getAdler () se puede usar para obtener el valor Adler-32 del diccionario requerido.

Declaración

A continuación se muestra la declaración de java.util.zip.Inflater.inflate(byte[] b, int off, int len) método.

public int inflate(byte[] b, int off, int len)
   throws DataFormatException

Parámetros

  • b - el búfer para los datos sin comprimir.

  • off - el desplazamiento inicial de los datos.

  • len - el número máximo de bytes de datos sin comprimir.

Devoluciones

El número real de bytes de bytes sin comprimir.

Excepciones

  • DataFormatException - si el formato de datos comprimidos no es válido.

Ejemplo

El siguiente ejemplo muestra el uso del método java.util.zip.Inflater.inflate (byte [] b, int off, int len).

package com.tutorialspoint;

import java.io.UnsupportedEncodingException;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

public class InflaterDemo {
   public static void main(String[] args) 
      throws DataFormatException, UnsupportedEncodingException {
      String message = "Welcome to TutorialsPoint.com;"
         +"Welcome to TutorialsPoint.com;"
         +"Welcome to TutorialsPoint.com;"
         +"Welcome to TutorialsPoint.com;"
         +"Welcome to TutorialsPoint.com;"
         +"Welcome to TutorialsPoint.com;"
         +"Welcome to TutorialsPoint.com;"
         +"Welcome to TutorialsPoint.com;"
         +"Welcome to TutorialsPoint.com;"
         +"Welcome to TutorialsPoint.com;";
      System.out.println("Original Message length : " + message.length());
      byte[] input = message.getBytes("UTF-8");

      // Compress the bytes
      byte[] output = new byte[1024];
      Deflater deflater = new Deflater();
      deflater.setInput(input);
      deflater.finish();
      int compressedDataLength = deflater.deflate(output,0,output.length);
      deflater.end();

      System.out.println("Compressed Message length : " + compressedDataLength);

      // Decompress the bytes
      Inflater inflater = new Inflater();
      inflater.setInput(output, 0, compressedDataLength);
      byte[] result = new byte[1024];
      int resultLength = inflater.inflate(result, 0 , result.length);
      inflater.end();

      // Decode the bytes into a String
      message = new String(result, 0, resultLength, "UTF-8");
   
      System.out.println("UnCompressed Message length : " + message.length());
   }
}

Compilemos y ejecutemos el programa anterior, esto producirá el siguiente resultado:

Original Message length : 300
Compressed Message length : 42
UnCompressed Message length : 300
Impresión