]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.lz4/src/org/simantics/lz4/LZ4.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.lz4 / src / org / simantics / lz4 / LZ4.java
1 package org.simantics.lz4;
2
3 import java.io.File;
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;
9
10 import net.jpountz.lz4.LZ4Factory;
11 import net.jpountz.util.Native;
12
13 import org.simantics.compressions.impl.Buffers;
14 import org.simantics.lz4.impl.LZ4InputStream;
15 import org.simantics.lz4.impl.LZ4OutputStream;
16
17 public class LZ4 {
18
19         private static LZ4Factory INSTANCE = null;
20         
21     static {
22         Native.load();
23         INSTANCE = LZ4Factory.fastestInstance();
24     }
25     
26     public static LZ4Factory getInstance() {
27         return INSTANCE;
28     }
29     
30     public static boolean isNativeInitialized() {
31         return Native.isLoaded();
32     }
33
34     /**
35      * The output buffer must be at least 5% larger than the input buffer and
36      * can not be smaller than 66 bytes.
37      * 
38      * @param inputSize size of uncompressed input data in bytes
39      * @return maximum amount of bytes needed for the compressed data
40      */
41     public static int compressBound(int inputSize) {
42         return INSTANCE.fastCompressor().maxCompressedLength(inputSize);
43     }
44     
45     /**
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.
49      * 
50      * <p>
51      * The output buffer must be at least 5% larger than the input buffer and
52      * can not be smaller than 66 bytes.
53      * 
54      * <p>
55      * If the input is not compressible, the return value might be larger than
56      * length (input buffer size).
57      * 
58      * <p>
59      * The input buffer and the output buffer can not overlap.
60      * 
61      * <p>
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.
66      */
67     public static int compressBuffer(ByteBuffer input, int inputOffset, int length,
68             ByteBuffer output, int outputOffset) {
69         
70         if (output.isReadOnly())
71             throw new IllegalArgumentException("read-only output buffer");
72
73         if (input.isDirect() && output.isDirect()) {
74             return INSTANCE.fastCompressor().compress(input, inputOffset, length, output, outputOffset, output.capacity() - outputOffset);
75         }
76
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);
81         return result;
82     }
83
84     /**
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
88      * returned instead.
89      * 
90      * <p>
91      * The input buffer and the output buffer can not overlap.
92      * 
93      * <p>
94      * Decompression is memory safe and guaranteed not to write the output
95      * buffer more than what is specified in maxout.
96      * 
97      * <p>
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.
102      */
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");
107
108         if (input.isDirect() && output.isDirect())
109             return INSTANCE.safeDecompressor().decompress(input, inputOffset, length, output, outputOffset, maxout);
110         
111
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);
116         return result;
117     }
118
119     /**
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
125      *         thrown
126      */
127     public static InputStream read(File file) throws FileNotFoundException {
128         return new LZ4InputStream(file);
129     }
130
131     /**
132      * @param input
133      * @return
134      * @param input
135      *            the input stream to decompress
136      * @return a stream that decompresses the specified FastLZ compressed input
137      *         stream
138      */
139     public static InputStream read(InputStream input) {
140         return new LZ4InputStream(input);
141     }
142
143     /**
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
149      *         thrown
150      */
151     public static OutputStream write(File file) throws FileNotFoundException {
152         return new LZ4OutputStream(file);
153     }
154
155     /**
156      * @param output
157      *            the stream to write compressed output into
158      * @return a stream that compresses into the specified output stream with
159      *         FastLZ
160      */
161     public static OutputStream write(OutputStream output) {
162         return new LZ4OutputStream(output);
163     }
164
165 }