This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch release
in repository https://gitbox.apache.org/repos/asf/commons-codec.git
The following commit(s) were added to refs/heads/release by this push:
new 3397682 [CODEC-276] Reliance on default encoding in MurmurHash2 and
MurmurHash3.
3397682 is described below
commit 3397682996c8dc618469b4ca910dd130540451e3
Author: Gary Gregory <[email protected]>
AuthorDate: Mon Dec 30 09:13:18 2019 -0500
[CODEC-276] Reliance on default encoding in MurmurHash2 and MurmurHash3.
Reuse our StringUtils.getBytesUtf8(text).
---
.../org/apache/commons/codec/digest/MurmurHash2.java | 4 ++--
.../org/apache/commons/codec/digest/MurmurHash3.java | 17 ++++++-----------
.../apache/commons/codec/digest/MurmurHash3Test.java | 9 +++++----
3 files changed, 13 insertions(+), 17 deletions(-)
diff --git a/src/main/java/org/apache/commons/codec/digest/MurmurHash2.java
b/src/main/java/org/apache/commons/codec/digest/MurmurHash2.java
index 98698ad..4708ec6 100644
--- a/src/main/java/org/apache/commons/codec/digest/MurmurHash2.java
+++ b/src/main/java/org/apache/commons/codec/digest/MurmurHash2.java
@@ -130,7 +130,7 @@ public final class MurmurHash2 {
/**
* Generates a 32-bit hash from a string with a default seed.
* <p>
- * Before 1.14 the string was converted using default encoding.
+ * Before 1.14 the string was converted using default encoding.
* Since 1.14 the string is converted to bytes using UTF-8 encoding.
* </p>
* This is a helper method that will produce the same result as:
@@ -244,7 +244,7 @@ public final class MurmurHash2 {
/**
* Generates a 64-bit hash from a string with a default seed.
* <p>
- * Before 1.14 the string was converted using default encoding.
+ * Before 1.14 the string was converted using default encoding.
* Since 1.14 the string is converted to bytes using UTF-8 encoding.
* </p>
* This is a helper method that will produce the same result as:
diff --git a/src/main/java/org/apache/commons/codec/digest/MurmurHash3.java
b/src/main/java/org/apache/commons/codec/digest/MurmurHash3.java
index 059f9af..fd37bb5 100644
--- a/src/main/java/org/apache/commons/codec/digest/MurmurHash3.java
+++ b/src/main/java/org/apache/commons/codec/digest/MurmurHash3.java
@@ -20,6 +20,8 @@ package org.apache.commons.codec.digest;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
+import org.apache.commons.codec.binary.StringUtils;
+
/**
* Implementation of the MurmurHash3 32-bit and 128-bit hash functions.
*
@@ -59,13 +61,6 @@ import java.nio.charset.StandardCharsets;
public final class MurmurHash3 {
/**
- * Default Charset used to convert strings into bytes.
- *
- * Consider private; package private for tests only.
- */
- static final Charset GET_BYTES_CHARSET = StandardCharsets.UTF_8;
-
- /**
* A random number to use for a hash code.
*
* @deprecated This is not used internally and will be removed in a future
release.
@@ -238,7 +233,7 @@ public final class MurmurHash3 {
/**
* Generates 32-bit hash from a string with a default seed.
* <p>
- * Before 1.14 the string was converted using default encoding.
+ * Before 1.14 the string was converted using default encoding.
* Since 1.14 the string is converted to bytes using UTF-8 encoding.
* </p>
* This is a helper method that will produce the same result as:
@@ -262,7 +257,7 @@ public final class MurmurHash3 {
*/
@Deprecated
public static int hash32(final String data) {
- final byte[] bytes = data.getBytes(GET_BYTES_CHARSET);
+ final byte[] bytes = StringUtils.getBytesUtf8(data);
return hash32(bytes, 0, bytes.length, DEFAULT_SEED);
}
@@ -759,7 +754,7 @@ public final class MurmurHash3 {
/**
* Generates 128-bit hash from a string with a default seed.
* <p>
- * Before 1.14 the string was converted using default encoding.
+ * Before 1.14 the string was converted using default encoding.
* Since 1.14 the string is converted to bytes using UTF-8 encoding.
* </p>
* This is a helper method that will produce the same result as:
@@ -782,7 +777,7 @@ public final class MurmurHash3 {
*/
@Deprecated
public static long[] hash128(final String data) {
- final byte[] bytes = data.getBytes(GET_BYTES_CHARSET);
+ final byte[] bytes = StringUtils.getBytesUtf8(data);
return hash128(bytes, 0, bytes.length, DEFAULT_SEED);
}
diff --git a/src/test/java/org/apache/commons/codec/digest/MurmurHash3Test.java
b/src/test/java/org/apache/commons/codec/digest/MurmurHash3Test.java
index 61df7f2..21515b2 100644
--- a/src/test/java/org/apache/commons/codec/digest/MurmurHash3Test.java
+++ b/src/test/java/org/apache/commons/codec/digest/MurmurHash3Test.java
@@ -23,6 +23,7 @@ import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.concurrent.ThreadLocalRandom;
+import org.apache.commons.codec.binary.StringUtils;
import org.apache.commons.codec.digest.MurmurHash3.IncrementalHash32;
import org.apache.commons.codec.digest.MurmurHash3.IncrementalHash32x86;
import org.junit.Test;
@@ -355,7 +356,7 @@ public class MurmurHash3Test {
pos += Character.toChars(codePoint, chars, pos);
}
final String text = String.copyValueOf(chars, 0, pos);
- final byte[] bytes = text.getBytes(MurmurHash3.GET_BYTES_CHARSET);
+ final byte[] bytes = StringUtils.getBytesUtf8(text);
final int h1 = MurmurHash3.hash32(bytes, 0, bytes.length, seed);
final int h2 = MurmurHash3.hash32(text);
Assert.assertEquals(h1, h2);
@@ -455,7 +456,7 @@ public class MurmurHash3Test {
*/
@Test
public void testHash64() {
- final byte[] origin =
TEST_HASH64.getBytes(MurmurHash3.GET_BYTES_CHARSET);
+ final byte[] origin = StringUtils.getBytesUtf8(TEST_HASH64);
final long hash = MurmurHash3.hash64(origin);
Assert.assertEquals(5785358552565094607L, hash);
}
@@ -466,7 +467,7 @@ public class MurmurHash3Test {
*/
@Test
public void testHash64WithOffsetAndLength() {
- final byte[] origin =
TEST_HASH64.getBytes(MurmurHash3.GET_BYTES_CHARSET);
+ final byte[] origin = StringUtils.getBytesUtf8(TEST_HASH64);
final byte[] originOffset = new byte[origin.length + 150];
Arrays.fill(originOffset, (byte) 123);
System.arraycopy(origin, 0, originOffset, 150, origin.length);
@@ -627,7 +628,7 @@ public class MurmurHash3Test {
pos += Character.toChars(codePoint, chars, pos);
}
final String text = String.copyValueOf(chars, 0, pos);
- final byte[] bytes = text.getBytes(MurmurHash3.GET_BYTES_CHARSET);
+ final byte[] bytes = StringUtils.getBytesUtf8(text);
final long[] h1 = MurmurHash3.hash128(bytes, 0, bytes.length,
seed);
final long[] h2 = MurmurHash3.hash128(text);
Assert.assertArrayEquals(h1, h2);