Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does BinaryReader do if the bytes I am reading aren't present yet?

I am using a BinaryReader on top of a NetworkStream to read data off of a network. This has worked really well for me, but I want to understand what's going on behind the scenes, so I took a look at the documentation for BinaryReader and found it to be extremely sparse.

My question is this: What will BinaryReader.ReadBytes(bufferSize) do if bufferSize bytes are not present on the network stream when I call ReadBytes?

In my mind there are a few options:
1) Read any bytes that are present on the network stream and return only that many
2) Wait until bufferSize bytes are present on the stream, then read
3) Throw an exception

I assume option 2 is happening, since I've never received any exceptions and all my data is received whole, not in pieces. However, I would like to know for sure what is going on. If someone could enlighten me, I would be grateful.

like image 733
Darkhydro Avatar asked May 30 '12 05:05

Darkhydro


1 Answers

I believe it actually goes for hidden option 4:

  • Read the data as it becomes available, looping round in the same way that you normally would do manually. It will only return a value less than the number of bytes you asked for if it reaches the end of the stream while reading.

This is subtly different from your option 2 as it does drain the stream as data becomes available - it doesn't wait until it could read all of the data in one go.

It's easy to show that it does return a lower number of bytes than you asked for if it reaches the end:

var ms = new MemoryStream(new byte[10]);
var readData = new BinaryReader(ms).ReadBytes(100);
Console.WriteLine(readData.Length); // 10

It's harder to prove the looping part, without a custom stream which would explicitly require multiple Read calls to return all the data.

The documentation isn't as clear as it might be, but the return value part is at least somewhat helpful:

A byte array containing data read from the underlying stream. This might be less than the number of bytes requested if the end of the stream is reached.

Note the final part that I've highlighted, and compare that with Stream.Read:

The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.

If you're expecting an exact amount of data and only that amount will be useful, I suggest you write a ReadExactly method which calls Read and throws EndOfStreamException if you need more data than the stream provided before it was closed.

like image 82
Jon Skeet Avatar answered Nov 15 '22 20:11

Jon Skeet