Suppose we have a code block like this :
//Assuming s is a stream:
byte[] data = new byte[1000];
s.Read(data,0,data.Length);
The read Method could read anywhere from 1 to 1000 bytes, leaving the balance of the stream unread.
This is what a C# book says.
I don't understand,why Read
method would read anywhere from the stream ? It ain't reading all of the stream ?
And it says the work-around should be like this :
//bytesRead will always end up at 1000, unless the stream is itself smaller in length:
int bytesRead = 0;
int chunkSize = 1;
while(bytesRead < data.Length && chunkSize > 0 )
bytesRead += chunkSize = s.Read(data,bytesRead,dataLength-bytesRead);
This code above is also provided by the book as a work-around. What I am trying to understand whether Read method is beginning to read to the end and write all of the bytes in specified range to byte array. Why is he using the bytesRead
as a start point in s.Read(data,bytesRead,dataLength-bytesRead);
Thanks in advance.