java - FileOutputStream.writeBytes out of bounds -
java - FileOutputStream.writeBytes out of bounds -
my programme reads file byte array , tries carve out bmp image file. problem getting out of bounds error.
{ public static void main( string[] args ) { fileinputstream fileinputstream=null; file file = new file("c:/thumbcache_32.db"); byte[] bfile = new byte[(int) file.length()]; system.out.println("byte array size: " + file.length()); seek { //convert file array of bytes fileinputstream = new fileinputstream(file); fileinputstream.read(bfile); fileinputstream.close(); //convert array of bytes file fileoutputstream fileouputstream = new fileoutputstream("c:/users/zak/desktop/thumb final/carved_image.bmp"); fileouputstream.write(bfile,1573278,1577427); fileouputstream.close(); system.out.println("done"); }catch(exception e){ e.printstacktrace(); } }
}
the size of byte array 1 file loaded "3145728"
and trying re-create bytes "1573278" "1577427". can see these bytes within bounds of byte array im unsure why getting error
output of programme when run
byte array size: 3145728 java.lang.indexoutofboundsexception @ java.io.fileoutputstream.writebytes(native method) @ java.io.fileoutputstream.write(unknown source) @ byte_copy.main(byte_copy.java:28)
fileoutputstream.write takes 3 params, lastly 2 beingness offset , length. pretend have array of size 10:
byte [] arr = new byte[10]; fileoutputstream out = ... out.write(arr, 5, 5); // writes lastly 5 bytes of file, skipping first 5 out.write(arr, 0, 10); // writes bytes of array out.write(arr, 5, 10); // error! index out of bounds, // attempting write 10 bytes starting @ offset 5
now in code utilize fileouputstream.write(bfile,1573278,1577427);
and 1573278+1577427=3150705
, can see 3150705 > 3145728. getting index out of bounds because either offset or limit high. dont know meaning behind why chose 2 numbers this.
fileouputstream.write(bfile, 1573278, bfile.length - 1573278);
java indexoutofboundsexception
Comments
Post a Comment