|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Why is PeekChar causing a problem?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 wrote:
> Additional information: Conversion buffer overflow Hi AnilI 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. 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 -- Show quoteHide quoteHope 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 > 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 >> > > I guess, the BinaryReader takes the pointer past the end of file and the I would say the PeekChar takes the pointer past the end of file and then it > Peekhar then chokes. chokes. That the PeekChar is wanting, insisting on, 3 or 4 bytes & they are not there ergo the exception. -- Show quoteHide quoteHope 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 >>> >> > > 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 >>>> >>> >> >> > 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. -- Show quoteHide quoteHope 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: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 >>>>> >>>> >>> >>> >> > >
Slow startup of Deployed Application
How to reset Windows Application's default programmatically? What is the Best Icon Maker? Display Crystal Report passing parms to sql stored procedure Problem Adding new record to a database sqldatareader padding varchar fields with extra white space Image list controls and image resources Data Access Tad bit of combo box newbie help, please! |
|||||||||||||||||||||||