[英]Why Does Passing A StreamReader Between Methods Set It To the End of the Stream?
[英]Why does CodedInputStream set stream position to end?
我在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之后的下一個字節。
此行為是由於CodedInputStream
緩沖了原始流(如您在源代碼中所看到的) 。 它可能不適合手動讀取和搜索流。 一種替代方法是使用Marc Gravell的部分源代碼讀取varint
( 在此處可用) ,並直接通過原始流。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.