JNI bindings for Zstd native library that provides fast and high compression lossless algorithm for Java and all JVM languages.
npx @tessl/cli install tessl/maven-com-github-luben--zstd-jni@1.2.0Zstd-jni provides JNI (Java Native Interface) bindings for the Zstd (Zstandard) compression library, enabling Java and JVM language applications to use fast, lossless compression with high compression ratios. The library offers static compress/decompress methods for direct data compression, implements InputStream and OutputStream interfaces for transparent stream compression fully compatible with the standard zstd program, and maintains minimal performance overhead through efficient native bindings.
<dependency>
<groupId>com.github.luben</groupId>
<artifactId>zstd-jni</artifactId>
<version>1.2.0</version>
</dependency>import com.github.luben.zstd.Zstd;
import com.github.luben.zstd.ZstdInputStream;
import com.github.luben.zstd.ZstdOutputStream;
import com.github.luben.zstd.ZstdDictCompress;
import com.github.luben.zstd.ZstdDictDecompress;import com.github.luben.zstd.Zstd;
// Simple compression with default level
byte[] originalData = "Hello, World!".getBytes();
byte[] compressed = Zstd.compress(originalData);
// Decompress back to original
byte[] decompressed = Zstd.decompress(compressed, originalData.length);
String result = new String(decompressed); // "Hello, World!"
// Compression with specific level (1-22, higher = better compression)
byte[] compressedLevel10 = Zstd.compress(originalData, 10);Zstd-jni is built around several key components:
Core compression and decompression functionality using static methods. Ideal for one-shot operations on complete data sets with byte arrays and ByteBuffers.
// Basic compression
public static byte[] compress(byte[] src);
public static byte[] compress(byte[] src, int level);
public static long compress(byte[] dst, byte[] src, int level);
// Basic decompression
public static byte[] decompress(byte[] src, int originalSize);
public static long decompress(byte[] dst, byte[] src);
// ByteBuffer compression
public static int compress(ByteBuffer dstBuf, ByteBuffer srcBuf, int level);
public static ByteBuffer compress(ByteBuffer srcBuf, int level);
// ByteBuffer decompression
public static int decompress(ByteBuffer dstBuf, ByteBuffer srcBuf);
public static ByteBuffer decompress(ByteBuffer srcBuf, int originalSize);Static Compression and Decompression
Dictionary-based compression for improved compression ratios when compressing similar data. Supports both byte array dictionaries and pre-compiled dictionary objects.
// Dictionary compression with byte arrays
public static byte[] compressUsingDict(byte[] src, byte[] dict, int level);
public static long compress(byte[] dst, byte[] src, byte[] dict, int level);
// Dictionary compression with dictionary objects
public static byte[] compress(byte[] src, ZstdDictCompress dict);
public static int compress(ByteBuffer dstBuf, ByteBuffer srcBuf, ZstdDictCompress dict);
// Dictionary decompression
public static byte[] decompress(byte[] src, byte[] dict, int originalSize);
public static byte[] decompress(byte[] src, ZstdDictDecompress dict, int originalSize);Standard Java I/O streams for transparent compression and decompression. Compatible with existing Java I/O patterns and fully interoperable with the standard zstd command-line tool.
// Input stream for decompression
public ZstdInputStream(InputStream inStream) throws IOException;
public ZstdInputStream setContinuous(boolean b);
// Output stream for compression
public ZstdOutputStream(OutputStream outStream) throws IOException;
public ZstdOutputStream(OutputStream outStream, int level) throws IOException;
public ZstdOutputStream(OutputStream outStream, int level, boolean closeFrameOnFlush) throws IOException;High-performance streaming API for direct ByteBuffers with minimal memory copying. Suitable for high-throughput applications and custom buffer management strategies.
// Direct buffer compression stream
protected ZstdDirectBufferCompressingStream(ByteBuffer target, int level) throws IOException;
public void compress(ByteBuffer source) throws IOException;
public static int recommendedOutputBufferSize();
// Direct buffer decompression stream
public ZstdDirectBufferDecompressingStream(ByteBuffer source);
public int read(ByteBuffer target) throws IOException;
public static int recommendedTargetBufferSize();Utility methods for size estimation, error handling, dictionary creation, and accessing Zstd constants.
// Size and bounds
public static long compressBound(long srcSize);
public static long decompressedSize(byte[] src);
public static long decompressedSize(ByteBuffer srcBuf);
// Error handling
public static boolean isError(long code);
public static String getErrorName(long code);
// Dictionary training
public static long trainFromBuffer(byte[][] samples, byte[] dictBuffer);
// Zstd constants
public static int magicNumber();
public static int windowLogMin();
public static int windowLogMax();// Dictionary classes
class ZstdDictCompress implements Closeable {
public ZstdDictCompress(byte[] dict, int level);
public ZstdDictCompress(byte[] dict, int offset, int length, int level);
public void close() throws IOException;
}
class ZstdDictDecompress implements Closeable {
public ZstdDictDecompress(byte[] dict);
public ZstdDictDecompress(byte[] dict, int offset, int length);
public void close() throws IOException;
}