为什么CodedInputStream将流位置设置为结束?

时间:2015-11-16 11:08:36

标签: c# protocol-buffers

我在c#中使用协议缓冲区3。我试图通过流反弹来查找每条消息的起始位置,而不实际反序列化消息。所有消息都使用WriteDelimitedTo写入流。

然后我使用此代码尝试从长度标记跳转:

_map = new List<int>();
_stream.Seek(0, SeekOrigin.Begin);

var codedStream = new CodedInputStream(_stream);

while (_stream.Position < _stream.Length)
{
    var length = codedStream.ReadInt32();

    _map.Add((int) _stream.Position);

    _stream.Seek(length, SeekOrigin.Current);
}

然而,当我codedStream.ReadInt32()时,流位置被设置为结束,而不是仅仅是varint32之后的下一个字节。

1 个答案:

答案 0 :(得分:1)

此行为是由于CodedInputStream将原始流缓冲为you can see in the source code。它可能不适合手动阅读和搜索流。另一种方法是使用Marc Gravell的部分源代码来读取varintavailable here,然后直接移动原始流。

相关问题