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