1 package net.jpountz.xxhash;
3 import java.util.zip.Checksum;
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
22 * Streaming interface for {@link XXHash64}.
24 * This API is compatible with the {@link XXHash64 block API} and the following
25 * code samples are equivalent:
26 * <pre class="prettyprint">
27 * long hash(XXHashFactory xxhashFactory, byte[] buf, int off, int len, long seed) {
28 * return xxhashFactory.hash64().hash(buf, off, len, seed);
31 * <pre class="prettyprint">
32 * long hash(XXHashFactory xxhashFactory, byte[] buf, int off, int len, long seed) {
33 * StreamingXXHash64 sh64 = xxhashFactory.newStreamingHash64(seed);
34 * sh64.update(buf, off, len);
35 * return sh64.getValue();
39 * Instances of this class are <b>not</b> thread-safe.
41 public abstract class StreamingXXHash64 {
45 StreamingXXHash64 newStreamingHash(long seed);
51 StreamingXXHash64(long seed) {
56 * Get the value of the checksum.
58 public abstract long getValue();
61 * Update the value of the hash with buf[off:off+len].
63 public abstract void update(byte[] buf, int off, int len);
66 * Reset this instance to the state it had right after instantiation. The
67 * seed remains unchanged.
69 public abstract void reset();
72 public String toString() {
73 return getClass().getSimpleName() + "(seed=" + seed + ")";
77 * Return a {@link Checksum} view of this instance. Modifications to the view
78 * will modify this instance too and vice-versa.
80 public final Checksum asChecksum() {
81 return new Checksum() {
84 public long getValue() {
85 return StreamingXXHash64.this.getValue();
90 StreamingXXHash64.this.reset();
94 public void update(int b) {
95 StreamingXXHash64.this.update(new byte[] {(byte) b}, 0, 1);
99 public void update(byte[] b, int off, int len) {
100 StreamingXXHash64.this.update(b, off, len);
104 public String toString() {
105 return StreamingXXHash64.this.toString();