Hi,
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
Hi,
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
That's what I've always done in the past and I've never seen a problem with it. The code using it has been in a production environment for 2+ years.
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
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.