3

I'm using System.IO.Stream.Read(byte[] buffer, int offset, int count). Is there an alternative to that method (or a property to set) so that the method won't return until all count is read (or end of stream is reached)? Or should I do something of like this:

int n = 0, readCount = 0;
while ((n = myStream.Read(buffer, readCount, countToRead - readCount)) > 0)
    readCount += n;
Hosam Aly
  • 41,555
  • 36
  • 141
  • 182

1 Answers1

9

BinaryReader.ReadBytes blocks in the desired way. That's not equivalent to reading to the end of the stream, however. (You don't want to call BinarReader.ReadBytes(int.MaxValue) - it will try to create a 2GB buffer!)

I tend to use a MemoryStream for reading all the data from a stream of an unknown size. See this related question for sample code.

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194