1 package net.jpountz.lz4;
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 import java.nio.ByteBuffer;
20 import net.jpountz.util.ByteBufferUtils;
21 import net.jpountz.util.SafeUtils;
25 * {@link LZ4FastDecompressor} implemented with JNI bindings to the original C
26 * implementation of LZ4.
28 final class LZ4JNIFastDecompressor extends LZ4FastDecompressor {
30 public static final LZ4JNIFastDecompressor INSTANCE = new LZ4JNIFastDecompressor();
31 private static LZ4FastDecompressor SAFE_INSTANCE;
34 public final int decompress(byte[] src, int srcOff, byte[] dest, int destOff, int destLen) {
35 SafeUtils.checkRange(src, srcOff);
36 SafeUtils.checkRange(dest, destOff, destLen);
37 final int result = LZ4JNI.LZ4_decompress_fast(src, null, srcOff, dest, null, destOff, destLen);
39 throw new LZ4Exception("Error decoding offset " + (srcOff - result) + " of input buffer");
45 public int decompress(ByteBuffer src, int srcOff, ByteBuffer dest, int destOff, int destLen) {
46 ByteBufferUtils.checkNotReadOnly(dest);
47 ByteBufferUtils.checkRange(src, srcOff);
48 ByteBufferUtils.checkRange(dest, destOff, destLen);
50 if ((src.hasArray() || src.isDirect()) && (dest.hasArray() || dest.isDirect())) {
51 byte[] srcArr = null, destArr = null;
52 ByteBuffer srcBuf = null, destBuf = null;
55 srcOff += src.arrayOffset();
57 assert src.isDirect();
60 if (dest.hasArray()) {
61 destArr = dest.array();
62 destOff += dest.arrayOffset();
64 assert dest.isDirect();
68 final int result = LZ4JNI.LZ4_decompress_fast(srcArr, srcBuf, srcOff, destArr, destBuf, destOff, destLen);
70 throw new LZ4Exception("Error decoding offset " + (srcOff - result) + " of input buffer");
74 LZ4FastDecompressor safeInstance = SAFE_INSTANCE;
75 if (safeInstance == null) {
76 safeInstance = SAFE_INSTANCE = LZ4Factory.safeInstance().fastDecompressor();
78 return safeInstance.decompress(src, srcOff, dest, destOff, destLen);