|
| 1 | +/** |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0. |
| 4 | + */ |
| 5 | +package software.amazon.awssdk.crt.checksums; |
| 6 | +import software.amazon.awssdk.crt.CrtResource; |
| 7 | + |
| 8 | +public class XXHash extends CrtResource { |
| 9 | + |
| 10 | + private XXHash(long nativeHandle) { |
| 11 | + acquireNativeHandle(nativeHandle); |
| 12 | + } |
| 13 | + |
| 14 | + /** |
| 15 | + * Determines whether a resource releases its dependencies at the same time the native handle is released or if it waits. |
| 16 | + * Resources that wait are responsible for calling releaseReferences() manually. |
| 17 | + */ |
| 18 | + @Override |
| 19 | + protected boolean canReleaseReferencesImmediately() { return true; } |
| 20 | + |
| 21 | + /** |
| 22 | + * Releases the instance's reference to the underlying native key pair |
| 23 | + */ |
| 24 | + @Override |
| 25 | + protected void releaseNativeHandle() { |
| 26 | + if (!isNull()) { |
| 27 | + xxHashRelease(getNativeHandle()); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Create new streaming XXHash64. |
| 33 | + * @param seed seed to use for the hash |
| 34 | + * @return new XXHash instance |
| 35 | + */ |
| 36 | + static public XXHash newXXHash64(long seed) { |
| 37 | + long nativeHandle = xxHash64Create(seed); |
| 38 | + return new XXHash(nativeHandle); |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | + /** |
| 43 | + * Create new streaming XXHash64. |
| 44 | + * @return new XXHash instance |
| 45 | + */ |
| 46 | + static public XXHash newXXHash64() { |
| 47 | + long nativeHandle = xxHash64Create(0); |
| 48 | + return new XXHash(nativeHandle); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Create new streaming XXHash3_64. |
| 53 | + * @param seed seed to use for the hash |
| 54 | + * @return new XXHash instance |
| 55 | + */ |
| 56 | + static public XXHash newXXHash3_64(long seed) { |
| 57 | + long nativeHandle = xxHash364Create(seed); |
| 58 | + return new XXHash(nativeHandle); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Create new streaming XXHash3_64. |
| 63 | + * @return new XXHash instance |
| 64 | + */ |
| 65 | + static public XXHash newXXHash3_64() { |
| 66 | + long nativeHandle = xxHash364Create(0); |
| 67 | + if (nativeHandle != 0) { |
| 68 | + return new XXHash(nativeHandle); |
| 69 | + } |
| 70 | + |
| 71 | + return null; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Create new streaming XXHash3_128. |
| 76 | + * @param seed seed to use for the hash |
| 77 | + * @return new XXHash instance |
| 78 | + */ |
| 79 | + static public XXHash newXXHash3_128(long seed) { |
| 80 | + long nativeHandle = xxHash3128Create(seed); |
| 81 | + if (nativeHandle != 0) { |
| 82 | + return new XXHash(nativeHandle); |
| 83 | + } |
| 84 | + |
| 85 | + return null; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Create new streaming XXHash3_128. |
| 90 | + * @return new XXHash instance |
| 91 | + */ |
| 92 | + static public XXHash newXXHash3_128() { |
| 93 | + long nativeHandle = xxHash3128Create(0); |
| 94 | + if (nativeHandle != 0) { |
| 95 | + return new XXHash(nativeHandle); |
| 96 | + } |
| 97 | + |
| 98 | + return null; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Update xxhash state from input |
| 103 | + * @param input input to update with |
| 104 | + */ |
| 105 | + public void update(byte[] input) { |
| 106 | + xxHashUpdate(getNativeHandle(), input); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Return digest for the current state of hash. |
| 111 | + * @return hash as bytes in big endian |
| 112 | + */ |
| 113 | + public byte[] digest() { |
| 114 | + return xxHashFinalize(getNativeHandle()); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Oneshot compute XXHash64. |
| 119 | + * @param input input input to hash |
| 120 | + * @param seed seed |
| 121 | + * @return xxhash64 hash |
| 122 | + */ |
| 123 | + static public byte[] computeXXHash64(byte[] input, long seed) { |
| 124 | + return xxHash64Compute(input, seed); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Oneshot compute XXHash64. |
| 129 | + * @param input input input to hash |
| 130 | + * @return xxhash64 hash |
| 131 | + */ |
| 132 | + static public byte[] computeXXHash64(byte[] input) { |
| 133 | + return xxHash64Compute(input, 0); |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | + /** |
| 138 | + * Oneshot compute XXHash3_64. |
| 139 | + * @param input input input to hash |
| 140 | + * @param seed seed |
| 141 | + * @return xxhash64 hash |
| 142 | + */ |
| 143 | + static public byte[] computeXXHash3_64(byte[] input, long seed) { |
| 144 | + return xxHash364Compute(input, seed); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Oneshot compute XXHash3_64. |
| 149 | + * @param input input input to hash |
| 150 | + * @return xxhash64 hash |
| 151 | + */ |
| 152 | + static public byte[] computeXXHash3_64(byte[] input) { |
| 153 | + return xxHash364Compute(input, 0); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Oneshot compute XXHash3_128. |
| 158 | + * @param input input input to hash |
| 159 | + * @param seed seed |
| 160 | + * @return xxhash64 hash |
| 161 | + */ |
| 162 | + static public byte[] computeXXHash3_128(byte[] input, long seed) { |
| 163 | + return xxHash3128Compute(input, seed); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Oneshot compute XXHash3_128. |
| 168 | + * @param input input input to hash |
| 169 | + * @return xxhash64 hash |
| 170 | + */ |
| 171 | + static public byte[] computeXXHash3_128(byte[] input) { |
| 172 | + return xxHash3128Compute(input, 0); |
| 173 | + } |
| 174 | + |
| 175 | + /******************************************************************************* |
| 176 | + * native methods |
| 177 | + ******************************************************************************/ |
| 178 | + private static native byte[] xxHash64Compute(byte[] input, long seed); |
| 179 | + private static native byte[] xxHash364Compute(byte[] input, long seed); |
| 180 | + private static native byte[] xxHash3128Compute(byte[] input, long seed); |
| 181 | + |
| 182 | + private static native long xxHash64Create(long seed); |
| 183 | + private static native long xxHash364Create(long seed); |
| 184 | + private static native long xxHash3128Create(long seed); |
| 185 | + private static native void xxHashRelease(long xxhash); |
| 186 | + |
| 187 | + private static native void xxHashUpdate(long xxhash, byte[] input); |
| 188 | + private static native byte[] xxHashFinalize(long xxhash); |
| 189 | +} |
0 commit comments