Skip to content

Using Qat‐Java in a Gradle Project

mulugetam edited this page Aug 18, 2023 · 7 revisions

Using Qat-Java in a Gradle Project

  1. Create a directory structure for your project:

    qat_java_example/
    ├── src/
    │   ├── main/
    │   │   └── java/
    │   │       └── com/
    │   │           └── example/
    │   │               └── ByteArrayExample.java
    │   └── test/
    └── build.gradle
    
  2. Create ByteArrayExample.java:

    Inside the src/main/java/com/example directory, create a file named ByteArrayExample.java with the following content.

    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();
        }
      }
    }
    
  3. Create build.gradle:

    In the root directory of your project, create a file named build.gradle with the following content. The file describes your project and its dependencies.

    apply plugin: 'application'
    
    repositories {
      mavenCentral()
    }
    
    dependencies {
      implementation 'com.intel.qat:qat-java:1.0.0'
    }
    
    application {
      mainClassName = 'com.example.ByteArrayExample'
    }
    
  4. Build and Run:

    Open a terminal, navigate to your project directory, and run the following command:

    gradle wrapper
    

    To build and run, use the following commands:

    ./gradlew build
    ./gradlew run
    

    If successful, you should see the below output in your terminal:

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