By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,864 Members | 1,266 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,864 IT Pros & Developers. It's quick & easy.

Why is PeekChar causing a problem?

P: n/a
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,
--
Anil Gupte
www.keeninc.net
www.icinema.com
Sep 21 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Anil Gupte wrote:
Additional information: Conversion buffer overflow
Hi Anil

I found this link with a quick google may be of some help to you

http://www.thescripts.com/forum/thread349779.html

--
DaveG
------------
If things don't change...
They will stay the same.
Sep 21 '06 #2

P: n/a
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" <an*******@icinema.comwrote in message
news:OI**************@TK2MSFTNGP05.phx.gbl...
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,
--
Anil Gupte
www.keeninc.net
www.icinema.com
Sep 21 '06 #3

P: n/a
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 [MVP - Outlook]" <Ja************@tsbradley.netwrote in
message news:84**********************************@microsof t.com...
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" <an*******@icinema.comwrote in message
news:OI**************@TK2MSFTNGP05.phx.gbl...
>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,
--
Anil Gupte
www.keeninc.net
www.icinema.com

Sep 23 '06 #4

P: n/a
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" <an*******@icinema.comwrote in message
news:eR*************@TK2MSFTNGP02.phx.gbl...
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 [MVP - Outlook]" <Ja************@tsbradley.netwrote in
message news:84**********************************@microsof t.com...
>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" <an*******@icinema.comwrote in message
news:OI**************@TK2MSFTNGP05.phx.gbl...
>>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,
--
Anil Gupte
www.keeninc.net
www.icinema.com

Sep 24 '06 #5

P: n/a
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 [MVP - Outlook]" <Ja************@tsbradley.netwrote in
message news:1E**********************************@microsof t.com...
>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" <an*******@icinema.comwrote in message
news:eR*************@TK2MSFTNGP02.phx.gbl...
>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 [MVP - Outlook]" <Ja************@tsbradley.netwrote in
message news:84**********************************@microsof t.com...
>>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" <an*******@icinema.comwrote in message
news:OI**************@TK2MSFTNGP05.phx.gbl...
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,
--
Anil Gupte
www.keeninc.net
www.icinema.com



Sep 25 '06 #6

P: n/a
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" <an*******@icinema.comwrote in message
news:Ol**************@TK2MSFTNGP06.phx.gbl...
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 [MVP - Outlook]" <Ja************@tsbradley.netwrote in
message news:1E**********************************@microsof t.com...
>>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" <an*******@icinema.comwrote in message
news:eR*************@TK2MSFTNGP02.phx.gbl...
>>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 [MVP - Outlook]" <Ja************@tsbradley.netwrote in
message news:84**********************************@microsof t.com...
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" <an*******@icinema.comwrote in message
news:OI**************@TK2MSFTNGP05.phx.gbl...
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,
--
Anil Gupte
www.keeninc.net
www.icinema.com
>

Sep 27 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.