Home All Groups Group Topic Archive Search About

Why is PeekChar causing a problem?

Author
21 Sep 2006 4:40 AM
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,

Author
21 Sep 2006 7:34 AM
DaveG
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.
Author
21 Sep 2006 12:14 PM
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

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


Show quoteHide quote
"Anil Gupte" <anil-l***@icinema.com> wrote in message
news:OI05MgT3GHA.3428@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
>
Author
23 Sep 2006 5:50 PM
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
Show quoteHide quote
"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@tsbradley.net> wrote in
message news:84455CBB-B754-4FE0-B50B-8F7B17A7F113@microsoft.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" <anil-l***@icinema.com> wrote in message
> news:OI05MgT3GHA.3428@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
>>
>
Author
24 Sep 2006 2:24 PM
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


Show quoteHide quote
"Anil Gupte" <anil-l***@icinema.com> wrote in message
news:eRlQUjz3GHA.988@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]" <Jay_Harlow_***@tsbradley.net> wrote in
> message news:84455CBB-B754-4FE0-B50B-8F7B17A7F113@microsoft.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" <anil-l***@icinema.com> wrote in message
>> news:OI05MgT3GHA.3428@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
>>>
>>
>
>
Author
25 Sep 2006 7:16 PM
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.

Show quoteHide quote
"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@tsbradley.net> wrote in
message news:1ED73D0A-B874-4C0E-9442-5866A03AD6A8@microsoft.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" <anil-l***@icinema.com> wrote in message
> news:eRlQUjz3GHA.988@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]" <Jay_Harlow_***@tsbradley.net> wrote in
>> message news:84455CBB-B754-4FE0-B50B-8F7B17A7F113@microsoft.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" <anil-l***@icinema.com> wrote in message
>>> news:OI05MgT3GHA.3428@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
>>>>
>>>
>>
>>
>
Author
27 Sep 2006 2:40 AM
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


Show quoteHide quote
"Anil Gupte" <anil-l***@icinema.com> wrote in message
news:Ol3yJhN4GHA.2424@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]" <Jay_Harlow_***@tsbradley.net> wrote in
> message news:1ED73D0A-B874-4C0E-9442-5866A03AD6A8@microsoft.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" <anil-l***@icinema.com> wrote in message
>> news:eRlQUjz3GHA.988@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]" <Jay_Harlow_***@tsbradley.net> wrote in
>>> message news:84455CBB-B754-4FE0-B50B-8F7B17A7F113@microsoft.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" <anil-l***@icinema.com> wrote in message
>>>> news:OI05MgT3GHA.3428@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
>>>>>
>>>>
>>>
>>>
>>
>
>