2 Questions on Reading Binary File; "Conversion buffer overflow"

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

    2 Questions on Reading Binary File; "Conversion buffer overflow"

    Hi All,

    What is the proper way to read a binary file into a byte[]?
    I am using BinaryReader to read from a Stream and call the ReadByte method
    of the BinaryReader object.

    The method I'm using leads to the second question. I got the "Conversion
    buffer overflow" error when I run the following code:
    Stream s = openFileDialog1 .OpenFile();

    System.IO.Binar yReader br = new BinaryReader(s) ;

    byte[] ba = new byte[s.Length]; // s.Length ~= 20000

    while (br.PeekChar() > -1) {

    ba[s.Position] = br.ReadByte(); // <- s.Position ~= 400 when error
    occurred

    }

    Thanks in advance




  • Dave

    #2
    Re: 2 Questions on Reading Binary File; &quot;Conversio n buffer overflow&quot;

    Try the following:

    byte[] bytes = new byte[stream.Length];

    stream.Position = 0;
    stream.Read(byt es, 0, bytes.Length);


    --
    Dave Sexton
    dave@www..jwaon line..com
    -----------------------------------------------------------------------
    "poifull" <poifull76@yaho o.com> wrote in message news:kITke.1282 25$h6.79530@tor nado.texas.rr.c om...[color=blue]
    > Hi All,
    >
    > What is the proper way to read a binary file into a byte[]?
    > I am using BinaryReader to read from a Stream and call the ReadByte method of the BinaryReader object.
    >
    > The method I'm using leads to the second question. I got the "Conversion buffer overflow" error when I run the following code:
    > Stream s = openFileDialog1 .OpenFile();
    >
    > System.IO.Binar yReader br = new BinaryReader(s) ;
    >
    > byte[] ba = new byte[s.Length]; // s.Length ~= 20000
    >
    > while (br.PeekChar() > -1) {
    >
    > ba[s.Position] = br.ReadByte(); // <- s.Position ~= 400 when error occurred
    >
    > }
    >
    > Thanks in advance
    >
    >
    >
    >[/color]


    Comment

    • Ignacio Machin \( .NET/ C# MVP \)

      #3
      Re: 2 Questions on Reading Binary File; &quot;Conversio n buffer overflow&quot;

      Hi,


      Take a look at this code:
      int readed=0;
      byte[] buff = new Byte[2048];
      FileStream fstream = new FileStream( filename, FileMode.Open);

      while( (readed=fstream .Read( buff, 0, 2048))>0 )
      networkstream.W rite( buff, 0, readed);
      fstream.Close() ;


      Cheers,

      --
      Ignacio Machin,
      ignacio.machin AT dot.state.fl.us
      Florida Department Of Transportation


      "poifull" <poifull76@yaho o.com> wrote in message
      news:kITke.1282 25$h6.79530@tor nado.texas.rr.c om...[color=blue]
      > Hi All,
      >
      > What is the proper way to read a binary file into a byte[]?
      > I am using BinaryReader to read from a Stream and call the ReadByte method
      > of the BinaryReader object.
      >
      > The method I'm using leads to the second question. I got the "Conversion
      > buffer overflow" error when I run the following code:
      > Stream s = openFileDialog1 .OpenFile();
      >
      > System.IO.Binar yReader br = new BinaryReader(s) ;
      >
      > byte[] ba = new byte[s.Length]; // s.Length ~= 20000
      >
      > while (br.PeekChar() > -1) {
      >
      > ba[s.Position] = br.ReadByte(); // <- s.Position ~= 400 when
      > error occurred
      >
      > }
      >
      > Thanks in advance
      >
      >
      >
      >[/color]


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: 2 Questions on Reading Binary File; &quot;Conversio n buffer overflow&quot;

        Dave <NOSPAM-dave@dotcomdata solutions.com> wrote:[color=blue]
        > byte[] bytes = new byte[stream.Length];
        >
        > stream.Position = 0;
        > stream.Read(byt es, 0, bytes.Length);[/color]

        As it stands, that's not good code - you should *always* test the
        result of stream.Read, as you may not be able to read everything in one
        chunk.

        See http://www.pobox.com/~skeet/csharp/readbinary.html

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        Working...