0

The documentation for System.IO.BinaryReader.Read(byte[], int, int) says it can throw an ArgumentException if

The number of decoded characters to read is greater than count. This can happen if a Unicode decoder returns fallback characters or a surrogate pair.

I don't understand how encoding comes into play at all when I'm asking for raw bytes. Would it interpret the underlying stream's bytes as Unicode and skip things like the byte order mark?

Even if it does something like surrogate pair resolution, how would that end up creating more bytes than I asked for, not less?

Assuming the BinaryReader's encoding does not affect the underlying Stream, does that mean that binaryReader.Read(..) and binaryReader.BaseStream.Read(..) are fundamentally not the same? They seem to be exactly the same in Mono's implementation of BinaryReader. The decoder is not involved in the implementation of this function either.

Is this simply a copy/paste error in the MSDN documentation?

The reason I'm asking all of this is because I just ran into the ArgumentException with this block of code, and of the two documented cases that can throw an ArgumentException it can't be the trivial one:

public void Foo(BinaryReader reader)
{
    int bar = reader.ReadInt32();
    int baz = reader.ReadInt32();

    int bufferSize = 8192;
    var buffer = new byte[bufferSize];

    int bytesRead = 0;
    while ( (bytesRead = reader.Read(buffer, 0, bufferSize)) != 0 )
    {
        // do something with the read bytes here
        ...
    }
}
1
  • " I just ran into the ArgumentException" -- do you happen to have more information on the error itself? I just played around and even merging Unicode and ASCII byte arrays, the Read functions just fine. Can you reproduce and show us the stacktrace and message? Commented Aug 9, 2017 at 20:23

1 Answer 1

3

Is this simply a copy/paste error in the MSDN documentation?

Likely. According to the reference source, the character encoding does not come in to play.

http://referencesource.microsoft.com/#mscorlib/system/io/binaryreader.cs,504

The reason I'm asking all of this is because I just ran into the ArgumentException with this block of code, and of the two documented cases that can throw an ArgumentException.

It does throw an ArgumentException if the buffer is too small:

if (buffer.Length - index < count)
    throw ArgumentException(...);
Sign up to request clarification or add additional context in comments.

2 Comments

The OP is saying that the trivial case shouldn't happen since they pass 0 as the index and the size of the buffer as the count.
OP is asking if ArgumentException can be thrown in that overload of Read due to encoding issues. I have given a link to the reference source showing it does in fact not do that for encoding. Perhaps the underlying Stream that BinaryReader threw an exception in its own read. At this point it's a guessing game.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.