]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.lz4/src/net/jpountz/xxhash/StreamingXXHash64.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.lz4 / src / net / jpountz / xxhash / StreamingXXHash64.java
1 package net.jpountz.xxhash;
2
3 import java.util.zip.Checksum;
4
5 /*
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19
20
21 /**
22  * Streaming interface for {@link XXHash64}.
23  * <p>
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);
29  *   }
30  * </pre>
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();
36  *   }
37  * </pre>
38  * <p>
39  * Instances of this class are <b>not</b> thread-safe.
40  */
41 public abstract class StreamingXXHash64 {
42
43   interface Factory {
44
45     StreamingXXHash64 newStreamingHash(long seed);
46
47   }
48
49   final long seed;
50
51   StreamingXXHash64(long seed) {
52     this.seed = seed;
53   }
54
55   /**
56    * Get the value of the checksum.
57    */
58   public abstract long getValue();
59
60   /**
61    * Update the value of the hash with buf[off:off+len].
62    */
63   public abstract void update(byte[] buf, int off, int len);
64
65   /**
66    * Reset this instance to the state it had right after instantiation. The
67    * seed remains unchanged.
68    */
69   public abstract void reset();
70
71   @Override
72   public String toString() {
73     return getClass().getSimpleName() + "(seed=" + seed + ")";
74   }
75
76   /**
77    * Return a {@link Checksum} view of this instance. Modifications to the view
78    * will modify this instance too and vice-versa.
79    */
80   public final Checksum asChecksum() {
81     return new Checksum() {
82
83       @Override
84       public long getValue() {
85         return StreamingXXHash64.this.getValue();
86       }
87
88       @Override
89       public void reset() {
90         StreamingXXHash64.this.reset();
91       }
92
93       @Override
94       public void update(int b) {
95         StreamingXXHash64.this.update(new byte[] {(byte) b}, 0, 1);
96       }
97
98       @Override
99       public void update(byte[] b, int off, int len) {
100         StreamingXXHash64.this.update(b, off, len);
101       }
102
103       @Override
104       public String toString() {
105         return StreamingXXHash64.this.toString();
106       }
107
108     };
109   }
110
111 }