473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Why is PeekChar causing a problem?

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
6 2148
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jim C Nguyen | last post by:
I have a table with ~2.2 million rows. Sometimes when I do an update to one single row it will instead update ALL of the rows using the same update. This happens every one in about 500,000...
11
by: Timothy Shih | last post by:
Hi, I am having a freezing issue with my application. My application serves several remotable objects, all of which must be initialized before their use. Furthermore, some of them depend on each...
10
by: BBFrost | last post by:
We just recently moved one of our major c# apps from VS Net 2002 to VS Net 2003. At first things were looking ok, now problems are starting to appear. So far ... (1) ...
19
by: Mikolas | last post by:
I was trying to use BinaryReader.PeekChar to "peek" the next byte, that works fine unless the byte value's above 127 .... is there anything I can do abouit it? thnx, mikolas
1
by: D A H | last post by:
I have gotten the same exception in multiple projects. I have solved the underlying problem. My question is if anyone knew of a setting that would cause this exception to be thrown. A...
2
by: Rob Meade | last post by:
Hi all, We have recently adopted to using .net for our web applications which were previously written in vanilla ASP. Things have been going ok until recently, or at least its only recently...
5
by: Tim | last post by:
Hi, I'm experiencing some problem with the following code: st = File.Open(sFilename, FileMode.Open, FileAccess.ReadWrite) br = New BinaryReader(st) Do Until br.PeekChar = -1 Dim buffer()...
4
by: Geordie | last post by:
Hi, I'm in the process of converting a production VS 2003 application to VS 2005. To simplify the conversion I'm converting a small piece at a time and then unit testing the code to confirm it...
3
by: mthomsit | last post by:
Hi, I have a script which after a time shows the following error in IE: "A script on this page is causing Internet Explorer to run slowly. If it continues to run, your computer may become...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.