I'm going to suggest that even though you requested 1024 bytes, you actually got back 1023, and you can check bytesRead
to verify this.
This is allowed by FileStream
implementations. See documentation here.
The Read method returns zero only after reaching the end of the stream. Otherwise, Read always reads at least one byte from the stream before returning. If no data is available from the stream upon a call to Read, the method will block until at least one byte of data can be returned. An implementation is free to return fewer bytes than requested even if the end of the stream has not been reached.
Your code is still correct, in that it only stops reading when zero is returned. But you can't expect every chunk read from the file to be exactly 1024 bytes - as you can see, it's not necessarily always the case.