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