Liking cljdoc? Tell your friends :D

zstd-java

A pure-Java Zstandard compressor and decompressor without sun.misc.Unsafe and without native code. It runs on Java 17+, on any platform and byte order, and in GraalVM native images.

It is a port of aircompressor's Java implementation. aircompressor reads memory through sun.misc.Unsafe, whose memory-access methods are deprecated for removal (JEP 471/498: they warn on JDK 24+ and throw by default from JDK 26), and requires Java 22+ and a little endian platform. This port reads through VarHandle byte-array views instead, with the codec otherwise unchanged.

Frames are standard Zstandard frames: they are read by the reference C library (zstd-jni), Node's built-in zlib zstd and any other conforming decoder, and this library reads theirs.

Installation

Clojars Project

org.replikativ/zstd-java {:mvn/version "RELEASE"}
<repository><id>clojars</id><url>https://repo.clojars.org/</url></repository>
<dependency>
  <groupId>org.replikativ</groupId>
  <artifactId>zstd-java</artifactId>
  <version><!-- see Clojars --></version>
</dependency>

Java 17 or newer. No other dependencies.

Usage

import org.replikativ.zstd.ZstdJavaCompressor;
import org.replikativ.zstd.ZstdJavaDecompressor;

ZstdJavaCompressor compressor = new ZstdJavaCompressor();   // thread safe, reuse it
byte[] out = new byte[compressor.maxCompressedLength(input.length)];
int compressedSize = compressor.compress(input, 0, input.length, out, 0, out.length);

ZstdJavaDecompressor decompressor = new ZstdJavaDecompressor();
byte[] restored = new byte[input.length];
decompressor.decompress(out, 0, compressedSize, restored, 0, restored.length);

ZstdJavaOutputStream and ZstdJavaInputStream implement the streaming format. For many small blocks prefer the block API: a stream allocates its window buffers per instance.

The compressor implements zstd level 3 (zstd's default); the decompressor reads frames of any level.

Differences from aircompressor

  • Memory replaces sun.misc.Unsafe: a buffer is a byte[] and an address is an index into it. Multi-byte values are little endian on every platform.
  • ZstdJavaCompressor pools its compression contexts (hash tables, sequence buffers) instead of allocating them per call. An instance is safe to share between threads.
  • Huffman leaves are bucket sorted (same order as aircompressor's insertion sort) and byte histograms are counted with four interleaved arrays.
  • Corrupt input the decoder overruns on is reported as MalformedInputException rather than ArrayIndexOutOfBoundsException or IllegalStateException.
  • No MemorySegment API, no native implementation, no Hadoop codecs.

Compressed output is byte-identical to aircompressor's.

Performance

Measured on 2,964 index nodes of a Datahike database (6.2 KB each) and the Canterbury and Calgary corpora, JDK 25, interleaved runs on a loaded machine (expect ±20 % between runs):

compressdecompressratio
this library, nodes0.6x zstd-jni0.73x zstd-jni5.80 (zstd-jni 5.82)
this library, corpus0.64x zstd-jni0.5x zstd-jni3.55 (zstd-jni 3.57)
this library vs aircompressor (Unsafe), nodes1.35-1.45x~1xsame bytes

Testing

test/compat.clj checks, against zstd-jni: round trips in both directions at levels 1/3/9/19 with and without checksums, the streaming classes, decompressed size, byte-identical output to aircompressor (also with eight threads sharing a compressor), and mutated or truncated frames, which must decode or raise MalformedInputException.

clojure -T:build javac
clojure -M:test -m compat                                   # generated inputs (CI)
clojure -M:test -m compat <corpus dirs...> <dir of .raw blobs>
clojure -M:smoke                                            # Java 17: zstd-jni round trips only
clojure -M:test -m bench <dir of .raw blobs> <corpus dir>

The full suite needs Java 22+ for its aircompressor oracle; :smoke runs on 17.

License

Apache License 2.0, as aircompressor. See LICENSE, NOTICE and NOTICE-aircompressor.md.

Can you improve this documentation?Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close