为什么CodedInputStream集流位置结束? [英] Why does CodedInputStream set stream position to end?

查看:352
本文介绍了为什么CodedInputStream集流位置结束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#中协议缓冲区3。我想通过一个流的反弹找到每个消息的开始位置,而无需实际Deserialising的消息。所有消息都写入流与 WriteDelimitedTo

I'm using protocol buffers 3 in c#. I'm trying to bounce through a stream to find the start locations of each message, without actually Deserialising the messages. All messages are written to the stream with WriteDelimitedTo.

然后我用这个代码,试图从长度标记跳:

I then use this code to try to jump from length markers:

_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之后的下一个字节。

However, the moment I do codedStream.ReadInt32() the stream position is set to the end, rather than just the next byte after the varint32.

推荐答案

此行​​为是由于 CodedInputStream 缓冲原始流为< A HREF =htt​​ps://searchcode.com/codesearch/view/12180469/相对=nofollow>您可以在源代码中看到。它可能是不适合手动读取,并通过一个流求。另一种方法是使用马克·Gravell的源代码部分用于读取 varint 可在这里和移动虽然直接的原始流。

This behaviour is due to CodedInputStream buffering the original stream as you can see in the source code. It is probably unsuitable for manually reading and seeking through a stream. An alternative is to use parts of Marc Gravell's source code for reading a varint, available here, and move though the raw stream directly.

这篇关于为什么CodedInputStream集流位置结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆