diff --git a/protocol/src/main/java/mc/protocol/io/NetByteBuf.java b/protocol/src/main/java/mc/protocol/io/NetByteBuf.java index d1c3638..da59f07 100644 --- a/protocol/src/main/java/mc/protocol/io/NetByteBuf.java +++ b/protocol/src/main/java/mc/protocol/io/NetByteBuf.java @@ -93,19 +93,16 @@ public class NetByteBuf extends ByteBuf { } public void writeString(String string) { - byte[] buf; - int length = (int) string.codePoints().count(); + byte[] buf = string.getBytes(StandardCharsets.UTF_8); - if (length > Short.MAX_VALUE) { - log.warn("String is too long: {} > {}", length, Short.MAX_VALUE); - buf = string.substring(0, Short.MAX_VALUE).getBytes(StandardCharsets.UTF_8); + if (buf.length > Short.MAX_VALUE) { + log.warn("String is too long: {} > {}", buf.length, Short.MAX_VALUE); writeVarInt(Short.MAX_VALUE); + writeBytes(buf, 0, Short.MAX_VALUE); } else { - buf = string.getBytes(StandardCharsets.UTF_8); - writeVarInt(length); + writeVarInt(buf.length); + writeBytes(buf); } - - writeBytes(buf); } //endregion diff --git a/protocol/src/test/java/mc/protocol/io/NetByteBufWriteTest.java b/protocol/src/test/java/mc/protocol/io/NetByteBufWriteTest.java index 74b6c5a..3386041 100644 --- a/protocol/src/test/java/mc/protocol/io/NetByteBufWriteTest.java +++ b/protocol/src/test/java/mc/protocol/io/NetByteBufWriteTest.java @@ -50,15 +50,15 @@ class NetByteBufWriteTest { @ParameterizedTest @MethodSource("paramsWriteString") - void writeString(String string) { + void writeString(String string, int exceptedLength) { ByteBuf byteBuf = Unpooled.buffer(); NetByteBuf netByteBuf = new NetByteBuf(byteBuf); netByteBuf.writeString(string); byte[] actualArray = netByteBuf.copy(0, netByteBuf.readableBytes()).array(); - int length = actualArray[0]; // допустим, что размер поместился в один байт - assertEquals(string.codePoints().count(), length); + int actualLength = actualArray[0]; // допустим, что размер поместился в один байт + assertEquals(exceptedLength, actualLength); byte[] dataBytes = new byte[actualArray.length - 1]; System.arraycopy(actualArray, 1, dataBytes, 0, dataBytes.length); @@ -196,12 +196,16 @@ class NetByteBufWriteTest { @SuppressWarnings("unused") private static Stream paramsWriteString() { return Stream.of( - Arguments.of(""), - Arguments.of("Latin"), - Arguments.of("Кириллица"), - Arguments.of("العربية"), - Arguments.of("ﬦﬣﬡ"), // Алфавитные формы представления - Arguments.of("\uD800\uDD07") // Эгейские цифры, [один] + Arguments.of("", 0), + Arguments.of("Latin", 5), + Arguments.of("Кириллица", 37), + // (9) -> "Кириллица"(18) => 18*2=36 (37?) + Arguments.of("العربية", 30), + // (7) -> "Ш§Щ„Ш№Ш±ШЁЩЉШ©"(14) => 14*2=28 (30?) + Arguments.of("ﬦﬣﬡ", 18), // Алфавитные формы представления + // (3) -> "ﬦﬣﬡ"(9) => 9*2=18 + Arguments.of("\uD800\uDD07", 4) // Эгейские цифры, [один] + // (1) -> "𐄇" => ...4! ); }