gpt4 book ai didi

java - 使用 byte[] 数组通过 ByteBuf 读写字符串

转载 作者:行者123 更新时间:2023-11-30 02:25:18 27 4
gpt4 key购买 nike

我正在尝试使用 ByteBuf 通过 netty 发送字符串。首先,我将字符串转换为字节数组,如下所示:

byteBuf.writeInt(this.serverName.length());
byteBuf.writeInt(this.ipAdress.length());

byteBuf.writeBytes(this.serverName.getBytes(StandardCharsets.UTF_8));
byteBuf.writeBytes(this.ipAdress.getBytes(StandardCharsets.UTF_8));

这很好用,但我不知道如何读取字节以将它们转换回字符串?

我尝试过类似的事情:

int snLen = byteBuf.readInt();
int ipLen = byteBuf.readInt();

byte[] bytes = new byte[byteBuf.readableBytes()];

System.out.println(byteBuf.readBytes(bytes).readByte());
this.ipAdress = "";

一定有什么东西可以取回字节。您可以从字符串发送字节,但最后无法取回字节?似乎有一个方法,但我不知道如何做到这一点。

我希望你们中的任何人都可以帮助我。提前致谢! :)

最佳答案

在 netty 4.1 中你可以使用:

byteBuf.writeCharSequence(...)
byteBuf.readCharSequence(...)

关于java - 使用 byte[] 数组通过 ByteBuf 读写字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45762752/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com