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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.fastlz;
\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
21 import org.simantics.fastlz.java.FastLZImpl;
\r
22 import org.simantics.fastlz.java.FastLZJavaInputStream;
\r
23 import org.simantics.fastlz.java.FastLZJavaOutputStream;
\r
26 * A Java port of the native {@link FastLZ} library.
\r
28 * @author Tuukka Lehtonen
\r
32 * @see FastLZJavaInputStream
\r
33 * @see FastLZJavaOutputStream
\r
35 public class FastLZJava {
\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
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
47 * If the input is not compressible, the return value might be larger than
\r
48 * length (input buffer size).
\r
51 * The input buffer and the output buffer can not overlap.
\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
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
64 * The input buffer and the output buffer can not overlap.
\r
67 * Decompression is memory safe and guaranteed not to write the output
\r
68 * buffer more than what is specified in maxout.
\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
75 * @param file the FastLZ-compressed file to read
\r
76 * @return input stream that decompresses its output using the FastLZ
\r
78 * @throws FileNotFoundException see
\r
79 * {@link FileOutputStream#FileOutputStream(File)} for when this is
\r
82 public static InputStream read(File file) throws FileNotFoundException {
\r
83 return new FastLZJavaInputStream(new FileInputStream(file));
\r
87 * @param file the FastLZ-compressed file to write
\r
88 * @return output stream that compresses its input using the FastLZ
\r
90 * @throws FileNotFoundException see
\r
91 * {@link FileOutputStream#FileOutputStream(File)} for when this is
\r
94 public static OutputStream write(File file) throws FileNotFoundException {
\r
95 return new FastLZJavaOutputStream(new FileOutputStream(file));
\r