Brotli4j

repository·main·Indexed 41 days ago

https://github.com/hyperxpro/brotli4j

Brotli compression and decompression capabilities for Java applications using native bindings. Version 1.23.0 supports various architectures across Windows, Linux, and macOS, providing a Direct API for byte arrays and stream-based processing via BrotliOutputStream and BrotliInputStream.

Tokens
5.1K
Snippets
13
Records
17
Agent score
100%

What's inside Brotli4j

  1. Manage Brotli4j versions with Gradle BOM

    main

    Use the Brotli4j BOM in Gradle to maintain version consistency across multiple Brotli4j artifacts. This is useful when referencing individual native-* artifacts.

    // Gradle Kotlin DSL
    dependencies {
        implementation(platform("com.aayushatharva.brotli4j:brotli4j-bom:1.23.0"))
    
        implementation("com.aayushatharva.brotli4j:brotli4j")
        // Optional: pin a specific runtime native (version managed by the BOM)
        runtimeOnly("com.aayushatharva.brotli4j:native-linux-x86_64")
    }
    // Gradle Groovy DSL
    dependencies {
        implementation platform('com.aayushatharva.brotli4j:brotli4j-bom:1.23.0')
    
        implementation 'com.aayushatharva.brotli4j:brotli4j'
        // Optional: pin a specific runtime native (version managed by the BOM)
        runtimeOnly 'com.aayushatharva.brotli4j:native-linux-x86_64'
    }
  2. Configure Shadow JAR for all Brotli4j architectures in Gradle

    main

    When using the shadow plugin, you can configure a custom configuration (e.g., shaded) to include natives for all architectures in the resulting shaded JAR. This prevents the distribution from only containing the natives for the architecture it was compiled on.

    // Example for Gradle Kotlin DSL (Subproject-specific)
    val shaded by configurations.registering {
        extendsFrom(configurations.runtimeClasspath)
    
        isCanBeConsumed = false
        isCanBeResolved = true
        isCanBeDeclared = true
    
        attributes {
            attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage.JAVA_RUNTIME))
            attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category.LIBRARY))
            attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, objects.named(LibraryElements.JAR))
    
            attribute(OperatingSystemFamily.OPERATING_SYSTEM_ATTRIBUTE, objects.named("all"))
            attribute(MachineArchitecture.ARCHITECTURE_ATTRIBUTE, objects.named("all"))
        }
    }
    
    dependencies {
        shaded("com.aayushatharva.brotli4j:natives:1.23.0")
    }
    
    tasks {
        shadowJar.configure {
            // shadow 8.x
            configurations = listOf(project.configurations.shaded).map { it.get() }
            // shadow 9.x
            configurations.add(project.configurations.shaded)
        }
    }
  3. Install Brotli4j via Gradle

    main

    To use Brotli4j in your Gradle project, add the dependency to your build.gradle file using the group ID com.aayushatharva.brotli4j.

    // Example Gradle dependency
    implementation 'com.aayushatharva.brotli4j:brotli4j:[VERSION]'
  4. Use the Brotli CLI for compression and decompression

    main

    The brotli command-line tool provides three operation modes: compression (default), decompression (--decompress), and integrity testing (--test).

    Operation Modes

    • Compression: The default mode. It appends a suffix (default .br) to the source filename.
    • Decompression: Activated with --decompress or -d. It removes the suffix from the source filename.
    • Integrity Test: Activated with --test or -t. This is equivalent to --decompress --stdout, but the decompressed data is discarded instead of being written to standard output.

    Shortcuts

    • unbrotli is a shortcut for brotli --decompress.
    • brcat is a shortcut for brotli --decompress --concatenated --stdout.

    Input and Output

    • If no files are provided, or if the file argument is -, brotli reads from standard input.
    • To write to standard output, use the --stdout or -c option.
    • To specify a specific output filename, use --output=FILE or -o FILE (only valid if there is a single input entry).
    • By default, source files are preserved. Use --rm or -j to remove source files after processing.
    # Compress a file
    brotli input.txt
    
    # Decompress a file
    brotli --decompress input.txt.br
    
    # Decompress and write to stdout
    brotli -d -c input.txt.br > output.txt
    
    # Compress with a specific quality level (0-11)
    brotli -q 9 input.txt
    
    # Test file integrity
    brotli --test input.txt.br
  5. Configure automatic native resolution in Gradle

    main

    To allow Gradle to automatically resolve the correct native artifacts for your architecture, you must implement the Brotli4JRule (a ComponentMetadataRule). This can be applied in two ways:

    1. Project-wide: Add the rule class and global rule usage to settings.gradle.kts or settings.gradle. This is best for multi-project builds.
    2. Subproject-specific: Add the rule class and usage to the specific build.gradle.kts or build.gradle file. This is best for single-subproject builds.

    Note: This method uses internal Gradle APIs and requires Gradle 8.0+. It is compatible with the shadow plugin for creating shaded JARs containing all architectures.

  6. Install Brotli4j via Gradle

    main

    For standard Gradle usage, add the brotli4j dependency and the natives dependency. Note that to automatically resolve the correct native artifacts based on your architecture, you must also include the Brotli4JRule dependency metadata rule (see Gradle configuration guide).

    dependencies {
        implementation("com.aayushatharva.brotli4j:brotli4j:1.23.0")
        runtimeOnly("com.aayushatharva.brotli4j:natives:1.23.0")
    }
  7. Manage Brotli4j versions with Maven BOM

    main

    If you are using multiple Brotli4j artifacts (e.g., pinning a specific native runtime), use the Brotli4j Bill of Materials (BOM) to ensure all dependencies use a consistent version. When using the BOM, declare Brotli4j artifacts without an explicit <version> tag.

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.aayushatharva.brotli4j</groupId>
                <artifactId>brotli4j-bom</artifactId>
                <version>1.23.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <dependencies>
        <dependency>
            <groupId>com.aayushatharva.brotli4j</groupId>
            <artifactId>brotli4j</artifactId>
        </dependency>
        <!-- Optional: pin a specific runtime native (version managed by the BOM) -->
        <dependency>
            <groupId>com.aayushatharva.brotli4j</groupId>
            <artifactId>native-linux-x86_64</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
  8. Load the Brotli4j native library

    main

    Before using Brotli4j, you must ensure the native library is loaded. The recommended way is to call Brotli4jLoader.ensureAvailability() once in your application. This method uses automatic dependency resolution to load the correct native library for your platform.

    If you need to load the native library from a custom location, you can specify the System Property brotli4j.library.path.

    // Call this once before using any Brotli4j APIs
    Brotli4jLoader.ensureAvailability();
  9. Install Brotli4j via Maven

    main

    To use Brotli4j in a Maven project, add the brotli4j dependency. The native libraries for your specific system family and architecture will be imported automatically.

    <dependency>
        <groupId>com.aayushatharva.brotli4j</groupId>
        <artifactId>brotli4j</artifactId>
        <version>1.23.0</version>
    </dependency>
  10. Run Brotli4j development environment with Docker Compose

    main

    You can use the provided docker-compose.yml to set up a consistent development and build environment. The configuration defines several services that share a common base configuration.

    Services

    • runtime-setup: An initialization service used to prepare the environment. It builds from Dockerfile.centos6.
    • build: A service designed to run the Maven build process. It executes mvn -B -ntp clean package --file pom.xml inside the container.
    • shell: An interactive service that provides a /bin/bash entrypoint, allowing you to run commands manually within the environment.

    Common Configuration

    All functional services (build and shell) use a common configuration template that includes:

    • Dependencies: They depend on the runtime-setup service being ready.
    • Volume Mounts:
      • ~/.m2:/root/.m2: Mounts your local Maven repository to the container to persist dependencies and speed up builds.
      • ..:/code: Mounts the project root into the /code working directory.
    • Working Directory: Set to /code.
    version: "3"
    
    services:
    runtime-setup:
        image: brotli4j:default
        build:
          context: .
          dockerfile: Dockerfile.centos6
    
    common: &common
        image: brotli4j:default
        depends_on: [ runtime-setup ]
        volumes:
          - ~/.m2:/root/.m2
          - ..:/code
        working_dir: /code
    
    build:
        <<: *common
        command: /bin/bash -cl "mvn -B -ntp clean package --file pom.xml"
    
    shell:
        <<: *common
        entrypoint: /bin/bash
  11. Platform compatibility notes

    main

    Windows-AArch64

    Brotli4j is compiled with JDK 11 but targets JDK 8 because JDK 8 Windows AArch64 builds are currently unavailable. It is compatible with JDK 8 and above.

    RISC-V64

    • This platform requires JDK 11+ (e.g., JDK 11, 17, 21).
    • While the native module bytecode is cross-compiled with JDK 8 to maintain compatibility with existing applications, you must use a JDK 11+ runtime to run Brotli4j on RISC-V64.