1 package org.simantics.lz4;
4 import java.io.FileNotFoundException;
5 import java.io.FileOutputStream;
6 import java.io.InputStream;
7 import java.io.OutputStream;
8 import java.nio.ByteBuffer;
10 import net.jpountz.lz4.LZ4Factory;
11 import net.jpountz.util.Native;
13 import org.simantics.compressions.impl.Buffers;
14 import org.simantics.lz4.impl.LZ4InputStream;
15 import org.simantics.lz4.impl.LZ4OutputStream;
19 private static LZ4Factory INSTANCE = null;
23 INSTANCE = LZ4Factory.fastestInstance();
26 public static LZ4Factory getInstance() {
30 public static boolean isNativeInitialized() {
31 return Native.isLoaded();
35 * The output buffer must be at least 5% larger than the input buffer and
36 * can not be smaller than 66 bytes.
38 * @param inputSize size of uncompressed input data in bytes
39 * @return maximum amount of bytes needed for the compressed data
41 public static int compressBound(int inputSize) {
42 return INSTANCE.fastCompressor().maxCompressedLength(inputSize);
46 * Compress a block of data in the input buffer and returns the size of
47 * compressed block. The size of input buffer is specified by length. The
48 * minimum input buffer size is 16.
51 * The output buffer must be at least 5% larger than the input buffer and
52 * can not be smaller than 66 bytes.
55 * If the input is not compressible, the return value might be larger than
56 * length (input buffer size).
59 * The input buffer and the output buffer can not overlap.
62 * It is recommended to have both input buffers as direct or heap buffers,
63 * not mixed. Mixing different types of buffers will hurt performance a lot.
64 * If both buffers are direct byte buffers and native decompression is
65 * available, it will be employed.
67 public static int compressBuffer(ByteBuffer input, int inputOffset, int length,
68 ByteBuffer output, int outputOffset) {
70 if (output.isReadOnly())
71 throw new IllegalArgumentException("read-only output buffer");
73 if (input.isDirect() && output.isDirect()) {
74 return INSTANCE.fastCompressor().compress(input, inputOffset, length, output, outputOffset, output.capacity() - outputOffset);
77 byte[] inarr = Buffers.getInputArray(input);
78 byte[] outarr = Buffers.getOutputArray(output);
79 int result = INSTANCE.fastCompressor().compress(inarr, inputOffset, length, outarr, outputOffset, outarr.length - outputOffset);
80 Buffers.writeOutput(output, outarr);
85 * Decompress a block of compressed data and returns the size of the
86 * decompressed block. If error occurs, e.g. the compressed data is
87 * corrupted or the output buffer is not large enough, then 0 (zero) will be
91 * The input buffer and the output buffer can not overlap.
94 * Decompression is memory safe and guaranteed not to write the output
95 * buffer more than what is specified in maxout.
98 * It is recommended to have both input buffers as direct or heap buffers,
99 * not mixed. Mixing different types of buffers will hurt performance a lot.
100 * If both buffers are direct byte buffers and native decompression is
101 * available, it will be employed.
103 public static int decompressBuffer(ByteBuffer input, int inputOffset, int length,
104 ByteBuffer output, int outputOffset, int maxout) {
105 if (output.isReadOnly())
106 throw new IllegalArgumentException("read-only output buffer");
108 if (input.isDirect() && output.isDirect())
109 return INSTANCE.safeDecompressor().decompress(input, inputOffset, length, output, outputOffset, maxout);
112 byte[] inarr = Buffers.getInputArray(input);
113 byte[] outarr = Buffers.getOutputArray(output);
114 int result = INSTANCE.safeDecompressor().decompress(inarr, inputOffset, length, outarr, outputOffset, maxout);
115 Buffers.writeOutput(output, outarr);
120 * @param file the FastLZ-compressed file to read
121 * @return input stream that decompresses its output using the FastLZ
122 * algorithm. Caller is responsible of closing the returned stream.
123 * @throws FileNotFoundException see
124 * {@link FileOutputStream#FileOutputStream(File)} for when this is
127 public static InputStream read(File file) throws FileNotFoundException {
128 return new LZ4InputStream(file);
135 * the input stream to decompress
136 * @return a stream that decompresses the specified FastLZ compressed input
139 public static InputStream read(InputStream input) {
140 return new LZ4InputStream(input);
144 * @param file the FastLZ-compressed file to write
145 * @return output stream that compresses its input using the FastLZ
146 * algorithm. Caller is responsible of closing the returned stream.
147 * @throws FileNotFoundException see
148 * {@link FileOutputStream#FileOutputStream(File)} for when this is
151 public static OutputStream write(File file) throws FileNotFoundException {
152 return new LZ4OutputStream(file);
157 * the stream to write compressed output into
158 * @return a stream that compresses into the specified output stream with
161 public static OutputStream write(OutputStream output) {
162 return new LZ4OutputStream(output);