java协议缓冲区:从文件中读取所有序列化消息
我正在尝试序列化和反序列化protobuf消息。按照documentation中的例子,我写道:
ByteArrayOutputStream out = new ByteArrayOutputStream(...);
ArrayList<AddressBookProtos.Person> messages = ...;
for (int i = 0; i < messages.size; i++){
messages.writeTo(out);
out.flush();
}
out.close();
当我读回这些信息时,我希望写下:
ByteArrayInputStream in = new ByteArrayInputStream(...);
ArrayList<AddressBookProtos.Person> messages = new ArrayList<...>();
while (in.available() > 0) {
messages.add(AddressBookProtos.Person.parseFrom(in));
}
然而,我总是只得到一个结果。其他结果都到哪里去了
# 1 楼答案
您必须使用^{} /
parseDelimitedFrom
或实现自己的基于分隔符的解决方案根据javadoc: