Exemple de méthode java.util.zip.DeflaterInputStream.skip ()

La description

le java.util.zip.DeflaterInputStream.skip(long n)La méthode saute et supprime les données du flux d'entrée. Cette méthode peut bloquer jusqu'à ce que le nombre d'octets spécifié soit lu et ignoré. Remarque: alors que n est donné comme un long, le nombre maximum d'octets qui peuvent être ignorés est Integer.MAX_VALUE.

Déclaration

Voici la déclaration pour java.util.zip.DeflaterInputStream.skip(long n) méthode.

public long skip(long n)
   throws IOException

Paramètres

  • n - nombre d'octets à sauter.

Retour

le nombre réel d'octets ignorés.

Exceptions

  • IOException - si une erreur d'E / S se produit ou si ce flux est déjà fermé.

Exemple

L'exemple suivant montre l'utilisation de la méthode java.util.zip.DeflaterInputStream.skip (long n).

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 DeflaterInputStreamDemo {
   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];
      System.out.println("Before Skipping, available: " + inflaterInputStream.available());
      inflaterInputStream.skip(result.length);
      System.out.println("After Skipping, available: " + inflaterInputStream.available());
      inflaterInputStream.close();     
   }
}

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
Before Skipping, available: 1
After Skipping, available: 0
Impression