1
votes

I wish to peek a byte of the binary data coming in using binary reader. Using PeekChar works fine only if it is UTF-8. Otherwise, I get the error "The output char buffer is too small to contain the decoded characters, encoding 'Unicode (UTF-8)' fallback".

Is there a way for binary reader to peek one byte irrespective of the encoding?

Stream stream = new MemoryStream(buffer);
BinaryReader reader = new BinaryReader(stream);
type = (uint)reader.PeekChar(); // Throws error I mentioned for a particular case
1
Is the data text? UTF8 can have multi-byte characters. Are you looking for the next character or the next byte? - Scott Chamberlain
@ScottChamberlain No. Its binary data. I am looking for the next byte. - Tyler Durden

1 Answers

2
votes

BinaryReader can't peek a single byte, as simple as that. However, in your case, it's not really important. Since you have a MemoryStream underlying the BinaryReader, you can safely do a ReadByte and just move the Position back one byte.