BinaryReader.PeekChar ArgumentException: Conversion Buffer Overflow

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim

    BinaryReader.PeekChar ArgumentException: Conversion Buffer Overflow

    Hi,

    I'm experiencing some problem with the following code:

    st = File.Open(sFile name, FileMode.Open, FileAccess.Read Write)
    br = New BinaryReader(st )

    Do Until br.PeekChar = -1
    Dim buffer() As Byte = br.ReadBytes(10 24)
    ...
    Loop

    Basically, PeekChar would sometimes cause the System.Argument Exception 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


  • Fergus Cooney

    #2
    Re: BinaryReader.Pe ekChar ArgumentExcepti on: Conversion Buffer Overflow

    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


    Comment

    • Jay B. Harlow [MVP - Outlook]

      #3
      Re: BinaryReader.Pe ekChar ArgumentExcepti on: Conversion Buffer Overflow

      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.
      [color=blue]
      > Dim buffer(1024) As Byte[/color]
      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" <timothybug@yah oo.com> wrote in message
      news:%23hdSChri DHA.3660@tk2msf tngp13.phx.gbl. ..[color=blue]
      > Hi,
      >
      > I'm experiencing some problem with the following code:
      >
      > st = File.Open(sFile name, FileMode.Open, FileAccess.Read Write)
      > br = New BinaryReader(st )
      >
      > Do Until br.PeekChar = -1
      > Dim buffer() As Byte = br.ReadBytes(10 24)
      > ...
      > Loop
      >
      > Basically, PeekChar would sometimes cause the System.Argument Exception in
      > mscorlib.dll with the message "Conversion buffer overflow." However, when[/color]
      I[color=blue]
      > 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
      >
      >[/color]


      Comment

      • Tim

        #4
        Re: BinaryReader.Pe ekChar ArgumentExcepti on: Conversion Buffer Overflow

        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" <filter-1@tesco.net> wrote in message
        news:u3DHrzriDH A.2308@TK2MSFTN GP09.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



        Comment

        • Tim

          #5
          Re: BinaryReader.Pe ekChar ArgumentExcepti on: Conversion Buffer Overflow

          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 "Programmin g Microsoft Visual Basic .NET
          Core Reference" by Francesco Balena, in which the following example is used:

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

          Do Until br2.PeekChar = -1
          Console.WriteLi ne(br2.ReadDoub le)
          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


          Comment

          • Jay B. Harlow [MVP - Outlook]

            #6
            Re: BinaryReader.Pe ekChar ArgumentExcepti on: Conversion Buffer Overflow

            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" <timothybug@yah oo.com> wrote in message
            news:O1EKcatiDH A.2232@TK2MSFTN GP09.phx.gbl...[color=blue]
            > Jay,
            >
            > The exceptions are occurring in the middle of the file (almost never at[/color]
            the[color=blue]
            > end), so I'm going to assume that sizeof(char) is not the issue.
            >
            > I guess I was mislead by the book "Programmin g Microsoft Visual Basic .NET
            > Core Reference" by Francesco Balena, in which the following example is[/color]
            used:[color=blue]
            >
            > Dim st2 as Stream = File.Open("c:\v alue.dat", FileMode.Open,
            > FileAccess.Read )
            > Dim br2 as New BinaryReader(st 2)
            >
            > Do Until br2.PeekChar = -1
            > Console.WriteLi ne(br2.ReadDoub le)
            > Loop
            >
            > br2.Close()
            > st2.Close()
            >
            > But then again, since sizeof(double) % sizeof(char) == 0, I guess that's[/color]
            why[color=blue]
            > it works.
            >
            > Thanks a lot for the help. I'll go ahead and use the second version.
            >
            >
            > Tim
            >
            >[/color]


            Comment

            Working...