1 package org.simantics.lz4;
3 import java.io.UnsupportedEncodingException;
5 import net.jpountz.lz4.LZ4Compressor;
6 import net.jpountz.lz4.LZ4Factory;
7 import net.jpountz.lz4.LZ4FastDecompressor;
8 import net.jpountz.lz4.LZ4SafeDecompressor;
12 public static void main(String[] args) throws UnsupportedEncodingException {
13 LZ4Factory factory = LZ4Factory.fastestInstance();
15 byte[] data = "12345345234572".getBytes("UTF-8");
16 final int decompressedLength = data.length;
19 LZ4Compressor compressor = factory.fastCompressor();
20 int maxCompressedLength = compressor.maxCompressedLength(decompressedLength);
21 byte[] compressed = new byte[maxCompressedLength];
22 int compressedLength = compressor.compress(data, 0, decompressedLength, compressed, 0, maxCompressedLength);
25 // - method 1: when the decompressed length is known
26 LZ4FastDecompressor decompressor = factory.fastDecompressor();
27 byte[] restored = new byte[decompressedLength];
28 int compressedLength2 = decompressor.decompress(compressed, 0, restored, 0, decompressedLength);
29 System.out.println(compressedLength == compressedLength2);
31 // - method 2: when the compressed length is known (a little slower)
32 // the destination buffer needs to be over-sized
33 LZ4SafeDecompressor decompressor2 = factory.safeDecompressor();
34 int decompressedLength2 = decompressor2.decompress(compressed, 0, compressedLength, restored, 0);
35 System.out.println(decompressedLength == decompressedLength2);