Brotli4j
Brotli4j provides Brotli compression and decompression for Java.
Maven
<dependency>
<groupId>com.aayushatharva.brotli4j</groupId>
<artifactId>brotli4j</artifactId>
<version>1.2.2</version>
</dependency>
Supported Platforms:
Windows 64-Bit
Linux 64-Bit
macOS Catalina 10.15
Loading native library:
Call Brotli4jLoader.ensureAvailability()
in your application once before using Brotli4j.
Examples
Direct API
// Load the native library
Brotli4jLoader.ensureAvailability();
// Compress data and get output in byte array
byte[] compressed = Encoder.compress("Meow".getBytes());
// Decompress data and get output in DirectDecompress
DirectDecompress directDecompress = Decoder.decompress(compressed); // or DirectDecompress.decompress(compressed);
if (directDecompress.getResultStatus() == DecoderJNI.Status.DONE) {
System.out.println("Decompression Successful: " + new String(directDecompress.getDecompressedData()));
} else {
System.out.println("Some Error Occurred While Decompressing");
}
Compressing a stream:
// Load the native library
Brotli4jLoader.ensureAvailability();
FileInputStream inFile = new FileInputStream(filePath);
FileOutputStream outFile = new FileOutputStream(filePath + ".br");
Encoder.Parameters params = new Encoder.Parameters().setQuality(4);
BrotliOutputStream brotliOutputStream = new BrotliOutputStream(outFile, params);
int read = inFile.read();
while(read > -1) {
brotliOutputStream.write(read);
read = inFile.read();
}
// Close the BrotliOutputStream. This also closes the FileOutputStream.
brotliOutputStream.close();
inFile.close();
Decompressing a stream:
// Load the native library
Brotli4jLoader.ensureAvailability();
FileInputStream inFile = new FileInputStream(filePath);
FileOutputStream outFile = new FileOutputStream(decodedfilePath);
BrotliInputStream brotliInputStream = new BrotliInputStream(inFile);
int read = brotliInputStream.read();
while(read > -1) {
outFile.write(read);
read = brotliInputStream.read();
}
// Close the BrotliInputStream. This also closes the FileInputStream.
brotliInputStream.close();
outFile.close();
Sponsors
JProfiler is supporting Brotli4J with its full-featured Java Profiler. JProfiler's intuitive UI helps you resolve performance bottlenecks, pin down memory leaks and understand threading issues. Click below to know more: