azotcsit commented on code in PR #1284:
URL: https://github.com/apache/activemq/pull/1284#discussion_r1730689087


##########
activemq-client/src/main/java/org/apache/activemq/util/DataByteArrayInputStream.java:
##########
@@ -264,8 +264,7 @@ public String readUTF() throws IOException {
         if (pos + length > buf.length) {
             throw new UTFDataFormatException("bad string");
         }
-        char chararr[] = new char[length];
-        String result = MarshallingSupport.convertUTF8WithBuf(buf, chararr, 
pos, length);

Review Comment:
   `chararr` is no longer required for conversion logic, so I removed it.



##########
activemq-client/src/main/java/org/apache/activemq/util/DataByteArrayOutputStream.java:
##########
@@ -210,7 +210,6 @@ public void writeUTF(String text) throws IOException {
         ensureEnoughBuffer((int)(pos + encodedsize + 2));
         writeShort((int)encodedsize);
 
-        byte[] buffer = new byte[(int)encodedsize];

Review Comment:
   it was not in use.



##########
activemq-client/src/main/java/org/apache/activemq/util/MarshallingSupport.java:
##########
@@ -310,86 +311,53 @@ public static void writeUTF8(DataOutput dataOut, String 
text) throws IOException
     }
 
     /**
-     * From: 
http://svn.apache.org/repos/asf/harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/io/DataOutputStream.java
+     * Inspired by: 
http://svn.apache.org/repos/asf/harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/io/DataOutputStream.java
      */
-    public static long countUTFBytes(String str) {
+    public static long countUTFBytes(String str) throws UTFDataFormatException 
{
         int utfCount = 0, length = str.length();
         for (int i = 0; i < length; i++) {
-            int charValue = str.charAt(i);
-            if (charValue > 0 && charValue <= 127) {
+            int codePoint = str.codePointAt(i);
+
+            if (codePoint >= 0 && codePoint <= 127) {
                 utfCount++;
-            } else if (charValue <= 2047) {
+            } else if (codePoint <= 2047) {
                 utfCount += 2;
-            } else {
+            } else if (codePoint <= 65535) {
                 utfCount += 3;
+            } else if (codePoint <= 1114111) {
+                utfCount += 4;
+                i++;
+            } else {
+                throw new UTFDataFormatException();
             }
         }
         return utfCount;
     }
 
-    /**
-     * From: 
http://svn.apache.org/repos/asf/harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/io/DataOutputStream.java
-     */
-    public static int writeUTFBytesToBuffer(String str, long count,
+    public static int writeUTFBytesToBuffer(String str, int count,
                                      byte[] buffer, int offset) throws 
IOException {
-        int length = str.length();
-        for (int i = 0; i < length; i++) {
-            int charValue = str.charAt(i);
-            if (charValue > 0 && charValue <= 127) {
-                buffer[offset++] = (byte) charValue;
-            } else if (charValue <= 2047) {
-                buffer[offset++] = (byte) (0xc0 | (0x1f & (charValue >> 6)));
-                buffer[offset++] = (byte) (0x80 | (0x3f & charValue));
-            } else {
-                buffer[offset++] = (byte) (0xe0 | (0x0f & (charValue >> 12)));
-                buffer[offset++] = (byte) (0x80 | (0x3f & (charValue >> 6)));
-                buffer[offset++] = (byte) (0x80 | (0x3f & charValue));
-            }
+        byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
+        if (bytes.length != count) {
+            throw new UTFDataFormatException();
         }
-        return offset;
+        System.arraycopy(bytes, 0, buffer, offset, count);
+        return offset + count;
     }
 
     public static String readUTF8(DataInput dataIn) throws IOException {
         int utflen = dataIn.readInt();
         if (utflen > -1) {
             byte bytearr[] = new byte[utflen];
-            char chararr[] = new char[utflen];
             dataIn.readFully(bytearr, 0, utflen);
-            return convertUTF8WithBuf(bytearr, chararr, 0, utflen);
+            return convertUTF8WithBuf(bytearr, 0, utflen);
         } else {
             return null;
         }
     }
 
-    /**
-     * From: 
http://svn.apache.org/repos/asf/harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/org/apache/harmony/luni/util/Util.java
-     */
-    public static String convertUTF8WithBuf(byte[] buf, char[] out, int offset,
-                                            int utfSize) throws 
UTFDataFormatException {
-        int count = 0, s = 0, a;
-        while (count < utfSize) {
-            if ((out[s] = (char) buf[offset + count++]) < '\u0080')
-                s++;
-            else if (((a = out[s]) & 0xe0) == 0xc0) {
-                if (count >= utfSize)
-                    throw new UTFDataFormatException();
-                int b = buf[offset + count++];
-                if ((b & 0xC0) != 0x80)
-                    throw new UTFDataFormatException();
-                out[s++] = (char) (((a & 0x1F) << 6) | (b & 0x3F));
-            } else if ((a & 0xf0) == 0xe0) {
-                if (count + 1 >= utfSize)
-                    throw new UTFDataFormatException();
-                int b = buf[offset + count++];
-                int c = buf[offset + count++];
-                if (((b & 0xC0) != 0x80) || ((c & 0xC0) != 0x80))
-                    throw new UTFDataFormatException();
-                out[s++] = (char) (((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c 
& 0x3F));
-            } else {
-                throw new UTFDataFormatException();
-            }
-        }
-        return new String(out, 0, s);
+    public static String convertUTF8WithBuf(byte[] buf, int offset,
+                                            int utfSize) {
+        return new String(buf, offset, utfSize, StandardCharsets.UTF_8);

Review Comment:
   here is the JDK standard logic to convert bytes into a string.



##########
activemq-client/src/main/java/org/apache/activemq/util/MarshallingSupport.java:
##########
@@ -310,86 +311,53 @@ public static void writeUTF8(DataOutput dataOut, String 
text) throws IOException
     }
 
     /**
-     * From: 
http://svn.apache.org/repos/asf/harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/io/DataOutputStream.java
+     * Inspired by: 
http://svn.apache.org/repos/asf/harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/io/DataOutputStream.java
      */
-    public static long countUTFBytes(String str) {
+    public static long countUTFBytes(String str) throws UTFDataFormatException 
{
         int utfCount = 0, length = str.length();
         for (int i = 0; i < length; i++) {
-            int charValue = str.charAt(i);
-            if (charValue > 0 && charValue <= 127) {
+            int codePoint = str.codePointAt(i);
+
+            if (codePoint >= 0 && codePoint <= 127) {

Review Comment:
   somehow 0 code point was missing in the original logic, I fixed it.



##########
activemq-client/src/main/java/org/apache/activemq/util/MarshallingSupport.java:
##########
@@ -310,86 +311,53 @@ public static void writeUTF8(DataOutput dataOut, String 
text) throws IOException
     }
 
     /**
-     * From: 
http://svn.apache.org/repos/asf/harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/io/DataOutputStream.java
+     * Inspired by: 
http://svn.apache.org/repos/asf/harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/io/DataOutputStream.java
      */
-    public static long countUTFBytes(String str) {
+    public static long countUTFBytes(String str) throws UTFDataFormatException 
{
         int utfCount = 0, length = str.length();
         for (int i = 0; i < length; i++) {
-            int charValue = str.charAt(i);
-            if (charValue > 0 && charValue <= 127) {
+            int codePoint = str.codePointAt(i);
+
+            if (codePoint >= 0 && codePoint <= 127) {
                 utfCount++;
-            } else if (charValue <= 2047) {
+            } else if (codePoint <= 2047) {
                 utfCount += 2;
-            } else {
+            } else if (codePoint <= 65535) {
                 utfCount += 3;
+            } else if (codePoint <= 1114111) {
+                utfCount += 4;
+                i++;
+            } else {
+                throw new UTFDataFormatException();
             }
         }
         return utfCount;
     }
 
-    /**
-     * From: 
http://svn.apache.org/repos/asf/harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/io/DataOutputStream.java
-     */
-    public static int writeUTFBytesToBuffer(String str, long count,
+    public static int writeUTFBytesToBuffer(String str, int count,
                                      byte[] buffer, int offset) throws 
IOException {
-        int length = str.length();
-        for (int i = 0; i < length; i++) {
-            int charValue = str.charAt(i);
-            if (charValue > 0 && charValue <= 127) {
-                buffer[offset++] = (byte) charValue;
-            } else if (charValue <= 2047) {
-                buffer[offset++] = (byte) (0xc0 | (0x1f & (charValue >> 6)));
-                buffer[offset++] = (byte) (0x80 | (0x3f & charValue));
-            } else {
-                buffer[offset++] = (byte) (0xe0 | (0x0f & (charValue >> 12)));
-                buffer[offset++] = (byte) (0x80 | (0x3f & (charValue >> 6)));
-                buffer[offset++] = (byte) (0x80 | (0x3f & charValue));
-            }
+        byte[] bytes = str.getBytes(StandardCharsets.UTF_8);

Review Comment:
   here is the JDK standard logic to get bytes from a string.



##########
activemq-client/src/main/java/org/apache/activemq/util/MarshallingSupport.java:
##########
@@ -310,86 +311,53 @@ public static void writeUTF8(DataOutput dataOut, String 
text) throws IOException
     }
 
     /**
-     * From: 
http://svn.apache.org/repos/asf/harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/io/DataOutputStream.java
+     * Inspired by: 
http://svn.apache.org/repos/asf/harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/io/DataOutputStream.java
      */
-    public static long countUTFBytes(String str) {
+    public static long countUTFBytes(String str) throws UTFDataFormatException 
{
         int utfCount = 0, length = str.length();
         for (int i = 0; i < length; i++) {
-            int charValue = str.charAt(i);
-            if (charValue > 0 && charValue <= 127) {
+            int codePoint = str.codePointAt(i);
+
+            if (codePoint >= 0 && codePoint <= 127) {
                 utfCount++;
-            } else if (charValue <= 2047) {
+            } else if (codePoint <= 2047) {
                 utfCount += 2;
-            } else {
+            } else if (codePoint <= 65535) {
                 utfCount += 3;
+            } else if (codePoint <= 1114111) {
+                utfCount += 4;
+                i++;
+            } else {
+                throw new UTFDataFormatException();
             }
         }
         return utfCount;
     }
 
-    /**
-     * From: 
http://svn.apache.org/repos/asf/harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/io/DataOutputStream.java
-     */
-    public static int writeUTFBytesToBuffer(String str, long count,
+    public static int writeUTFBytesToBuffer(String str, int count,
                                      byte[] buffer, int offset) throws 
IOException {
-        int length = str.length();
-        for (int i = 0; i < length; i++) {
-            int charValue = str.charAt(i);
-            if (charValue > 0 && charValue <= 127) {
-                buffer[offset++] = (byte) charValue;
-            } else if (charValue <= 2047) {
-                buffer[offset++] = (byte) (0xc0 | (0x1f & (charValue >> 6)));
-                buffer[offset++] = (byte) (0x80 | (0x3f & charValue));
-            } else {
-                buffer[offset++] = (byte) (0xe0 | (0x0f & (charValue >> 12)));
-                buffer[offset++] = (byte) (0x80 | (0x3f & (charValue >> 6)));
-                buffer[offset++] = (byte) (0x80 | (0x3f & charValue));
-            }
+        byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
+        if (bytes.length != count) {

Review Comment:
   `bytes.length` should be always equal to `count`. instead of removing 
`count` method parameter, I decided to add this check.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: gitbox-unsubscr...@activemq.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscr...@activemq.apache.org
For additional commands, e-mail: gitbox-h...@activemq.apache.org
For further information, visit: https://activemq.apache.org/contact


Reply via email to