]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.lz4/src/net/jpountz/lz4/LZ4JNIFastDecompressor.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.lz4 / src / net / jpountz / lz4 / LZ4JNIFastDecompressor.java
1 package net.jpountz.lz4;
2
3 /*
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17
18 import java.nio.ByteBuffer;
19
20 import net.jpountz.util.ByteBufferUtils;
21 import net.jpountz.util.SafeUtils;
22
23
24 /**
25  * {@link LZ4FastDecompressor} implemented with JNI bindings to the original C
26  * implementation of LZ4.
27  */
28 final class LZ4JNIFastDecompressor extends LZ4FastDecompressor {
29
30   public static final LZ4JNIFastDecompressor INSTANCE = new LZ4JNIFastDecompressor();
31   private static LZ4FastDecompressor SAFE_INSTANCE;
32
33   @Override
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);
38     if (result < 0) {
39       throw new LZ4Exception("Error decoding offset " + (srcOff - result) + " of input buffer");
40     }
41     return result;
42   }
43
44   @Override
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);
49
50     if ((src.hasArray() || src.isDirect()) && (dest.hasArray() || dest.isDirect())) {
51       byte[] srcArr = null, destArr = null;
52       ByteBuffer srcBuf = null, destBuf = null;
53       if (src.hasArray()) {
54         srcArr = src.array();
55         srcOff += src.arrayOffset();
56       } else {
57         assert src.isDirect();
58         srcBuf = src;
59       }
60       if (dest.hasArray()) {
61         destArr = dest.array();
62         destOff += dest.arrayOffset();
63       } else {
64         assert dest.isDirect();
65         destBuf = dest;
66       }
67
68       final int result = LZ4JNI.LZ4_decompress_fast(srcArr, srcBuf, srcOff, destArr, destBuf, destOff, destLen);
69       if (result < 0) {
70         throw new LZ4Exception("Error decoding offset " + (srcOff - result) + " of input buffer");
71       }
72       return result;
73     } else {
74       LZ4FastDecompressor safeInstance = SAFE_INSTANCE;
75       if (safeInstance == null) {
76         safeInstance = SAFE_INSTANCE = LZ4Factory.safeInstance().fastDecompressor();
77       }
78       return safeInstance.decompress(src, srcOff, dest, destOff, destLen);
79     }
80   }
81
82 }