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 XXHash32}.
24 * This API is compatible with the {@link XXHash32 block API} and the following
25 * code samples are equivalent:
26 * <pre class="prettyprint">
27 * int hash(XXHashFactory xxhashFactory, byte[] buf, int off, int len, int seed) {
28 * return xxhashFactory.hash32().hash(buf, off, len, seed);
31 * <pre class="prettyprint">
32 * int hash(XXHashFactory xxhashFactory, byte[] buf, int off, int len, int seed) {
33 * StreamingXXHash32 sh32 = xxhashFactory.newStreamingHash32(seed);
34 * sh32.update(buf, off, len);
35 * return sh32.getValue();
39 * Instances of this class are <b>not</b> thread-safe.
41 public abstract class StreamingXXHash32 {
45 StreamingXXHash32 newStreamingHash(int seed);
51 StreamingXXHash32(int seed) {
56 * Get the value of the checksum.
58 public abstract int 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 StreamingXXHash32.this.getValue() & 0xFFFFFFFL;
90 StreamingXXHash32.this.reset();
94 public void update(int b) {
95 StreamingXXHash32.this.update(new byte[] {(byte) b}, 0, 1);
99 public void update(byte[] b, int off, int len) {
100 StreamingXXHash32.this.update(b, off, len);
104 public String toString() {
105 return StreamingXXHash32.this.toString();