Skip to content

zrurf/LibDeflate4J

Repository files navigation

LibDeflate4J

Java 25 FFM Binding for libdeflate

English | 中文

Overview

LibDeflate4J is a high-performance Java binding for libdeflate (a heavily optimized library for DEFLATE/zlib/gzip compression and decompression.) This project leverages Java 25's Foreign Function & Memory (FFM) API to provide native-speed compression/decompression with a clean Java API.

Key Features

  • High Performance: Direct native binding via Java FFM API, minimal overhead
  • Modern Java: Uses Java 25 features including FFM API

System Requirements

  • Java: JDK 25 or later
  • Build Tools: Gradle 8.0+
  • Native Compiler:
    • Windows: MSVC (Visual Studio 2015+), MinGW, or Cygwin
    • Linux: GCC 4.9+ or Clang 3.9+
    • macOS: Xcode 8+ (Clang)
    • CMake: 3.10+ (automatically invoked during build)

Quick Start

Versions Number

The version number consists of the main version of libdeflate and the project revision version, for example, 1.25.1 is the first revision using libdeflate v1.25.

Add Dependency

We have not yet published this package to any repository, so you may need to clone this project and manually build it through Gradle.

Basic Usage

import io.github.zrurf.*;

public class Example {
    public static void main(String[] args) {
        // Compress data
        byte[] data = "Hello, World!".getBytes();
        
        try (Compressor compressor = Compressor.create(6)) {
            byte[] compressed = compressor.deflateCompress(data);
            
            // Decompress data
            try (Decompressor decompressor = Decompressor.create()) {
                byte[] decompressed = decompressor.deflateDecompress(compressed);
                System.out.println(new String(decompressed)); // "Hello, World!"
            }
        }
        
        // Calculate checksums
        int adler32 = LibDeflate4J.adler32(data);
        int crc32 = LibDeflate4J.crc32(data);
        System.out.printf("Adler-32: %d, CRC-32: %d%n", adler32, crc32);
    }
}

Build from Source

# Clone the repository
git clone https://github.com/zrurf/libdeflate4j.git
cd libdeflate4j

# Build with Gradle (requires CMake and C compiler)
./gradlew build

# Run tests
./gradlew test

# Install to local Maven repository
./gradlew publishToMavenLocal

API Documentation

Core Classes

  • LibDeflate4J: Static utility methods for checksums and compression bounds
  • Compressor: Compresses data using DEFLATE/zlib/gzip formats
  • Decompressor: Decompresses data from DEFLATE/zlib/gzip formats
  • Result: Enumeration of decompression result codes
  • Memory: Utility for safe native memory management

Compression Levels

  • 0: No compression (fastest)
  • 1-9: Increasing compression ratio (6 is default)
  • 10-12: Maximum compression (slowest)

Supported Formats

  1. DEFLATE: Raw DEFLATE format without headers
  2. zlib: RFC 1950 format with zlib headers
  3. gzip: RFC 1952 format with gzip headers

Cross-Platform Support

The build system automatically downloads and compiles libdeflate for the current platform:

Platform Library Format Compiler
Windows .dll MSVC/MinGW
Linux .so GCC/Clang
macOS .dylib Xcode command line tools

Testing

Run the comprehensive test suite:

./gradlew test --enable-native-access=ALL-UNNAMED --enable-preview

Tests include:

  • Round-trip compression/decompression
  • Checksum calculations
  • Edge cases (empty data, large data)
  • Multiple compression levels

Publishing

Currently, only publishing to local Maven repositories is supported, and remote repositories are not yet supported.

TODO

  • Publish to remote maven repository

License

This project is licensed under the MIT License.

See the LICENSE file for details.

Acknowledgments

  • libdeflate by Eric Biggers and other contributors
  • Java Foreign Function & Memory API team
  • All contributors and testers

About

Java 25 FFM Binding for libdeflate

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages