index out of bounds exception in fileoutputstream (java)


Results 1 to 5 of 5

Thread: index out of bounds exception in fileoutputstream (java)

  1. #1
    Join Date
    Apr 2003
    Posts
    122

    index out of bounds exception in fileoutputstream (java)

    the following is some code im having some problems with:



    // int bytesread
    // InputStream imagestream
    // and FileOutputStream tempstream

    bytesread = imagestream.read(buffarray);

    while(bytesread!=-1)
    {


    tempstream.write(buffarray,offset,bytesread);
    offset+=bytesread;
    bytesread = imagestream.read(buffarray);



    }

    im using this bit of code to read a .bmp image from a mysql db, it does fine for images under 100k but anything above 100k i get an "index out of bounds exception" thrown on the fileoutput stream. any ideas??
    "Did you ever notice how fast Windows runs? Neither did I."

  2. #2
    Join Date
    Apr 2001
    Posts
    1,496
    Which specific line is giving you problems? And, if you are getting "IndexOutOfBoundsException" I would start checking anything that involves an array -- being trying to access outside an array's length, that exception is thrown. I notice you have a "buffarray" variable. Depending on where that exception is thrown, my guess would be to start looking there.
    Distribution: Gentoo
    Kernel: 2.6.9-r9-gentoo
    Processor: Intel Pentium 4 1.3Ghz
    RAM: 256MB RAMBUS
    HD Space: 300GB (60/80/160)

    <beastmaster> eh yeah same here, used to use mandy lol
    <beastmaster> yeah damn.. that was a waste of 2 years right there lol

  3. #3
    Join Date
    Apr 2003
    Posts
    122
    the exception comes from java.io.fileoutputstream.writebytes(). buffarray is defined as byte[102400].. im not seeing how i could have a indexoutofbounds regarding this array as the iimagestream.read() procedure automatically reads to buffarray.length and returns the # of bytes read in bytesread so then i write the bytes out in tempstream.write at an offset...so from this loop at no time should bytesread > buffarray.length() where buffarray.length() = total # of elements in array....

    as a test i doubled size of bytearray and it wrote the 1st 200 bytes of my file and then crapped out SO this leads me to beleive that there is a problem with the offset parameter im passing to the tempstream.write() function...im thinking the offset should be = to bytesread-1 since the offset is 0 based and bytesread is not...after i wake up fully i shall test this further...
    "Did you ever notice how fast Windows runs? Neither did I."

  4. #4
    Join Date
    Jan 2001
    Location
    Somewhere in middle America
    Posts
    164

    Re: index out of bounds exception in fileoutputstream (java)

    Originally posted by ev8r
    the following is some code im having some problems with:

    // int bytesread
    // InputStream imagestream
    // and FileOutputStream tempstream

    bytesread = imagestream.read(buffarray);

    while(bytesread!=-1)
    {
    tempstream.write(buffarray,offset,bytesread);
    offset+=bytesread;
    bytesread = imagestream.read(buffarray);
    }

    I haven't done any Java in about 3 years, so I may have lost my Java legs.
    Wouldn't read populate indices 0 through bytesread-1 of buffarray each time it is called? If that is the case, shouldn't the write offset always be 0? As I understand write it copies indices offset through offset + bytesread into the tempstream.

    If these are all true, then only the first write to tempstream would write useful data, but you stated that everything works for files under 100k.
    My Machine:
    Maytag SAV5905
    710 rpm Stainless Steel Drum
    Dual boot: Gentoo / Tide

  5. #5
    Join Date
    Apr 2003
    Posts
    122
    yes i just realized that!


    i hate writing code when im sleepy :P
    "Did you ever notice how fast Windows runs? Neither did I."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •