473,385 Members | 1,606 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,385 software developers and data experts.

BinaryReader.PeekChar ArgumentException: Conversion Buffer Overflow

Tim
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)
...
Loop

Basically, PeekChar would sometimes cause the System.ArgumentException in
mscorlib.dll with the message "Conversion buffer overflow." However, when I
examine the value after inserting breakpoints, br.PeekChar returns normal
values without error. Furthermore, the number of bytes read before the
error occurs is always the same, assuming I am using the same file.

This problem disappears after I change the code to the following:

Dim len As Integer = 1

Do Until len = 0
Dim buffer(1024) As Byte
len = br.Read(buffer, 0, 1024)
...
Loop

This makes me believe that the problem _is_ indeed caused by PeekChar.

I've tried searching for people expericing similar problems online, but to
no avail. Anyone has any idea?

Thanks.
Tim
Nov 20 '05 #1
5 7465
Hi Tim,

I've got no idea what the problem is but I'm intrigued by 'Furthermore,
the number of bytes read before the error occurs is always the same, assuming
I am using the same file.' which suggests that there's something in the file
at that point which is causing the error. This makes sense if it's a
conversion error.
The questions must be, therefore:
What is it converting? and
What is it trying to convert it to?

Time to go get some bytes out of that file. And make sure that you have
the right bytes! ;-)

Regards,
Fergus
Nov 20 '05 #2
Tim,
Remember a Char is 2 bytes. Depending on the encoding on the underlying
stream if you have an odd number of bytes in the file I would expect the
error!

I would use a variation of your second loop, as PeekChar is NOT the EOF
function.

Dim len As Integer
Do
Dim buffer(1023) As Byte
len = br.Read(buffer, 0, 1024)
Loop Until len = 0

Although your second loop effectively does the same thing.
Dim buffer(1024) As Byte BTW: You have an array of 1025 bytes, as the 1024 is the high index, the low
index is 0.

Hope this helps
Jay

"Tim" <ti********@yahoo.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl... 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)
...
Loop

Basically, PeekChar would sometimes cause the System.ArgumentException in
mscorlib.dll with the message "Conversion buffer overflow." However, when I examine the value after inserting breakpoints, br.PeekChar returns normal
values without error. Furthermore, the number of bytes read before the
error occurs is always the same, assuming I am using the same file.

This problem disappears after I change the code to the following:

Dim len As Integer = 1

Do Until len = 0
Dim buffer(1024) As Byte
len = br.Read(buffer, 0, 1024)
...
Loop

This makes me believe that the problem _is_ indeed caused by PeekChar.

I've tried searching for people expericing similar problems online, but to
no avail. Anyone has any idea?

Thanks.
Tim

Nov 20 '05 #3
Tim
Fergus,

I've looked into the content of the file at each point where the exception
occurs, and there appears to be absolutely no correlation between the bytes.
I'm not sure if VB is trying to do any kind of underlying conversion, but I
certainly am not :)
Tim

"Fergus Cooney" <fi******@tesco.net> wrote in message
news:u3**************@TK2MSFTNGP09.phx.gbl...
Hi Tim,

I've got no idea what the problem is but I'm intrigued by 'Furthermore,
the number of bytes read before the error occurs is always the same,
assuming
I am using the same file.' which suggests that there's something in the file
at that point which is causing the error. This makes sense if it's a
conversion error.
The questions must be, therefore:
What is it converting? and
What is it trying to convert it to?

Time to go get some bytes out of that file. And make sure that you have
the right bytes! ;-)

Regards,
Fergus

Nov 20 '05 #4
Tim
Jay,

The exceptions are occurring in the middle of the file (almost never at the
end), so I'm going to assume that sizeof(char) is not the issue.

I guess I was mislead by the book "Programming Microsoft Visual Basic .NET
Core Reference" by Francesco Balena, in which the following example is used:

Dim st2 as Stream = File.Open("c:\value.dat", FileMode.Open,
FileAccess.Read)
Dim br2 as New BinaryReader(st2)

Do Until br2.PeekChar = -1
Console.WriteLine(br2.ReadDouble)
Loop

br2.Close()
st2.Close()

But then again, since sizeof(double) % sizeof(char) == 0, I guess that's why
it works.

Thanks a lot for the help. I'll go ahead and use the second version.
Tim
Nov 20 '05 #5
Tim,
And what about Encoding itself?

Even if you are not at the end of the file, I understand that PeekChar needs
to decode the bytes into a Char. Based on the Encoding given (or not given)
when you open the Stream & BinaryReader that Encoding will attempt to
convert one or more bytes into a Char, if you just happen to be at a byte
that 'requires' four bytes to decode, but only have two bytes that make
sense... I would expect an Exception.

Hence I would not use PeekChar to check for EOF.

Remember the default encoding used on BinaryReader is UTF8Encoding.

Hope this helps
Jay

"Tim" <ti********@yahoo.com> wrote in message
news:O1**************@TK2MSFTNGP09.phx.gbl...
Jay,

The exceptions are occurring in the middle of the file (almost never at the end), so I'm going to assume that sizeof(char) is not the issue.

I guess I was mislead by the book "Programming Microsoft Visual Basic .NET
Core Reference" by Francesco Balena, in which the following example is used:
Dim st2 as Stream = File.Open("c:\value.dat", FileMode.Open,
FileAccess.Read)
Dim br2 as New BinaryReader(st2)

Do Until br2.PeekChar = -1
Console.WriteLine(br2.ReadDouble)
Loop

br2.Close()
st2.Close()

But then again, since sizeof(double) % sizeof(char) == 0, I guess that's why it works.

Thanks a lot for the help. I'll go ahead and use the second version.
Tim

Nov 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Supra | last post by:
i saved file in binary. i saved 16 colours.. but how do i read back values into listbox? Dim st2 As Stream = File.Open _ ("C:\Documents and Settings\My Documents\Visual Studio...
23
by: Matt Garman | last post by:
Is there a clean, portable way to determine the maximum value of converted numerical fields with printf()-like functions? Doing this at compile-time would be preferable. For example, %i should...
2
by: Bob Rock | last post by:
I already found an alternative way to accomplish this (using ReadBytes), still I'd like to understand why I'm getting and error reading a text file using the following method. The exception is...
4
by: Paul Steele | last post by:
I am writing a client/server application that communicates over tcp. The code I use to initiate the connection is as follows: NetworkStream networkStream = new NetworkStream(ClientSocket);...
1
by: Vitaly | last post by:
// Open input file and create the BinaryReader. br = new BinaryReader(new FileStream("Test.dat", FileMode.Open, FileAccess.Read)); // Read binary data. d = br.ReadDouble(); A question is...
6
by: Anil Gupte | last post by:
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()...
28
by: James Brown | last post by:
All, I have a series of characters which I need to convert to integer values. Each character is read in turn from a function 'nextch', and hex-digits are identified by the isxdigit function - so...
9
by: Notebooker | last post by:
Hello, I'm an intermediate noob reading-in data from ascii-file using an ifstream object. I have specified a c-style string buffer with size of type size_t and I am specifying to use this...
9
by: Drayshak | last post by:
Hi, I'm currently making an application which reads a file and extracts information from it. Whole files are embedded in this single file, and information about each file that is embedded is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.