This repository was archived by the owner on Dec 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 284
Expand file tree
/
Copy pathStreamingXXHash64JNI.java
More file actions
111 lines (92 loc) · 2.91 KB
/
StreamingXXHash64JNI.java
File metadata and controls
111 lines (92 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package net.jpountz.xxhash;
import static net.jpountz.util.ByteBufferUtils.checkRange;
import static net.jpountz.util.SafeUtils.checkRange;
import java.nio.ByteBuffer;
/*
* Copyright 2020 Linnaea Von Lavia and the lz4-java contributors.
*
* 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.
*/
/**
* Fast {@link StreamingXXHash64} implemented with JNI bindings.
* The methods are synchronized to avoid a race condition
* between freeing the native memory in finalize() and using it in
* reset(), getValue(), and update(). Note that GC can call finalize()
* after calling checkState() and before using XXHashJNI if the caller
* does not retain a reference to this object.
*/
final class StreamingXXHash64JNI extends StreamingXXHash64 {
static class Factory implements StreamingXXHash64.Factory {
public static final StreamingXXHash64.Factory INSTANCE = new Factory();
@Override
public StreamingXXHash64 newStreamingHash(long seed) {
return new StreamingXXHash64JNI(seed);
}
}
private long state;
StreamingXXHash64JNI(long seed) {
super(seed);
state = XXHashJNI.XXH64_init(seed);
}
private void checkState() {
if (state == 0) {
throw new AssertionError("Already finalized");
}
}
@Override
public synchronized void reset() {
checkState();
XXHashJNI.XXH64_free(state);
state = XXHashJNI.XXH64_init(seed);
}
@Override
public synchronized long getValue() {
checkState();
return XXHashJNI.XXH64_digest(state);
}
@Override
public synchronized void update(byte[] bytes, int off, int len) {
checkState();
XXHashJNI.XXH64_update(state, bytes, off, len);
}
@Override
public synchronized void update(ByteBuffer buf, int off, int len) {
checkState();
if (buf.isDirect()) {
checkRange(buf, off, len);
XXHashJNI.XXH64BB_update(state, buf, off, len);
} else if (buf.hasArray()) {
XXHashJNI.XXH64_update(state, buf.array(), off + buf.arrayOffset(), len);
} else {
// XXX: What to do here?
throw new RuntimeException("unsupported");
}
}
@Override
public synchronized void close() {
if (state != 0) {
super.close();
XXHashJNI.XXH64_free(state);
state = 0;
}
}
@Override
protected synchronized void finalize() throws Throwable {
super.finalize();
if (state != 0) {
// free memory
XXHashJNI.XXH64_free(state);
state = 0;
}
}
}