Kalvin2077 commented on code in PR #3769:
URL: https://github.com/apache/celeborn/pull/3769#discussion_r3671588908
##########
common/src/main/java/org/apache/celeborn/common/network/protocol/Encoders.java:
##########
@@ -20,20 +20,20 @@
import java.nio.charset.StandardCharsets;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufUtil;
/** Provides a canonical set of Encoders for simple types. */
public class Encoders {
/** Strings are encoded with their length followed by UTF-8 bytes. */
public static class Strings {
public static int encodedLength(String s) {
- return 4 + s.getBytes(StandardCharsets.UTF_8).length;
+ return 4 + ByteBufUtil.utf8Bytes(s);
}
public static void encode(ByteBuf buf, String s) {
- byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
- buf.writeInt(bytes.length);
- buf.writeBytes(bytes);
+ buf.writeInt(ByteBufUtil.utf8Bytes(s));
+ ByteBufUtil.writeUtf8(buf, s);
Review Comment:
`writeUtf8` reserves `utf8MaxBytes(s.length())`—up to 3 bytes per UTF-16
code unit. Therefore, a buffer initially sized from `encodedLength()`may still
grow.
##########
common/src/test/java/org/apache/celeborn/common/network/protocol/EncodersSuiteJ.java:
##########
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.celeborn.common.network.protocol;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+import java.nio.charset.StandardCharsets;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import org.junit.Test;
+
+public class EncodersSuiteJ {
+
+ private static final String[] TEST_STRINGS = {
+ "",
+ "ascii",
+ "application_1700000000000_0001-0",
+ "中文 shuffle key",
+ "emoji 🚀🔥",
+ "mixed ASCII 中文 🚀",
+ // NUL and control characters
+ "a\0b\1c",
+ // 2-byte / 3-byte UTF-8 boundaries
+ "߿ࠀ",
+ // BMP max and replacement char
+ "��"
+ };
+
+ // Malformed inputs: UTF-8 encoding is lossy for unpaired surrogates
(replaced with '?'),
+ // so these do not round-trip, but the encoded bytes must still match
getBytes(UTF_8).
+ private static final String[] LOSSY_STRINGS = {"abc\uD800", "܀abc",
"a\uD800\uD800b"};
Review Comment:
Try `\uD800中`.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]