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