]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.lz4/src/org/simantics/lz4/Main.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.lz4 / src / org / simantics / lz4 / Main.java
1 package org.simantics.lz4;
2
3 import java.io.UnsupportedEncodingException;
4
5 import net.jpountz.lz4.LZ4Compressor;
6 import net.jpountz.lz4.LZ4Factory;
7 import net.jpountz.lz4.LZ4FastDecompressor;
8 import net.jpountz.lz4.LZ4SafeDecompressor;
9
10 public class Main {
11
12         public static void main(String[] args) throws UnsupportedEncodingException {
13                 LZ4Factory factory = LZ4Factory.fastestInstance();
14
15                 byte[] data = "12345345234572".getBytes("UTF-8");
16                 final int decompressedLength = data.length;
17
18                 // compress data
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);
23
24                 // decompress data
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);
30
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);
36         }
37
38 }