]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.lz4/src/net/jpountz/xxhash/StreamingXXHash64JNI.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.lz4 / src / net / jpountz / xxhash / StreamingXXHash64JNI.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
18 final class StreamingXXHash64JNI extends StreamingXXHash64 {
19
20   static class Factory implements StreamingXXHash64.Factory {
21
22     public static final StreamingXXHash64.Factory INSTANCE = new Factory();
23
24     @Override
25     public StreamingXXHash64 newStreamingHash(long seed) {
26       return new StreamingXXHash64JNI(seed);
27     }
28
29   }
30
31   private long state;
32
33   StreamingXXHash64JNI(long seed) {
34     super(seed);
35     state = XXHashJNI.XXH64_init(seed);
36   }
37
38   private void checkState() {
39     if (state == 0) {
40       throw new AssertionError("Already finalized");
41     }
42   }
43
44   @Override
45   public void reset() {
46     checkState();
47     XXHashJNI.XXH64_free(state);
48     state = XXHashJNI.XXH64_init(seed);
49   }
50
51   @Override
52   public long getValue() {
53     checkState();
54     return XXHashJNI.XXH64_digest(state);
55   }
56
57   @Override
58   public void update(byte[] bytes, int off, int len) {
59     checkState();
60     XXHashJNI.XXH64_update(state, bytes, off, len);
61   }
62
63   @Override
64   protected void finalize() throws Throwable {
65     super.finalize();
66     // free memory
67     XXHashJNI.XXH64_free(state);
68     state = 0;
69   }
70
71 }