Esempio del metodo java.util.zip.Inflater.setDictionary ()

Descrizione

Il java.util.zip.Inflater.setDictionary(byte[] b, int off, int len)Il metodo imposta il dizionario preimpostato sull'array di byte specificato. Dovrebbe essere chiamato quando inflate () restituisce 0 e needsDictionary () restituisce true indicando che è richiesto un dizionario preimpostato. Il metodo getAdler () può essere utilizzato per ottenere il valore Adler-32 del dizionario necessario.

Dichiarazione

Di seguito è riportata la dichiarazione per java.util.zip.Inflater.setDictionary(byte[] b, int off, int len) metodo.

public void setDictionary(byte[] b, int off, int len)

Parametri

  • b - i byte di dati del dizionario.

  • off - l'offset iniziale dei dati.

  • len - la lunghezza dei dati.

Esempio

L'esempio seguente mostra l'utilizzo del metodo java.util.zip.Inflater.setDictionary (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;";
      String dictionary = "Welcome";
 
      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.setDictionary(dictionary.getBytes("UTF-8"),0, dictionary.length());
      deflater.setInput(input);
   
      deflater.finish();
      int compressedDataLength = deflater.deflate(output,0 , output.length, Deflater.NO_FLUSH);
      System.out.println("Total uncompressed bytes output :" + deflater.getTotalOut());
      System.out.println("Compressed Message Checksum :" + deflater.getAdler());     
      deflater.finished();

      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);
      if(inflater.needsDictionary()){
    
         inflater.setDictionary(dictionary.getBytes("UTF-8"),0, dictionary.length());
 
         resultLength = inflater.inflate(result);
      }
      inflater.finished();

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

Compiliamo ed eseguiamo il programma sopra, questo produrrà il seguente risultato:

Original Message length : 300
Total uncompressed bytes output :42
Compressed Message Checksum :368538129
Compressed Message length : 42
UnCompressed Message Checksum :368538129
UnCompressed Message length : 300
Stampa