views:

67

answers:

4

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

A: 

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.

Steve Danner
Try it on a DeflateStream sometime . . .
Jim Mischel
And with what kind of stream?
Henk Holterman
@Jim I did not know that. That's good information, thanks. Henk's answer gets a +1 then.
Steve Danner
+1  A: 

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

JaredPar
+2  A: 

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.

Hans Passant