In the callback for NetworkStream.BeginRead
I seem to notice that all bytes are always read. I see many tutorials check to see if the BytesRead is less than the total bytes and if so, read again, but this never seems to be the case.
The condition if (bytesRead < totalBytes)
never fires, even if a lot of data is sent at once (thousands of characters) and even if the buffer size is set to a very small value (16 or so).
I have not tested this with the 'old-fashioned way' as I am using Task.Factory.FromAsync instead of calling NetworkStream.BeginRead and providing a callback where I call EndRead. Perhaps Tasks automatically include this functionality of not returning until all data is read? I'm not sure.
Either way, I am still curious as to when all data would not be read at once. Is it even required to check if not all data was read, and if so, read again? I cannot seem to get the conditional to ever run.
Thanks.
Try sending megabytes of data over a slow link. Why would the stream want to wait until it was all there before giving the caller any of it? What if the other side hadn't closed the connection - there is no concept of "all the data" at that point.
Suppose you open a connection to another server and call
BeginRead
(orRead
) with a large buffer, but it only sends 100 bytes, then waits for your reply - what would you expectNetworkStream
to do? Never give you the data, because you gave it too big a buffer? That would be highly counterproductive.You should absolutely not assume that any stream (with the arguable exception of
MemoryStream
) will fill the buffer you give it. It's possible thatFileStream
always will for local files, but I'd expect it not to for shared files.EDIT: Sample code which shows the buffer not being filled - making an HTTP 1.1 request (fairly badly :)