我们正在考虑使用Protocol Buffers进行二进制日志记录,因为:
也就是说,我们应该如何处理它并不明显,因为API倾向于专注于创建整个对象,因此将DataLogEntry列表包装为DataLogFile中的重复字段将是您在消息传递中所做的事情术语,但我们真正想要的只是能够编写然后读取整个DataLogEntry,将其附加到文件的末尾。
我们这样做的第一个问题是这样做(在测试中:
FileInputStream fileIn = new FileInputStream(logFile);
CodedInputStream in = CodedInputStream.newInstance(fileIn);
while(!in.isAtEnd()) {
DataLogEntry entry = DataLogEntry.parseFrom(in);
// ... do stuff
}
仅导致从流中读取1个DataLogEntry。没有isAtEnd,它永远不会停止。
思想?
编辑:我已切换到使用entry.writeDelimitedTo和BidLogEntry.parseDelimitedFrom,这似乎有效......
答案 0 :(得分:4)
根据我对协议的理解,在单个流中缓存does not support个多条消息。因此,您可能需要自己跟踪消息的边界。您可以通过在日志中的每条消息之前存储消息的大小来实现此目的。
public class DataLog {
public void write(final DataOutputStream out, final DataLogEntry entry) throws IOException {
out.writeInt(entry.getSerializedSize());
CodedOutputStream codedOut = CodedOutputStream.newInstance(out);
entry.writeTo(codedOut);
codedOut.flush();
}
public void read(final DataInputStream in) throws IOException {
byte[] buffer = new byte[4096];
while (true) {
try {
int size = in.readInt();
CodedInputStream codedIn;
if (size <= buffer.length) {
in.read(buffer, 0, size);
codedIn = CodedInputStream.newInstance(buffer, 0, size);
} else {
byte[] tmp = new byte[size];
in.read(tmp);
codedIn = CodedInputStream.newInstance(tmp);
}
DataLogEntry.parseFrom(codedIn);
// ... do stuff
}
catch (final EOFException e) {
break;
}
}
}
}
注意:我已经使用EOFException来查找文件的结尾,您可能希望使用分隔符或跟踪手动读取的字节数。
答案 1 :(得分:4)
至少2.4.0a,这很容易。用writeDelimitedTo写你的消息。无需直接使用编码流。