]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.lz4/src/net/jpountz/xxhash/XXHash64JNI.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.lz4 / src / net / jpountz / xxhash / XXHash64JNI.java
1 package net.jpountz.xxhash;
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 import static net.jpountz.util.ByteBufferUtils.checkRange;
18 import static net.jpountz.util.SafeUtils.checkRange;
19
20 import java.nio.ByteBuffer;
21
22 final class XXHash64JNI extends XXHash64 {
23
24   public static final XXHash64 INSTANCE = new XXHash64JNI();
25   private static XXHash64 SAFE_INSTANCE;
26
27   @Override
28   public long hash(byte[] buf, int off, int len, long seed) {
29     checkRange(buf, off, len);
30     return XXHashJNI.XXH64(buf, off, len, seed);
31   }
32
33   @Override
34   public long hash(ByteBuffer buf, int off, int len, long seed) {
35     if (buf.isDirect()) {
36       checkRange(buf, off, len);
37       return XXHashJNI.XXH64BB(buf, off, len, seed);
38     } else if (buf.hasArray()) {
39       return hash(buf.array(), off + buf.arrayOffset(), len, seed);
40     } else {
41       XXHash64 safeInstance = SAFE_INSTANCE;
42       if (safeInstance == null) {
43         safeInstance = SAFE_INSTANCE = XXHashFactory.safeInstance().hash64();
44       }
45       return safeInstance.hash(buf, off, len, seed);
46     }
47   }
48
49 }