Why is PeekChar causing a problem?

A

Anil Gupte

Here is my code:

Dim fsReadStream As New FileStream(L3FileName, FileMode.Open,
FileAccess.Read)
Dim brReader As New BinaryReader(fsReadStream)
Dim ByteArray() As Byte
While brReader.PeekChar() > -1
ByteArray = brReader.ReadBytes(1)
End While

It processes for a while and then causes an exception and the line it stops
at is the While .... I get the message:

An unhandled exception of type 'System.ArgumentException' occurred in
mscorlib.dll
Additional information: Conversion buffer overflow.

I think it was working before, but appears to have stopped. Any ideas why
this is happening?

Thanx,
 
J

Jay B. Harlow [MVP - Outlook]

Anil,
Because you open the file for binary access:
Dim brReader As New BinaryReader(fsReadStream)

And are reading the file for binary access:
ByteArray = brReader.ReadBytes(1)


Yet are attempting to process it as a series of Chars:
While brReader.PeekChar() > -1

A Char can be one, two, or more bytes based on the encoding used. I want to
say that BinaryReader will default to UTF-8 if not given an Encoding in its
constructor. UTF-8 uses one, two, or more bytes to encode characters. If you
are near the end of the file & the encoding wants more bytes for the char
then available, you get the idea...


Generally you want to compare the number of bytes returned to the number of
bytes requested. If the number of bytes returned is 0 or less then requested
then you are at the end of the stream.
Dim ByteArray() As Byte
Do
ByteArray = brReader.ReadBytes(1)
Loop Until ByteArrary.Length = 0
 
A

Anil Gupte

You are correct, however, as I understand itl, the PeekChar and the inary
read are two different processes. In fact, I tested this, and made sure to
stop well before the end of the file was reached. And it worked fine. I
guess, the BinaryReader takes the pointer past the end of file and the
Peekhar then chokes. Bad thnking on Micorsoft's part. If PeekChar is nul
or EOF hass been reached, it should return some standard value such as -1.

My $0.02
--
Anil Gupte
www.keeninc.net
www.icinema.com

Jay B. Harlow said:
Anil,
Because you open the file for binary access:
Dim brReader As New BinaryReader(fsReadStream)

And are reading the file for binary access:
ByteArray = brReader.ReadBytes(1)


Yet are attempting to process it as a series of Chars:
While brReader.PeekChar() > -1

A Char can be one, two, or more bytes based on the encoding used. I want
to say that BinaryReader will default to UTF-8 if not given an Encoding in
its constructor. UTF-8 uses one, two, or more bytes to encode characters.
If you are near the end of the file & the encoding wants more bytes for
the char then available, you get the idea...


Generally you want to compare the number of bytes returned to the number
of bytes requested. If the number of bytes returned is 0 or less then
requested then you are at the end of the stream.
Dim ByteArray() As Byte
Do
ByteArray = brReader.ReadBytes(1)
Loop Until ByteArrary.Length = 0

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Anil Gupte said:
Here is my code:

Dim fsReadStream As New FileStream(L3FileName, FileMode.Open,
FileAccess.Read)
Dim brReader As New BinaryReader(fsReadStream)
Dim ByteArray() As Byte
While brReader.PeekChar() > -1
ByteArray = brReader.ReadBytes(1)
End While

It processes for a while and then causes an exception and the line it
stops at is the While .... I get the message:

An unhandled exception of type 'System.ArgumentException' occurred in
mscorlib.dll
Additional information: Conversion buffer overflow.

I think it was working before, but appears to have stopped. Any ideas
why this is happening?

Thanx,
 
J

Jay B. Harlow [MVP - Outlook]

I guess, the BinaryReader takes the pointer past the end of file and the
Peekhar then chokes.
I would say the PeekChar takes the pointer past the end of file and then it
chokes. That the PeekChar is wanting, insisting on, 3 or 4 bytes & they are
not there ergo the exception.

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Anil Gupte said:
You are correct, however, as I understand itl, the PeekChar and the inary
read are two different processes. In fact, I tested this, and made sure
to stop well before the end of the file was reached. And it worked fine.
I guess, the BinaryReader takes the pointer past the end of file and the
Peekhar then chokes. Bad thnking on Micorsoft's part. If PeekChar is nul
or EOF hass been reached, it should return some standard value such as -1.

My $0.02
--
Anil Gupte
www.keeninc.net
www.icinema.com

Jay B. Harlow said:
Anil,
Because you open the file for binary access:
Dim brReader As New BinaryReader(fsReadStream)

And are reading the file for binary access:
ByteArray = brReader.ReadBytes(1)


Yet are attempting to process it as a series of Chars:
While brReader.PeekChar() > -1

A Char can be one, two, or more bytes based on the encoding used. I want
to say that BinaryReader will default to UTF-8 if not given an Encoding
in its constructor. UTF-8 uses one, two, or more bytes to encode
characters. If you are near the end of the file & the encoding wants more
bytes for the char then available, you get the idea...


Generally you want to compare the number of bytes returned to the number
of bytes requested. If the number of bytes returned is 0 or less then
requested then you are at the end of the stream.
Dim ByteArray() As Byte
Do
ByteArray = brReader.ReadBytes(1)
Loop Until ByteArrary.Length = 0

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Anil Gupte said:
Here is my code:

Dim fsReadStream As New FileStream(L3FileName, FileMode.Open,
FileAccess.Read)
Dim brReader As New BinaryReader(fsReadStream)
Dim ByteArray() As Byte
While brReader.PeekChar() > -1
ByteArray = brReader.ReadBytes(1)
End While

It processes for a while and then causes an exception and the line it
stops at is the While .... I get the message:

An unhandled exception of type 'System.ArgumentException' occurred in
mscorlib.dll
Additional information: Conversion buffer overflow.

I think it was working before, but appears to have stopped. Any ideas
why this is happening?

Thanx,
 
A

Anil Gupte

According to the documentation, PeekChar never moves the pointer. The
BinaryReader does, and so when PeekChar sees the end of file, it should
return a -1 or some such value.

--
Anil Gupte
www.keeninc.net
www.icinema.com

Jay B. Harlow said:
I guess, the BinaryReader takes the pointer past the end of file and the
Peekhar then chokes.
I would say the PeekChar takes the pointer past the end of file and then
it chokes. That the PeekChar is wanting, insisting on, 3 or 4 bytes & they
are not there ergo the exception.

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Anil Gupte said:
You are correct, however, as I understand itl, the PeekChar and the inary
read are two different processes. In fact, I tested this, and made sure
to stop well before the end of the file was reached. And it worked fine.
I guess, the BinaryReader takes the pointer past the end of file and the
Peekhar then chokes. Bad thnking on Micorsoft's part. If PeekChar is
nul or EOF hass been reached, it should return some standard value such
as -1.

My $0.02
--
Anil Gupte
www.keeninc.net
www.icinema.com

Jay B. Harlow said:
Anil,
Because you open the file for binary access:

Dim brReader As New BinaryReader(fsReadStream)

And are reading the file for binary access:

ByteArray = brReader.ReadBytes(1)


Yet are attempting to process it as a series of Chars:

While brReader.PeekChar() > -1

A Char can be one, two, or more bytes based on the encoding used. I want
to say that BinaryReader will default to UTF-8 if not given an Encoding
in its constructor. UTF-8 uses one, two, or more bytes to encode
characters. If you are near the end of the file & the encoding wants
more bytes for the char then available, you get the idea...


Generally you want to compare the number of bytes returned to the number
of bytes requested. If the number of bytes returned is 0 or less then
requested then you are at the end of the stream.

Dim ByteArray() As Byte
Do
ByteArray = brReader.ReadBytes(1)
Loop Until ByteArrary.Length = 0

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Here is my code:

Dim fsReadStream As New FileStream(L3FileName, FileMode.Open,
FileAccess.Read)
Dim brReader As New BinaryReader(fsReadStream)
Dim ByteArray() As Byte
While brReader.PeekChar() > -1
ByteArray = brReader.ReadBytes(1)
End While

It processes for a while and then causes an exception and the line it
stops at is the While .... I get the message:

An unhandled exception of type 'System.ArgumentException' occurred in
mscorlib.dll
Additional information: Conversion buffer overflow.

I think it was working before, but appears to have stopped. Any ideas
why this is happening?

Thanx,
 
J

Jay B. Harlow [MVP - Outlook]

Anil,
Correct, it doesn't move the Stream Pointer.

However! as I stated in my initial comments it does move an encoder
"pointer".

PeekChar returns a char, BinaryReader needs to use a System.Text.Encoding,
by default a UTF8 encoding object is used to convert 1 to 4 bytes into that
Char. If there are not enough bytes to create a char. Bam!

I never really suggested that the BinaryReader was moving its Position
property (pointer) when its translating the next few bytes into a Char.


Ergo Don't attempt to read a Char if you don't have chars to read.



--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Anil Gupte said:
According to the documentation, PeekChar never moves the pointer. The
BinaryReader does, and so when PeekChar sees the end of file, it should
return a -1 or some such value.

--
Anil Gupte
www.keeninc.net
www.icinema.com

Jay B. Harlow said:
I guess, the BinaryReader takes the pointer past the end of file and the
Peekhar then chokes.
I would say the PeekChar takes the pointer past the end of file and then
it chokes. That the PeekChar is wanting, insisting on, 3 or 4 bytes &
they are not there ergo the exception.

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Anil Gupte said:
You are correct, however, as I understand itl, the PeekChar and the
inary read are two different processes. In fact, I tested this, and
made sure to stop well before the end of the file was reached. And it
worked fine. I guess, the BinaryReader takes the pointer past the end of
file and the Peekhar then chokes. Bad thnking on Micorsoft's part. If
PeekChar is nul or EOF hass been reached, it should return some standard
value such as -1.

My $0.02
--
Anil Gupte
www.keeninc.net
www.icinema.com

message Anil,
Because you open the file for binary access:

Dim brReader As New BinaryReader(fsReadStream)

And are reading the file for binary access:

ByteArray = brReader.ReadBytes(1)


Yet are attempting to process it as a series of Chars:

While brReader.PeekChar() > -1

A Char can be one, two, or more bytes based on the encoding used. I
want to say that BinaryReader will default to UTF-8 if not given an
Encoding in its constructor. UTF-8 uses one, two, or more bytes to
encode characters. If you are near the end of the file & the encoding
wants more bytes for the char then available, you get the idea...


Generally you want to compare the number of bytes returned to the
number of bytes requested. If the number of bytes returned is 0 or less
then requested then you are at the end of the stream.

Dim ByteArray() As Byte
Do
ByteArray = brReader.ReadBytes(1)
Loop Until ByteArrary.Length = 0

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Here is my code:

Dim fsReadStream As New FileStream(L3FileName, FileMode.Open,
FileAccess.Read)
Dim brReader As New BinaryReader(fsReadStream)
Dim ByteArray() As Byte
While brReader.PeekChar() > -1
ByteArray = brReader.ReadBytes(1)
End While

It processes for a while and then causes an exception and the line it
stops at is the While .... I get the message:

An unhandled exception of type 'System.ArgumentException' occurred in
mscorlib.dll
Additional information: Conversion buffer overflow.

I think it was working before, but appears to have stopped. Any ideas
why this is happening?

Thanx,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top