X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.fastlz%2Fsrc%2Forg%2Fsimantics%2Ffastlz%2FFastLZJava.java;fp=bundles%2Forg.simantics.fastlz%2Fsrc%2Forg%2Fsimantics%2Ffastlz%2FFastLZJava.java;h=3e0f4523e2df7c241cded18b49668e81de01d597;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 diff --git a/bundles/org.simantics.fastlz/src/org/simantics/fastlz/FastLZJava.java b/bundles/org.simantics.fastlz/src/org/simantics/fastlz/FastLZJava.java new file mode 100644 index 000000000..3e0f4523e --- /dev/null +++ b/bundles/org.simantics.fastlz/src/org/simantics/fastlz/FastLZJava.java @@ -0,0 +1,98 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.fastlz; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.OutputStream; + +import org.simantics.fastlz.java.FastLZImpl; +import org.simantics.fastlz.java.FastLZJavaInputStream; +import org.simantics.fastlz.java.FastLZJavaOutputStream; + +/** + * A Java port of the native {@link FastLZ} library. + * + * @author Tuukka Lehtonen + * + * @see FastLZ + * @see FastLZImpl + * @see FastLZJavaInputStream + * @see FastLZJavaOutputStream + */ +public class FastLZJava { + + /** + * 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. + * + *

+ * The output buffer must be at least 5% larger than the input buffer and + * can not be smaller than 66 bytes. + * + *

+ * If the input is not compressible, the return value might be larger than + * length (input buffer size). + * + *

+ * The input buffer and the output buffer can not overlap. + */ + public static int compress(byte[] input, int inputOffset, int length, byte[] output, int outputOffset) { + return FastLZImpl.fastlz_compress(input, inputOffset, length, output, outputOffset); + } + + /** + * 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. + * + *

+ * The input buffer and the output buffer can not overlap. + * + *

+ * Decompression is memory safe and guaranteed not to write the output + * buffer more than what is specified in maxout. + */ + public static int decompress(byte[] input, int inputOffset, int length, byte[] output, int outputOffset, int maxout) { + return FastLZImpl.fastlz_decompress(input, inputOffset, length, output, outputOffset, maxout); + } + + /** + * @param file the FastLZ-compressed file to read + * @return input stream that decompresses its output using the FastLZ + * algorithm + * @throws FileNotFoundException see + * {@link FileOutputStream#FileOutputStream(File)} for when this is + * thrown + */ + public static InputStream read(File file) throws FileNotFoundException { + return new FastLZJavaInputStream(new FileInputStream(file)); + } + + /** + * @param file the FastLZ-compressed file to write + * @return output stream that compresses its input using the FastLZ + * algorithm + * @throws FileNotFoundException see + * {@link FileOutputStream#FileOutputStream(File)} for when this is + * thrown + */ + public static OutputStream write(File file) throws FileNotFoundException { + return new FastLZJavaOutputStream(new FileOutputStream(file)); + } + +}