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