You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by jb...@apache.org on 2018/01/04 19:57:27 UTC
[2/2] activemq-artemis git commit: ARTEMIS-1572 UTF-8 Encoding is not
translating ASCII edge cases correctly
ARTEMIS-1572 UTF-8 Encoding is not translating ASCII edge cases correctly
It fixed 0-127 ASCII chars translation using UTF encoding
Project: http://git-wip-us.apache.org/repos/asf/activemq-artemis/repo
Commit: http://git-wip-us.apache.org/repos/asf/activemq-artemis/commit/dab05adf
Tree: http://git-wip-us.apache.org/repos/asf/activemq-artemis/tree/dab05adf
Diff: http://git-wip-us.apache.org/repos/asf/activemq-artemis/diff/dab05adf
Branch: refs/heads/master
Commit: dab05adf6c91a616b19125fb88b7a19f93d811b6
Parents: fee083a
Author: Francesco Nigro <ni...@gmail.com>
Authored: Thu Dec 21 18:16:49 2017 +0100
Committer: Justin Bertram <jb...@apache.org>
Committed: Thu Jan 4 13:57:08 2018 -0600
----------------------------------------------------------------------
.../apache/activemq/artemis/utils/UTF8Util.java | 6 ++---
.../artemis/tests/unit/util/UTF8Test.java | 25 ++++++++++++++++++++
2 files changed, 28 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/dab05adf/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/UTF8Util.java
----------------------------------------------------------------------
diff --git a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/UTF8Util.java b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/UTF8Util.java
index 84e1557..bd00bb1 100644
--- a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/UTF8Util.java
+++ b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/UTF8Util.java
@@ -103,7 +103,7 @@ public final class UTF8Util {
for (int i = 0; i < stringLength; i++) {
char charAtPos = buffer.charBuffer[i];
- if (charAtPos >= 1 && charAtPos < 0x7f) {
+ if (charAtPos <= 0x7f) {
buffer.byteBuffer[charCount++] = (byte) charAtPos;
} else if (charAtPos >= 0x800) {
buffer.byteBuffer[charCount++] = (byte) (0xE0 | charAtPos >> 12 & 0x0F);
@@ -145,7 +145,7 @@ public final class UTF8Util {
while (count < size) {
byte1 = buffer.byteBuffer[count++];
- if (byte1 > 0 && byte1 <= 0x7F) {
+ if (byte1 >= 0 && byte1 <= 0x7F) {
buffer.charBuffer[charCount++] = (char) byte1;
} else {
int c = byte1 & 0xff;
@@ -211,7 +211,7 @@ public final class UTF8Util {
for (int i = 0; i < stringLength; i++) {
char c = stringBuffer.charBuffer[i];
- if (c >= 1 && c < 0x7f) {
+ if (c <= 0x7f) {
calculatedLen++;
} else if (c >= 0x800) {
calculatedLen += 3;
http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/dab05adf/tests/unit-tests/src/test/java/org/apache/activemq/artemis/tests/unit/util/UTF8Test.java
----------------------------------------------------------------------
diff --git a/tests/unit-tests/src/test/java/org/apache/activemq/artemis/tests/unit/util/UTF8Test.java b/tests/unit-tests/src/test/java/org/apache/activemq/artemis/tests/unit/util/UTF8Test.java
index 570c791..8c56b0a 100644
--- a/tests/unit-tests/src/test/java/org/apache/activemq/artemis/tests/unit/util/UTF8Test.java
+++ b/tests/unit-tests/src/test/java/org/apache/activemq/artemis/tests/unit/util/UTF8Test.java
@@ -21,6 +21,8 @@ import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
import org.apache.activemq.artemis.api.core.ActiveMQBuffer;
import org.apache.activemq.artemis.api.core.ActiveMQBuffers;
@@ -35,6 +37,29 @@ import org.junit.Test;
public class UTF8Test extends ActiveMQTestBase {
@Test
+ public void testValidateUTFWithENDChars() {
+ testValidateUTFWithChars(1024, (char) 0);
+ }
+
+ @Test
+ public void testValidateUTFWithLastAsciiChars() {
+ testValidateUTFWithChars(1024, (char) Byte.MAX_VALUE);
+ }
+
+ private void testValidateUTFWithChars(final int size, final char c) {
+ final char[] chars = new char[size];
+ Arrays.fill(chars, c);
+ final String expectedUtf8String = new String(chars);
+ final ActiveMQBuffer buffer = ActiveMQBuffers.fixedBuffer(4 * chars.length);
+ UTF8Util.saveUTF(buffer.byteBuf(), expectedUtf8String);
+ final byte[] expectedBytes = expectedUtf8String.getBytes(StandardCharsets.UTF_8);
+ final int encodedSize = buffer.readUnsignedShort();
+ final byte[] realEncodedBytes = new byte[encodedSize];
+ buffer.getBytes(buffer.readerIndex(), realEncodedBytes);
+ Assert.assertArrayEquals(expectedBytes, realEncodedBytes);
+ }
+
+ @Test
public void testValidateUTF() throws Exception {
ActiveMQBuffer buffer = ActiveMQBuffers.fixedBuffer(60 * 1024);