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

I use System.IO.Stream.Read(byte[] buffer, int offset, int count). Is there an alternative to this method (or a property to set) so that the method does not return until the entire number of samples has been read (or the end of the stream)? Or should I do something like this:

int n = 0, readCount = 0;
while ((n = myStream.Read(buffer, readCount, countToRead - readCount)) > 0)
    readCount += n;
+3
source share
1 answer

BinaryReader.ReadBytes blocks the desired path. However, this is not equivalent to reading until the end of the stream. (You do not want to call BinarReader.ReadBytes(int.MaxValue)- it will try to create a 2 GB buffer!)

I am using a MemoryStream to read all data from a stream of unknown size. See this related question for sample code.

+9
source

All Articles