Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many bits does BinaryReader.PeekChar() read?

I am working on improving a stream reader class that uses a BinaryReader. It consists of a while loop that uses .PeekChar() to check if more data exists to continue processing.

The very first operation is a .ReadInt32() which reads 4 bytes. What if PeekChar only "saw" one byte (or one bit)? This doesn't seem like a reliable way of checking for EOF.

The BinaryReader is constructed using its default parameters, which as I understand it, uses UTF8 as the default encoding. I assume that .PeekChar() checks for 8 bits but I really am not sure.

How many bits does .PeekChar() look for? (and what are some alternate methods to checking for EOF?)

like image 669
JYelton Avatar asked Aug 24 '11 20:08

JYelton


People also ask

What is BinaryReader in c#?

The BinaryReader class provides methods that simplify reading primitive data types from a stream. For example, you can use the ReadBoolean method to read the next byte as a Boolean value and advance the current position in the stream by one byte. The class includes read methods that support different data types.

What is the binary reader and writer class define its functionalities?

The BinaryWriter class is used to write binary data to a stream. A BinaryWriter object is created by passing a FileStream object to its constructor. The following table describes commonly used methods of the BinaryWriter class. Sr.No. Function & Description.


1 Answers

Here BinaryReader.PeekChar

I read:

ArgumentException: The current character cannot be decoded into the internal character buffer by using the Encoding selected for the stream.

This makes clear that amount of memory read depends on Encoding applied to that stream.

EDIT

Actually definition according to MSDN is:

Returns the next available character and does not advance the byte or character position.*

Infact, it depends on encoding if this is a byte or more...

Hope this helps.

like image 124
Tigran Avatar answered Sep 30 '22 03:09

Tigran