Archived
0

fix: отправка строки в NetByteBuf

This commit is contained in:
2021-04-29 21:14:10 +03:00
parent aa0e9bce3b
commit 27c739ca09
2 changed files with 19 additions and 18 deletions

View File

@@ -93,20 +93,17 @@ 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);
}
}
//endregion
//region VarInt

View File

@@ -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<Arguments> 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!
);
}