]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.lz4/src/org/simantics/lz4/LZ4.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.lz4 / src / org / simantics / lz4 / LZ4.java
diff --git a/bundles/org.simantics.lz4/src/org/simantics/lz4/LZ4.java b/bundles/org.simantics.lz4/src/org/simantics/lz4/LZ4.java
new file mode 100644 (file)
index 0000000..7e5255e
--- /dev/null
@@ -0,0 +1,165 @@
+package org.simantics.lz4;\r
+\r
+import java.io.File;\r
+import java.io.FileNotFoundException;\r
+import java.io.FileOutputStream;\r
+import java.io.InputStream;\r
+import java.io.OutputStream;\r
+import java.nio.ByteBuffer;\r
+\r
+import net.jpountz.lz4.LZ4Factory;\r
+import net.jpountz.util.Native;\r
+\r
+import org.simantics.compressions.impl.Buffers;\r
+import org.simantics.lz4.impl.LZ4InputStream;\r
+import org.simantics.lz4.impl.LZ4OutputStream;\r
+\r
+public class LZ4 {\r
+\r
+       private static LZ4Factory INSTANCE = null;\r
+       \r
+    static {\r
+        Native.load();\r
+        INSTANCE = LZ4Factory.fastestInstance();\r
+    }\r
+    \r
+    public static LZ4Factory getInstance() {\r
+        return INSTANCE;\r
+    }\r
+    \r
+    public static boolean isNativeInitialized() {\r
+        return Native.isLoaded();\r
+    }\r
+\r
+    /**\r
+     * The output buffer must be at least 5% larger than the input buffer and\r
+     * can not be smaller than 66 bytes.\r
+     * \r
+     * @param inputSize size of uncompressed input data in bytes\r
+     * @return maximum amount of bytes needed for the compressed data\r
+     */\r
+    public static int compressBound(int inputSize) {\r
+        return INSTANCE.fastCompressor().maxCompressedLength(inputSize);\r
+    }\r
+    \r
+    /**\r
+     * Compress a block of data in the input buffer and returns the size of\r
+     * compressed block. The size of input buffer is specified by length. The\r
+     * minimum input buffer size is 16.\r
+     * \r
+     * <p>\r
+     * The output buffer must be at least 5% larger than the input buffer and\r
+     * can not be smaller than 66 bytes.\r
+     * \r
+     * <p>\r
+     * If the input is not compressible, the return value might be larger than\r
+     * length (input buffer size).\r
+     * \r
+     * <p>\r
+     * The input buffer and the output buffer can not overlap.\r
+     * \r
+     * <p>\r
+     * It is recommended to have both input buffers as direct or heap buffers,\r
+     * not mixed. Mixing different types of buffers will hurt performance a lot.\r
+     * If both buffers are direct byte buffers and native decompression is\r
+     * available, it will be employed.\r
+     */\r
+    public static int compressBuffer(ByteBuffer input, int inputOffset, int length,\r
+            ByteBuffer output, int outputOffset) {\r
+        \r
+        if (output.isReadOnly())\r
+            throw new IllegalArgumentException("read-only output buffer");\r
+\r
+        if (input.isDirect() && output.isDirect()) {\r
+            return INSTANCE.fastCompressor().compress(input, inputOffset, length, output, outputOffset, output.capacity() - outputOffset);\r
+        }\r
+\r
+        byte[] inarr = Buffers.getInputArray(input);\r
+        byte[] outarr = Buffers.getOutputArray(output);\r
+        int result = INSTANCE.fastCompressor().compress(inarr, inputOffset, length, outarr, outputOffset, outarr.length - outputOffset);\r
+        Buffers.writeOutput(output, outarr);\r
+        return result;\r
+    }\r
+\r
+    /**\r
+     * Decompress a block of compressed data and returns the size of the\r
+     * decompressed block. If error occurs, e.g. the compressed data is\r
+     * corrupted or the output buffer is not large enough, then 0 (zero) will be\r
+     * returned instead.\r
+     * \r
+     * <p>\r
+     * The input buffer and the output buffer can not overlap.\r
+     * \r
+     * <p>\r
+     * Decompression is memory safe and guaranteed not to write the output\r
+     * buffer more than what is specified in maxout.\r
+     * \r
+     * <p>\r
+     * It is recommended to have both input buffers as direct or heap buffers,\r
+     * not mixed. Mixing different types of buffers will hurt performance a lot.\r
+     * If both buffers are direct byte buffers and native decompression is\r
+     * available, it will be employed.\r
+     */\r
+    public static int decompressBuffer(ByteBuffer input, int inputOffset, int length,\r
+            ByteBuffer output, int outputOffset, int maxout) {\r
+        if (output.isReadOnly())\r
+            throw new IllegalArgumentException("read-only output buffer");\r
+\r
+        if (input.isDirect() && output.isDirect())\r
+            return INSTANCE.safeDecompressor().decompress(input, inputOffset, length, output, outputOffset, maxout);\r
+        \r
+\r
+        byte[] inarr = Buffers.getInputArray(input);\r
+        byte[] outarr = Buffers.getOutputArray(output);\r
+        int result = INSTANCE.safeDecompressor().decompress(inarr, inputOffset, length, outarr, outputOffset, maxout);\r
+        Buffers.writeOutput(output, outarr);\r
+        return result;\r
+    }\r
+\r
+    /**\r
+     * @param file the FastLZ-compressed file to read\r
+     * @return input stream that decompresses its output using the FastLZ\r
+     *         algorithm. Caller is responsible of closing the returned stream.\r
+     * @throws FileNotFoundException see\r
+     *         {@link FileOutputStream#FileOutputStream(File)} for when this is\r
+     *         thrown\r
+     */\r
+    public static InputStream read(File file) throws FileNotFoundException {\r
+        return new LZ4InputStream(file);\r
+    }\r
+\r
+    /**\r
+     * @param input\r
+     * @return\r
+     * @param input\r
+     *            the input stream to decompress\r
+     * @return a stream that decompresses the specified FastLZ compressed input\r
+     *         stream\r
+     */\r
+    public static InputStream read(InputStream input) {\r
+        return new LZ4InputStream(input);\r
+    }\r
+\r
+    /**\r
+     * @param file the FastLZ-compressed file to write\r
+     * @return output stream that compresses its input using the FastLZ\r
+     *         algorithm. Caller is responsible of closing the returned stream.\r
+     * @throws FileNotFoundException see\r
+     *         {@link FileOutputStream#FileOutputStream(File)} for when this is\r
+     *         thrown\r
+     */\r
+    public static OutputStream write(File file) throws FileNotFoundException {\r
+        return new LZ4OutputStream(file);\r
+    }\r
+\r
+    /**\r
+     * @param output\r
+     *            the stream to write compressed output into\r
+     * @return a stream that compresses into the specified output stream with\r
+     *         FastLZ\r
+     */\r
+    public static OutputStream write(OutputStream output) {\r
+        return new LZ4OutputStream(output);\r
+    }\r
+\r
+}\r