All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.jpountz.lz4.LZ4SafeDecompressor Maven / Gradle / Ivy

Go to download

Java ports and bindings of the LZ4 compression algorithm and the xxHash hashing algorithm

There is a newer version: 1.3.0
Show newest version
package net.jpountz.lz4;

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Arrays;

/**
 * LZ4 decompressor that requires the size of the compressed data to be known.
 * 

* Implementations of this class are usually a little slower than those of * {@link LZ4FastDecompressor} but do not require the size of the original data to * be known. */ public abstract class LZ4SafeDecompressor implements LZ4UnknownSizeDecompressor { /** * Uncompress src[srcOff:srcLen] into * dest[destOff:destOff+maxDestLen] and returns the number of * decompressed bytes written into dest. * * @param srcLen the exact size of the compressed stream * @return the original input size * @throws LZ4Exception if maxDestLen is too small */ public abstract int decompress(byte[] src, int srcOff, int srcLen, byte[] dest, int destOff, int maxDestLen); /** * Convenience method, equivalent to calling * {@link #decompress(byte[], int, int, byte[], int, int) decompress(src, srcOff, srcLen, dest, destOff, dest.length - destOff)}. */ public final int decompress(byte[] src, int srcOff, int srcLen, byte[] dest, int destOff) { return decompress(src, srcOff, srcLen, dest, destOff, dest.length - destOff); } /** * Convenience method, equivalent to calling * {@link #decompress(byte[], int, int, byte[], int) decompress(src, 0, src.length, dest, 0)} */ public final int decompress(byte[] src, byte[] dest) { return decompress(src, 0, src.length, dest, 0); } /** * Convenience method which returns src[srcOff:srcOff+srcLen] * decompressed. *

Warning: this method has an * important overhead due to the fact that it needs to allocate a buffer to * decompress into, and then needs to resize this buffer to the actual * decompressed length.

*

Here is how this method is implemented:

*
   * byte[] decompressed = new byte[maxDestLen];
   * final int decompressedLength = decompress(src, srcOff, srcLen, decompressed, 0, maxDestLen);
   * if (decompressedLength != decompressed.length) {
   *   decompressed = Arrays.copyOf(decompressed, decompressedLength);
   * }
   * return decompressed;
   * 
*/ public final byte[] decompress(byte[] src, int srcOff, int srcLen, int maxDestLen) { byte[] decompressed = new byte[maxDestLen]; final int decompressedLength = decompress(src, srcOff, srcLen, decompressed, 0, maxDestLen); if (decompressedLength != decompressed.length) { decompressed = Arrays.copyOf(decompressed, decompressedLength); } return decompressed; } /** * Convenience method, equivalent to calling * {@link #decompress(byte[], int, int, int) decompress(src, 0, src.length, maxDestLen)}. */ public final byte[] decompress(byte[] src, int maxDestLen) { return decompress(src, 0, src.length, maxDestLen); } @Override public String toString() { return getClass().getSimpleName(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy