gpt4 book ai didi

Java:从字节数组中删除第一个UTF字符串

转载 作者:行者123 更新时间:2023-12-01 13:05:29 33 4
gpt4 key购买 nike

我试图从字节数组中删除写入的字符串并维护原始的单独对象:

byte[] data... // this is populated with the following:
// 00094E6966747943686174001C00074D657373616765000B4372616674656446757279000474657374
// to string using converter : " ChannelMessageUsernametest"
// notice that tab/whitespace, ignore quotes
// The byte array was compiled by writing the following (writeUTF from a writer):
// Channel
// Message
// Username
// test

现在我试图从字节数组中删除Channel:

ByteArrayDataInput input = ByteStreams.newDataInput(message);
String channel = input.readUTF(); // Channel, don't want this
String message = input.readUTF(); // Message
// works good, but I don't want Channel,
// and I can't remove it from the data before it arrives,
// I have to work with what I have

这是我的问题:

byte[] newData = Arrays.copyOfRange(data, channel.length() + 2, data.length)
// I use Arrays.copyOfRange to strip the whitespace (I assume it's not needed)
// As well, since it's inclusive of length size, I have to add 1 more,
// resulting in channel.length() + 1
// ...
ByteArrayDataInput newInput = ByteStreams.newDataInput(message);
String channel = newInput.readUTF(); // MessageUsernametext

看看我如何失去对象的分离,如何将对象的原始“部分”保留在 byte[] newData 内的原始 byte[] data 中.

  • 可以安全地假设字符串 channel (剥离之前和之后)是一个字符串
  • 假设每个对象都是字符串是不安全的,假设一切都是随机的,因为它是

最佳答案

只要能保证channel始终在合理的字符范围内(例如字母数字),将channel.length() + 2更改为channel.length() + 4 应该足够了。

关于Java:从字节数组中删除第一个UTF字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23299972/

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