JNI bindings for Zstd native library that provides fast and high compression lossless algorithm for Java and all JVM languages.
—
Standard Java I/O streams for transparent compression and decompression. ZstdInputStream and ZstdOutputStream integrate seamlessly with existing Java I/O patterns and are fully compatible with the standard zstd command-line tool.
Transparent decompression of Zstd-compressed data through standard InputStream interface.
/**
* Creates a decompressing InputStream that reads compressed data from underlying stream
* @param inStream underlying InputStream containing compressed data
* @throws IOException if initialization fails
*/
public ZstdInputStream(InputStream inStream) throws IOException;
/**
* Sets continuous mode for handling unfinished frames
* @param b true to enable continuous mode (don't fail on unfinished frames)
* @return this instance for method chaining
*/
public ZstdInputStream setContinuous(boolean b);
/**
* Gets current continuous mode setting
* @return true if continuous mode is enabled
*/
public boolean getContinuous();
/**
* Reads decompressed data into buffer
* @param dst destination buffer
* @param offset offset in destination buffer
* @param len maximum number of bytes to read
* @return number of bytes read, or -1 if end of stream
* @throws IOException if decompression fails
*/
public int read(byte[] dst, int offset, int len) throws IOException;
/**
* Reads a single decompressed byte
* @return byte value (0-255) or -1 if end of stream
* @throws IOException if decompression fails
*/
public int read() throws IOException;
/**
* Returns estimate of bytes available for reading without blocking
* @return estimated available bytes
* @throws IOException if stream is closed
*/
public int available() throws IOException;
/**
* Mark/reset not supported
* @return false (mark/reset not supported)
*/
public boolean markSupported();
/**
* Skips bytes in the decompressed stream
* @param toSkip number of bytes to skip
* @return actual number of bytes skipped
* @throws IOException if decompression fails
*/
public long skip(long toSkip) throws IOException;
/**
* Closes the stream and underlying InputStream
* @throws IOException if close fails
*/
public void close() throws IOException;Usage Examples:
import com.github.luben.zstd.ZstdInputStream;
import java.io.*;
// Decompress a file
try (FileInputStream fis = new FileInputStream("data.zst");
ZstdInputStream zis = new ZstdInputStream(fis);
FileOutputStream fos = new FileOutputStream("data.txt")) {
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = zis.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
}
}
// Decompress from byte array
byte[] compressedData = ...; // compressed data
try (ByteArrayInputStream bais = new ByteArrayInputStream(compressedData);
ZstdInputStream zis = new ZstdInputStream(bais)) {
// Read decompressed data
byte[] buffer = new byte[1024];
int bytesRead = zis.read(buffer);
String result = new String(buffer, 0, bytesRead);
}
// Continuous mode for streaming/incomplete data
try (ZstdInputStream zis = new ZstdInputStream(inputStream).setContinuous(true)) {
// Will not fail on unfinished frames - useful for live streaming
byte[] buffer = new byte[1024];
int bytesRead = zis.read(buffer);
}Transparent compression of data through standard OutputStream interface.
/**
* Creates a compressing OutputStream with default settings
* @param outStream underlying OutputStream for compressed data
* @throws IOException if initialization fails
*/
public ZstdOutputStream(OutputStream outStream) throws IOException;
/**
* Creates a compressing OutputStream with specified compression level
* @param outStream underlying OutputStream for compressed data
* @param level compression level (1-22, higher = better compression)
* @throws IOException if initialization fails
*/
public ZstdOutputStream(OutputStream outStream, int level) throws IOException;
/**
* Creates a compressing OutputStream with full control
* @param outStream underlying OutputStream for compressed data
* @param level compression level (1-22, higher = better compression)
* @param closeFrameOnFlush true to close frame on flush (enables frame-by-frame processing)
* @throws IOException if initialization fails
*/
public ZstdOutputStream(OutputStream outStream, int level, boolean closeFrameOnFlush) throws IOException;
/**
* Writes data to be compressed
* @param src source data buffer
* @param offset offset in source buffer
* @param len number of bytes to write
* @throws IOException if compression fails
*/
public void write(byte[] src, int offset, int len) throws IOException;
/**
* Writes a single byte to be compressed
* @param i byte value to write
* @throws IOException if compression fails
*/
public void write(int i) throws IOException;
/**
* Flushes compressed data to underlying stream
* @throws IOException if flush fails
*/
public void flush() throws IOException;
/**
* Closes the stream and finishes compression
* @throws IOException if close fails
*/
public void close() throws IOException;Usage Examples:
import com.github.luben.zstd.ZstdOutputStream;
import java.io.*;
// Compress a file
try (FileInputStream fis = new FileInputStream("data.txt");
FileOutputStream fos = new FileOutputStream("data.zst");
ZstdOutputStream zos = new ZstdOutputStream(fos, 6)) {
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
zos.write(buffer, 0, bytesRead);
}
}
// Compress to byte array
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (ZstdOutputStream zos = new ZstdOutputStream(baos, 10)) {
String data = "Data to compress";
zos.write(data.getBytes());
}
byte[] compressedData = baos.toByteArray();
// Frame-by-frame compression for streaming
try (ZstdOutputStream zos = new ZstdOutputStream(outputStream, 6, true)) {
// Each flush creates a complete frame
zos.write("First chunk".getBytes());
zos.flush(); // Creates first frame
zos.write("Second chunk".getBytes());
zos.flush(); // Creates second frame
}Common patterns for integrating with Java I/O.
Buffered I/O:
// Wrap with BufferedStreams for better performance
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("input.zst"));
ZstdInputStream zis = new ZstdInputStream(bis);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("output.txt"))) {
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = zis.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);
}
}Try-with-resources:
// Automatic resource management
try (InputStream is = Files.newInputStream(Paths.get("input.zst"));
ZstdInputStream zis = new ZstdInputStream(is);
Reader reader = new InputStreamReader(zis, StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(reader)) {
return br.lines().collect(Collectors.toList());
}Chain with other streams:
// Decompress -> decrypt -> parse
try (ZstdInputStream zis = new ZstdInputStream(encryptedInputStream);
CipherInputStream cis = new CipherInputStream(zis, cipher);
ObjectInputStream ois = new ObjectInputStream(cis)) {
return (MyObject) ois.readObject();
}closeFrameOnFlush=true for streaming applications where frames need to be self-containedStream operations throw IOException on errors. The underlying Zstd error information is included in the exception message:
try (ZstdInputStream zis = new ZstdInputStream(inputStream)) {
// Stream operations
} catch (IOException e) {
// e.getMessage() contains Zstd error details if compression/decompression failed
System.err.println("Compression error: " + e.getMessage());
}Install with Tessl CLI
npx tessl i tessl/maven-com-github-luben--zstd-jni