Exemple de méthode java.util.zip.Inflater.read ()

La description

le java.util.zip.InflaterInputStream.read(byte[] buf, int off, int len)La méthode lit les données non compressées dans un tableau d'octets. Si len n'est pas égal à zéro, la méthode se bloquera jusqu'à ce qu'une entrée puisse être décompressée; sinon, aucun octet n'est lu et 0 est renvoyé.

Déclaration

Voici la déclaration pour java.util.zip.InflaterInputStream.read(byte[] buf, int off, int len) méthode.

public int read(byte[] buf, int off, int len)
   throws IOException

Paramètres

  • buf - le tampon dans lequel les données sont lues.

  • off - le décalage de départ dans le tableau de destination b.

  • len - le nombre maximum d'octets lus.

Retour

le nombre réel d'octets lus, ou -1 si la fin de l'entrée compressée est atteinte ou si un dictionnaire prédéfini est nécessaire.

Exceptions

  • NullPointerException - Si buf est nul.

  • IndexOutOfBoundsException - Si off est négatif, len est négatif ou len est supérieur à buf.length - off.

  • ZipException - si une erreur de format ZIP s'est produite.

  • IOException - si une erreur d'E / S s'est produite.

Exemple

L'exemple suivant montre l'utilisation de la méthode java.util.zip.InflaterInputStream.read (byte [] buf, int off, int len).

package com.tutorialspoint;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.InflaterInputStream;

public class InflaterInputStreamDemo {
   public static void main(String[] args) throws DataFormatException, IOException {
      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");
      int length = message.length();
      // 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.NO_FLUSH);
      System.out.println("Total uncompressed bytes input :" + deflater.getTotalIn());
      System.out.println("Compressed Message Checksum :" + deflater.getAdler());     
      deflater.finished();

      System.out.println("Compressed Message length : " + compressedDataLength);
   
      ByteArrayInputStream bin = new ByteArrayInputStream(output);
      InflaterInputStream inflaterInputStream = new InflaterInputStream(bin);
      byte[] result = new byte[1024];

      inflaterInputStream.read(result, 0, result.length);

      inflaterInputStream.close();
      // Decode the bytes into a String
      String message1 = new String(result,0, length,"UTF-8");
      System.out.println(message.equals(message1));
   }
}

Compilons et exécutons le programme ci-dessus, cela produira le résultat suivant -

Original Message length : 300
Total uncompressed bytes input :300
Compressed Message Checksum :368538129
Compressed Message length : 42
true
Impression