BinaryReader does not have EndOfStream property. Is it safe to use following code to check if end of stream is reached?
reader.BaseStream.Length>reader.BaseStream.Position
The easiest way I found is to check the returned value of the BinaryReader's PeekChar() method. If it returns -1, then you reached the end of the stream.
It depends. There are various stream types that do not implement the Length or Position property, you'd get a NotSupportedException. NetworkStream for example. Of course, if you'd use such a stream then you really do have to know up front how often to call the BinaryReader.Read() method. So, yes, it's fine.
This won't work as a general solution because it assumes that the BaseStream
value supports the Length
property. Many Stream
implementation do not and instead throw a NotSupportedException
. In particular any networking base stream such as HttpRequestStream
and NetworkStream
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With