Is it safe to use Stream.Seek when a BinaryReader is open?

前端 未结 3 622
长发绾君心
长发绾君心 2021-01-05 02:50

Because of the under the hood buffering strategy of BinaryReader, it is unclear to me whether is it ok or not to read an offset stored in a stream, then reposition the strea

相关标签:
3条回答
  • 2021-01-05 03:13

    BinaryReader does use a buffer but only to read enough bytes from the base stream to convert a value. In other words, ReadInt32() will buffer 4 bytes first, ReadDecimal() will buffer 16 bytes first, etcetera. ReadString() is the trickier method but it has counter-measures as well, a string is encoded in the file by BinaryWriter which writes the string length first. So that BinaryReader knows exactly how many bytes to buffer before converting the string.

    So the buffer is always empty after one of the ReadXxx() method returns and calling Seek() on the BaseStream is fine. Also the reason that Microsoft didn't need to override the Seek() method.

    The cautionary note in the MSDN article is appropriate, you certainly will read that "offset" value more than once if you call a ReadXxx() method after the Seek() call. I however assume that was entirely intentional.

    0 讨论(0)
  • 2021-01-05 03:24

    In my experience so long as you are using them both synchronously and no other thread is doing anything with the stream then it works perfectly fine.

    I do that extensively in applications I have written to work with binary file formats and have never encountered an issue.

    0 讨论(0)
  • 2021-01-05 03:28

    I would say that it is not always safe (although it might be safe in some cases).

    The Microsoft documentation for BinaryReader.BaseStream explicitly states:

    Using the underlying stream while reading or while using the BinaryReader can cause data loss and corruption. For example, the same bytes might be read more than once, bytes might be skipped, or character reading might become unpredictable.

    So I would avoid it.

    (Interestingly, there's a BinaryWriter.Seek() method, but no BinaryReader.Seek().)

    0 讨论(0)
提交回复
热议问题