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;

Answers


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

Reading in multiple files in R in the same table, as different variables

r statistics

I have 100 files, each named rundataX, where x is the number. I want to read them in in R in one table called mydata. Each of the rundata files has 3 columns: tmX, scoreX and currentX. I can read i...

How is ray coherence used to improve raytracing speed while still looking realistic?

optimization raytracing raycasting

I'm considering exploiting ray coherence in my software per-pixel realtime raycaster.