I am getting data from the NetworkStream of a TcpClient and I want to write the data into a fixed-length MemoryStream, because I know the size of the data.
I use Stream.CopyToAsync to write the data into the MemoryStream:
var result = new MemoryStream(size);
await ns.CopyToAsync(result);
result.Position = 0;
return result;
Looking at the source code of Stream.CopyToAsync, the method terminates when there is no data in the source stream. In my case, when the network connection is slow or interrupted, my method would return an incomplete MemoryStream.
Can I ignore this possibility or should I read the stream by my own since I know how much data will be sent?