tags:

views:

882

answers:

2

Hello,

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;
A: 
byte[] myBytes = System.IO.File.ReadAllBytes("MyFileName.dat");
Binary Worrier
+4  A: 

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.

Jon Skeet