Skip to content

Using Qat‐Java

mulugetam edited this page Aug 18, 2023 · 2 revisions

Example

  1. Create a directory for your example project:

    mkdir -p my_example_project
    
  2. Navigate to your project directory and create a directory for your package.

    cd my_example_project
    mkdir -p com/example
    
  3. Download the qat-java-1.0.0.jar from maven central.

    wget https://repo1.maven.org/maven2/com/intel/qat/qat-java/1.0.0/qat-java-1.0.0.jar 
    
  4. Create a ByteArrayExample.java file with the following content in the com/example directory:

    package com.example;
    
    import com.intel.qat.QatException;
    import com.intel.qat.QatZipper;
    
    public class ByteArrayExample {
     public static void main(String[] args) {
       try {
         String inputStr = "The quick brown fox jumps over the lazy dog.";
         byte[] input = inputStr.getBytes();
    
         // Create a QatZipper object
         QatZipper qzip = new QatZipper();
         
         // Create a buffer with enough size for compression
         byte[] compressedData = new byte[qzip.maxCompressedLength(input.length)];
    
         // Compress the bytes
         int compressedSize = qzip.compress(input, compressedData);
    
         // Decompress the bytes into a String
         byte[] decompressedData = new byte[input.length];
         int decompressedSize = qzip.decompress(compressedData, decompressedData);
    
         // Release resources
         qzip.end();
    
         // Convert the bytes into a String
         String outputStr = new String(decompressedData, 0, decompressedSize);
         System.out.println("Decompressed data: " + outputStr);
       } catch (QatException e) {
         e.printStackTrace();
       }
     }
    } 
    
  5. Compile the ByteArrayExample.java run the following command:

    javac -cp qat-java-1.0.0.jar com/example/ByteArrayExample.java 
    
  6. Run your example:

    java -cp .:qat-java-1.0.0.jar com.example.ByteArrayExample
    

    Output:

    Decompressed data: The quick brown fox jumps over the lazy dog.
    
Clone this wiki locally