CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-com-github-luben--zstd-jni

JNI bindings for Zstd native library that provides fast and high compression lossless algorithm for Java and all JVM languages.

Pending
Overview
Eval results
Files

static-compression.mddocs/

Static Compression and Decompression

Core compression and decompression functionality using static methods from the Zstd class. These methods provide direct, one-shot compression and decompression operations on byte arrays and ByteBuffers without requiring stream setup.

Capabilities

Basic Byte Array Compression

Compress byte arrays with automatic or specified compression levels.

/**
 * Compresses data using default compression level (3)
 * @param src source data to compress
 * @return compressed data as byte array
 */
public static byte[] compress(byte[] src);

/**
 * Compresses data using specified compression level
 * @param src source data to compress
 * @param level compression level (1-22, higher = better compression)
 * @return compressed data as byte array
 */
public static byte[] compress(byte[] src, int level);

/**
 * Compresses source into destination buffer
 * @param dst destination buffer (must be sized using compressBound)
 * @param src source data to compress
 * @param level compression level (1-22)
 * @return number of bytes written to dst, or error code (check with isError)
 */
public static long compress(byte[] dst, byte[] src, int level);

Usage Examples:

import com.github.luben.zstd.Zstd;

// Simple compression with default level
String text = "This is some text to compress";
byte[] data = text.getBytes();
byte[] compressed = Zstd.compress(data);

// Compression with specific level
byte[] compressedHigh = Zstd.compress(data, 19); // High compression
byte[] compressedFast = Zstd.compress(data, 1);  // Fast compression

// Pre-allocated destination buffer
long maxSize = Zstd.compressBound(data.length);
byte[] dst = new byte[(int) maxSize];
long actualSize = Zstd.compress(dst, data, 10);
if (Zstd.isError(actualSize)) {
    throw new RuntimeException("Compression failed: " + Zstd.getErrorName(actualSize));
}

Basic Byte Array Decompression

Decompress byte arrays back to original data.

/**
 * Decompresses data into new byte array
 * @param src compressed data
 * @param originalSize size of original uncompressed data
 * @return decompressed data as byte array
 */
public static byte[] decompress(byte[] src, int originalSize);

/**
 * Decompresses source into destination buffer
 * @param dst destination buffer (must be sized to original size)
 * @param src compressed data
 * @return number of bytes written to dst, or error code (check with isError)
 */
public static long decompress(byte[] dst, byte[] src);

Usage Examples:

// Decompress with known original size
int originalSize = data.length; // Saved from before compression
byte[] decompressed = Zstd.decompress(compressed, originalSize);
String result = new String(decompressed);

// Decompress with pre-allocated buffer
byte[] dst = new byte[originalSize];
long actualSize = Zstd.decompress(dst, compressed);
if (Zstd.isError(actualSize)) {
    throw new RuntimeException("Decompression failed: " + Zstd.getErrorName(actualSize));
}

ByteBuffer Compression

High-performance compression for direct ByteBuffers with minimal copying.

/**
 * Compresses direct ByteBuffer into another direct ByteBuffer
 * @param dstBuf destination buffer (must be direct, position marks write start)
 * @param srcBuf source buffer (must be direct, position to limit defines data)
 * @param level compression level (1-22)
 * @return number of bytes written to destination
 */
public static int compress(ByteBuffer dstBuf, ByteBuffer srcBuf, int level);

/**
 * Compresses direct ByteBuffer and returns new ByteBuffer with result
 * @param srcBuf source buffer (must be direct, position to limit defines data)
 * @param level compression level (1-22)
 * @return new direct ByteBuffer containing compressed data
 */
public static ByteBuffer compress(ByteBuffer srcBuf, int level);

/**
 * Native direct buffer compression (low-level method)
 * @param dst destination buffer
 * @param dstOffset offset in destination
 * @param dstSize maximum bytes to write
 * @param src source buffer  
 * @param srcOffset offset in source
 * @param srcSize bytes to read from source
 * @param level compression level
 * @return bytes written or error code
 */
public static long compressDirectByteBuffer(ByteBuffer dst, int dstOffset, int dstSize, 
                                           ByteBuffer src, int srcOffset, int srcSize, int level);

Usage Examples:

import java.nio.ByteBuffer;

// Compress direct ByteBuffers
ByteBuffer srcBuf = ByteBuffer.allocateDirect(1024);
srcBuf.put("Some data to compress".getBytes());
srcBuf.flip(); // Set position to 0, limit to data end

ByteBuffer dstBuf = ByteBuffer.allocateDirect((int) Zstd.compressBound(srcBuf.remaining()));
int compressedSize = Zstd.compress(dstBuf, srcBuf, 6);

// Or get new buffer with compressed data
ByteBuffer compressed = Zstd.compress(srcBuf, 6);

ByteBuffer Decompression

High-performance decompression for direct ByteBuffers.

/**
 * Decompresses direct ByteBuffer into another direct ByteBuffer
 * @param dstBuf destination buffer (must be direct, position marks write start)
 * @param srcBuf source buffer (must be direct, position to limit defines data)
 * @return number of bytes written to destination
 */
public static int decompress(ByteBuffer dstBuf, ByteBuffer srcBuf);

/**
 * Decompresses direct ByteBuffer and returns new ByteBuffer with result
 * @param srcBuf source buffer (must be direct, position to limit defines data)
 * @param originalSize size of original uncompressed data
 * @return new direct ByteBuffer containing decompressed data
 */
public static ByteBuffer decompress(ByteBuffer srcBuf, int originalSize);

/**
 * Native direct buffer decompression (low-level method)
 * @param dst destination buffer
 * @param dstOffset offset in destination
 * @param dstSize maximum bytes to write
 * @param src source buffer
 * @param srcOffset offset in source
 * @param srcSize bytes to read from source
 * @return bytes written or error code
 */
public static long decompressDirectByteBuffer(ByteBuffer dst, int dstOffset, int dstSize,
                                             ByteBuffer src, int srcOffset, int srcSize);

Usage Examples:

// Decompress direct ByteBuffers
ByteBuffer dstBuf = ByteBuffer.allocateDirect(originalSize);
int decompressedSize = Zstd.decompress(dstBuf, compressed);

// Or get new buffer with decompressed data
ByteBuffer decompressed = Zstd.decompress(compressed, originalSize);
decompressed.flip(); // Prepare for reading

Error Handling

All compression and decompression methods can return error codes. Always check for errors using the utility methods:

long result = Zstd.compress(dst, src, level);
if (Zstd.isError(result)) {
    throw new RuntimeException("Compression failed: " + Zstd.getErrorName(result));
}

Performance Considerations

  • ByteBuffer methods: Use direct ByteBuffers for best performance with minimal copying
  • Pre-allocation: Use compressBound() to pre-allocate destination buffers
  • Compression levels: Level 1-3 for speed, 10+ for better compression ratios
  • Buffer reuse: Reuse ByteBuffers when possible to reduce allocation overhead

Install with Tessl CLI

npx tessl i tessl/maven-com-github-luben--zstd-jni

docs

dictionary-compression.md

direct-buffer-streaming.md

index.md

static-compression.md

stream-compression.md

utility-functions.md

tile.json