From dab05adf6c91a616b19125fb88b7a19f93d811b6 Mon Sep 17 00:00:00 2001 From: Francesco Nigro Date: Thu, 21 Dec 2017 18:16:49 +0100 Subject: [PATCH] ARTEMIS-1572 UTF-8 Encoding is not translating ASCII edge cases correctly It fixed 0-127 ASCII chars translation using UTF encoding --- .../activemq/artemis/utils/UTF8Util.java | 6 ++--- .../artemis/tests/unit/util/UTF8Test.java | 25 +++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) 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 84e15572fd..bd00bb1cdf 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; 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 570c791eb0..8c56b0a88c 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; @@ -34,6 +36,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);