镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/java/net/jpountz/xxhash/StreamingXXHash32JNI.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ private void checkState() {
@Override
public synchronized void reset() {
checkState();
// create the new state first so that a failed init leaves this instance usable
final long newState = XXHashJNI.XXH32_init(seed);
XXHashJNI.XXH32_free(state);
state = XXHashJNI.XXH32_init(seed);
state = newState;
}

@Override
Expand Down
4 changes: 3 additions & 1 deletion src/java/net/jpountz/xxhash/StreamingXXHash64JNI.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ private void checkState() {
@Override
public synchronized void reset() {
checkState();
// create the new state first so that a failed init leaves this instance usable
final long newState = XXHashJNI.XXH64_init(seed);
XXHashJNI.XXH64_free(state);
state = XXHashJNI.XXH64_init(seed);
state = newState;
}

@Override
Expand Down
44 changes: 39 additions & 5 deletions src/jni/net_jpountz_xxhash_XXHashJNI.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,28 @@

static jclass OutOfMemoryError;

/* Stand-in input for empty direct buffers whose address is NULL. */
static const char empty_input[1] = { 0 };

static int init_class_ref(JNIEnv *env, jclass *target, const char *className) {
jclass localClass = (*env)->FindClass(env, className);
if (localClass == NULL) {
return 0;
}

*target = (jclass) (*env)->NewGlobalRef(env, localClass);
(*env)->DeleteLocalRef(env, localClass);
return *target != NULL;
}

/*
* Class: net_jpountz_xxhash_XXHashJNI
* Method: init
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_net_jpountz_xxhash_XXHashJNI_init
(JNIEnv *env, jclass cls) {
OutOfMemoryError = (*env)->FindClass(env, "java/lang/OutOfMemoryError");
init_class_ref(env, &OutOfMemoryError, "java/lang/OutOfMemoryError");
}

static void throw_OOM(JNIEnv *env) {
Expand Down Expand Up @@ -71,8 +85,13 @@ JNIEXPORT jint JNICALL Java_net_jpountz_xxhash_XXHashJNI_XXH32BB

in = (char*) (*env)->GetDirectBufferAddress(env, buf);
if (in == NULL) {
throw_OOM(env);
return 0;
if (len != 0) {
throw_OOM(env);
return 0;
}
/* e.g. a zero-length mapped buffer: hash the empty input */
in = (char*) empty_input;
off = 0;
}

h32 = XXH32(in + off, len, seed);
Expand All @@ -90,6 +109,11 @@ JNIEXPORT jlong JNICALL Java_net_jpountz_xxhash_XXHashJNI_XXH32_1init
(JNIEnv *env, jclass cls, jint seed) {

XXH32_state_t *state = XXH32_createState();
if (state == NULL) {
throw_OOM(env);
return 0;
}

if (XXH32_reset(state, seed) != XXH_OK) {
XXH32_freeState(state);
throw_OOM(env);
Expand Down Expand Up @@ -181,8 +205,13 @@ JNIEXPORT jlong JNICALL Java_net_jpountz_xxhash_XXHashJNI_XXH64BB

in = (char*) (*env)->GetDirectBufferAddress(env, buf);
if (in == NULL) {
throw_OOM(env);
return 0;
if (len != 0) {
throw_OOM(env);
return 0;
}
/* e.g. a zero-length mapped buffer: hash the empty input */
in = (char*) empty_input;
off = 0;
}

h64 = XXH64(in + off, len, seed);
Expand All @@ -200,6 +229,11 @@ JNIEXPORT jlong JNICALL Java_net_jpountz_xxhash_XXHashJNI_XXH64_1init
(JNIEnv *env, jclass cls, jlong seed) {

XXH64_state_t *state = XXH64_createState();
if (state == NULL) {
throw_OOM(env);
return 0;
}

if (XXH64_reset(state, seed) != XXH_OK) {
XXH64_freeState(state);
throw_OOM(env);
Expand Down
21 changes: 21 additions & 0 deletions src/test/net/jpountz/xxhash/XXHash32Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
* limitations under the License.
*/

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

import net.jpountz.lz4.AbstractLZ4Test;
import net.jpountz.util.SafeUtils;
Expand Down Expand Up @@ -104,6 +109,22 @@ public void testEmpty() {
}
}

@Test
public void testEmptyMappedBuffer() throws IOException {
// a zero-length mapping is a direct buffer with a NULL address
final Path file = Files.createTempFile("xxhash", ".dat");
try (FileChannel channel = FileChannel.open(file, StandardOpenOption.READ, StandardOpenOption.WRITE)) {
final ByteBuffer buf = channel.map(FileChannel.MapMode.READ_WRITE, 0, 0);
final int seed = randomInt();
final int expected = XXHashFactory.safeInstance().hash32().hash(buf, 0, 0, seed);
for (int i = 0; i < 2; ++i) {
assertEquals(expected, XXHashFactory.nativeInstance().hash32().hash(buf, 0, 0, seed));
}
} finally {
Files.delete(file);
}
}

@Test
public void testSafeUtilsRejectsNullEmptyRange() {
assertThrows(NullPointerException.class, () -> SafeUtils.checkRange(null, 0, 0));
Expand Down
21 changes: 21 additions & 0 deletions src/test/net/jpountz/xxhash/XXHash64Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
* limitations under the License.
*/

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

import net.jpountz.lz4.AbstractLZ4Test;
import net.jpountz.util.SafeUtils;
Expand Down Expand Up @@ -104,6 +109,22 @@ public void testEmpty() {
}
}

@Test
public void testEmptyMappedBuffer() throws IOException {
// a zero-length mapping is a direct buffer with a NULL address
final Path file = Files.createTempFile("xxhash", ".dat");
try (FileChannel channel = FileChannel.open(file, StandardOpenOption.READ, StandardOpenOption.WRITE)) {
final ByteBuffer buf = channel.map(FileChannel.MapMode.READ_WRITE, 0, 0);
final long seed = randomLong();
final long expected = XXHashFactory.safeInstance().hash64().hash(buf, 0, 0, seed);
for (int i = 0; i < 2; ++i) {
assertEquals(expected, XXHashFactory.nativeInstance().hash64().hash(buf, 0, 0, seed));
}
} finally {
Files.delete(file);
}
}

@Test
public void testSafeUtilsRejectsNullEmptyRange() {
assertThrows(NullPointerException.class, () -> SafeUtils.checkRange(null, 0, 0));
Expand Down
Loading