如何在字节缓冲区中表示目标列表
一个简单的问题:
我有一个目标列表,例如 A、B、C、D、E...,我需要将此目标列表放入字节缓冲区并使用 java 套接字发送到客户端。在客户端,它将解析字节缓冲区并获取目标列表。
我找到两种方法来做到这一点:首先,使用 dest_list_size + dest_list 的格式。那么上面的例子就变成:5+A;B;C;D;E;其次,使用哈希集来保存目的地列表。那么如何将哈希集转换为字节数组并转换为哈希集呢?
我想验证一下哪条路更好?谢谢。
A simple question:
I have a destination list, for example, A, B, C, D, E, ... and I need to put this destiantion list to a bytebuffer and send to a client using a java socket. At the client side, it will parse the bytebuffer and get the destination list.
I find two ways to do this: firstly, using format of dest_list_size + dest_list. Then the above example will becomes: 5+A;B;C;D;E; secondly, using a hashset to hold the destination list. Then how to transform hashset to a byte arrays and translates to a hashset?
I want to verify which way is better to go? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最好的方法是将其序列化为更标准的内容,例如 json。您可以使用现有的库,即 jackson。
The best way would be to serialize it to something more standard like json. You can use existing libraries i.e. jackson.
取决于您的目标字段的类型。
打印
如果您有不同类型的目标或可能超过 127 个目标,您需要根据需要更改写入该字段/值的方式。
Depending on the type of you destination field.
prints
If you have a different type of destination or more than 127 destinations possible you need to change how you write that field/value as required.