This is a little more complicated than I imagined. I am trying to read n bytes from a stream.
MSDN claims that Read should not return n bytes, it should simply return at least 1 and up to n bytes, with 0 bytes being a special case of reaching the end of a stream.
I usually use something like
var buf = new byte[size]; var count = stream.Read (buf, 0, size); if (count != size) { buf = buf.Take (count).ToArray (); } yield return buf;
I hope to get exactly size bytes, but according to the specification FileStream will be allowed to return a large number of 1-byte blocks. This should be avoided.
One way to resolve this issue is to have 2 buffers, one for reading and one for collecting chunks, until we get the requested number of bytes. It's a little cumbersome though.
I also looked at BinaryReader , but its specification also did not explicitly indicate that n bytes would be returned exactly.
To clarify: Of course, at the end of the stream, the returned number of bytes may be less than size - this is not a problem. I am only talking about not getting n bytes, even if they are available in the stream.
mafu
source share