Blocking version of System.IO.Stream.Read(byte[], int, int)

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;


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.

Need Your Help

Convert an object into list or array MVC

model-view-controller arraylist hashtable

I have an MVC grid control that is passing back the collection to my controller as an object.