]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.fastlz/src/org/simantics/fastlz/FastLZJava.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.fastlz / src / org / simantics / fastlz / FastLZJava.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.fastlz;\r
13 \r
14 import java.io.File;\r
15 import java.io.FileInputStream;\r
16 import java.io.FileNotFoundException;\r
17 import java.io.FileOutputStream;\r
18 import java.io.InputStream;\r
19 import java.io.OutputStream;\r
20 \r
21 import org.simantics.fastlz.java.FastLZImpl;\r
22 import org.simantics.fastlz.java.FastLZJavaInputStream;\r
23 import org.simantics.fastlz.java.FastLZJavaOutputStream;\r
24 \r
25 /**\r
26  * A Java port of the native {@link FastLZ} library.\r
27  * \r
28  * @author Tuukka Lehtonen\r
29  * \r
30  * @see FastLZ\r
31  * @see FastLZImpl\r
32  * @see FastLZJavaInputStream\r
33  * @see FastLZJavaOutputStream\r
34  */\r
35 public class FastLZJava {\r
36 \r
37     /**\r
38      * Compress a block of data in the input buffer and returns the size of\r
39      * compressed block. The size of input buffer is specified by length. The\r
40      * minimum input buffer size is 16.\r
41      * \r
42      * <p>\r
43      * The output buffer must be at least 5% larger than the input buffer and\r
44      * can not be smaller than 66 bytes.\r
45      * \r
46      * <p>\r
47      * If the input is not compressible, the return value might be larger than\r
48      * length (input buffer size).\r
49      * \r
50      * <p>\r
51      * The input buffer and the output buffer can not overlap.\r
52      */\r
53     public static int compress(byte[] input, int inputOffset, int length, byte[] output, int outputOffset) {\r
54         return FastLZImpl.fastlz_compress(input, inputOffset, length, output, outputOffset);\r
55     }\r
56 \r
57     /**\r
58      * Decompress a block of compressed data and returns the size of the\r
59      * decompressed block. If error occurs, e.g. the compressed data is\r
60      * corrupted or the output buffer is not large enough, then 0 (zero) will be\r
61      * returned instead.\r
62      * \r
63      * <p>\r
64      * The input buffer and the output buffer can not overlap.\r
65      * \r
66      * <p>\r
67      * Decompression is memory safe and guaranteed not to write the output\r
68      * buffer more than what is specified in maxout.\r
69      */\r
70     public static int decompress(byte[] input, int inputOffset, int length, byte[] output, int outputOffset, int maxout) {\r
71         return FastLZImpl.fastlz_decompress(input, inputOffset, length, output, outputOffset, maxout);\r
72     }\r
73 \r
74     /**\r
75      * @param file the FastLZ-compressed file to read\r
76      * @return input stream that decompresses its output using the FastLZ\r
77      *         algorithm\r
78      * @throws FileNotFoundException see\r
79      *         {@link FileOutputStream#FileOutputStream(File)} for when this is\r
80      *         thrown\r
81      */\r
82     public static InputStream read(File file) throws FileNotFoundException {\r
83         return new FastLZJavaInputStream(new FileInputStream(file));\r
84     }\r
85 \r
86     /**\r
87      * @param file the FastLZ-compressed file to write\r
88      * @return output stream that compresses its input using the FastLZ\r
89      *         algorithm\r
90      * @throws FileNotFoundException see\r
91      *         {@link FileOutputStream#FileOutputStream(File)} for when this is\r
92      *         thrown\r
93      */\r
94     public static OutputStream write(File file) throws FileNotFoundException {\r
95         return new FastLZJavaOutputStream(new FileOutputStream(file));\r
96     }\r
97 \r
98 }\r