473,545 Members | 2,051 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why is PeekChar causing a problem?

Here is my code:

Dim fsReadStream As New FileStream(L3Fi leName, FileMode.Open,
FileAccess.Read )
Dim brReader As New BinaryReader(fs ReadStream)
Dim ByteArray() As Byte
While brReader.PeekCh ar() -1
ByteArray = brReader.ReadBy tes(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.Argumen tException' 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 2159
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(fs ReadStream)
And are reading the file for binary access:
ByteArray = brReader.ReadBy tes(1)

Yet are attempting to process it as a series of Chars:
While brReader.PeekCh ar() -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.ReadBy tes(1)
Loop Until ByteArrary.Leng th = 0
--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Anil Gupte" <an*******@icin ema.comwrote in message
news:OI******** ******@TK2MSFTN GP05.phx.gbl...
Here is my code:

Dim fsReadStream As New FileStream(L3Fi leName, FileMode.Open,
FileAccess.Read )
Dim brReader As New BinaryReader(fs ReadStream)
Dim ByteArray() As Byte
While brReader.PeekCh ar() -1
ByteArray = brReader.ReadBy tes(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.Argumen tException' 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.netw rote in
message news:84******** *************** ***********@mic rosoft.com...
Anil,
Because you open the file for binary access:
>Dim brReader As New BinaryReader(fs ReadStream)

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


Yet are attempting to process it as a series of Chars:
>While brReader.PeekCh ar() -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.ReadBy tes(1)
Loop Until ByteArrary.Leng th = 0

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Anil Gupte" <an*******@icin ema.comwrote in message
news:OI******** ******@TK2MSFTN GP05.phx.gbl...
>Here is my code:

Dim fsReadStream As New FileStream(L3Fi leName, FileMode.Open,
FileAccess.Rea d)
Dim brReader As New BinaryReader(fs ReadStream)
Dim ByteArray() As Byte
While brReader.PeekCh ar() -1
ByteArray = brReader.ReadBy tes(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.Argumen tException' 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*******@icin ema.comwrote in message
news:eR******** *****@TK2MSFTNG P02.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.netw rote in
message news:84******** *************** ***********@mic rosoft.com...
>Anil,
Because you open the file for binary access:
>>Dim brReader As New BinaryReader(fs ReadStream)

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


Yet are attempting to process it as a series of Chars:
>>While brReader.PeekCh ar() -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.ReadBy tes(1)
Loop Until ByteArrary.Leng th = 0

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Anil Gupte" <an*******@icin ema.comwrote in message
news:OI******* *******@TK2MSFT NGP05.phx.gbl.. .
>>Here is my code:

Dim fsReadStream As New FileStream(L3Fi leName, FileMode.Open,
FileAccess.Re ad)
Dim brReader As New BinaryReader(fs ReadStream)
Dim ByteArray() As Byte
While brReader.PeekCh ar() -1
ByteArray = brReader.ReadBy tes(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.Argumen tException' occurred in
mscorlib.dl l
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.netw rote in
message news:1E******** *************** ***********@mic rosoft.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*******@icin ema.comwrote in message
news:eR******** *****@TK2MSFTNG P02.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.netw rote in
message news:84******** *************** ***********@mic rosoft.com...
>>Anil,
Because you open the file for binary access:

Dim brReader As New BinaryReader(fs ReadStream)

And are reading the file for binary access:

ByteArray = brReader.ReadBy tes(1)
Yet are attempting to process it as a series of Chars:

While brReader.PeekCh ar() -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.ReadBy tes(1)
Loop Until ByteArrary.Leng th = 0

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Anil Gupte" <an*******@icin ema.comwrote in message
news:OI****** ********@TK2MSF TNGP05.phx.gbl. ..
Here is my code:

Dim fsReadStream As New FileStream(L3Fi leName, FileMode.Open,
FileAccess.R ead)
Dim brReader As New BinaryReader(fs ReadStream)
Dim ByteArray() As Byte
While brReader.PeekCh ar() -1
ByteArray = brReader.ReadBy tes(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.Argumen tException' occurred in
mscorlib.d ll
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.Enc oding,
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*******@icin ema.comwrote in message
news:Ol******** ******@TK2MSFTN GP06.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.netw rote in
message news:1E******** *************** ***********@mic rosoft.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*******@icin ema.comwrote in message
news:eR******* ******@TK2MSFTN GP02.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.netw rote in
message news:84******** *************** ***********@mic rosoft.com...
Anil,
Because you open the file for binary access:

Dim brReader As New BinaryReader(fs ReadStream)

And are reading the file for binary access:

ByteArray = brReader.ReadBy tes(1)
Yet are attempting to process it as a series of Chars:

While brReader.PeekCh ar() -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.ReadBy tes(1)
Loop Until ByteArrary.Leng th = 0

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Anil Gupte" <an*******@icin ema.comwrote in message
news:OI***** *********@TK2MS FTNGP05.phx.gbl ...
Here is my code:
>
Dim fsReadStream As New FileStream(L3Fi leName, FileMode.Open,
FileAccess. Read)
Dim brReader As New BinaryReader(fs ReadStream)
Dim ByteArray() As Byte
While brReader.PeekCh ar() -1
ByteArray = brReader.ReadBy tes(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.Argumen tException' occurred in
mscorlib.dl l
Additiona l 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
2197
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 updates to a single row. I am running mysql 4.0.14 in WinXP. I am using PHP to do the update. I am thinking something in the update itself is...
11
2748
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 other. On my application startup, I configure the objects usting the RemotingConfiguration class to load the config file. Then I "ping" each of the...
10
2386
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) ComboBox.SelectedValue = db_value; If the db_value was not included in the ComboBox value list the ComboBox.SelectedIndex used to return -1, Now the very same code is
19
7172
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
1884
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 codeveloper on the same project can get a copy of the project from VSS and does not get this exception, ever. I do. We both have the code that causes...
2
1507
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 we've noticed the problems! It seems that from time to time our .net apps just stop responding, even if the server is rebooted when its all back up...
5
7478
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() As Byte = br.ReadBytes(1024) ...
4
1675
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 converted correctly. The application consists of 10 seperate projects. I have hit a problem with converting the core object model project. The...
3
14252
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 unresponsive". The page is quite complex and uses ajax to post updates to the server, updating parts of the page based on the response. It uses YUI...
0
7664
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7918
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7436
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7766
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5341
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4958
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1022
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
715
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.