java - FileOutputStream.writeBytes out of bounds -
My program reads a file in a byte array and then tries to create a BMP image from the file. The problem is that I am getting a direct error
{public static zero main (string [] args) {FileInputStream fileInputStream = null; File file = new file ("C: /thumbcache_32.db"); Byte [] bFile = new byte [(int) file. Lamps ()]; System.out.println ("Byte Array Size:" + file.length ()); Try {// Convert file bytes in the file's array InputStream = New FileInputStream (file); FileInputStream.read (bFile); FileInputStream.close (); Convert the bytes to a file fileOutputStream fileOuputStream = New FileOutputStream ("C: / Users / zak / Desktop / thumb final / Carved_image.bmp"); FileOuputStream.write (bFile, 15732781577427); FileOuputStream.close (); Println ("full"); } Hold (exception e) {e.printStackTrace (); }} }
The size of the byte array loaded in the file "3145728"
and I am attempting to copy Bytes "1573278" to "1577427" As you can see that these byte are within the byte array, I'm not sure why I got this error
Outputs while running the program < / Strong> Size of byte array: java.io.fileOutputStream.write (unknown source) bytete_copy on java.io.FileOutputStream.writeBytes (Native Method) at java.lang.IndexOfBoundsException. Main ( Byte_copy.java.28)
takes 3 parameters, the last 2 offsets and length is So we have an array of size 10:
byte [] arr = new byte [10]; FileOutputStream Out = ... out Write (arr, 5, 5); // write the last 5 bytes of the file, except the first 5 out. Write (arr, 0, 10); // writes all the bytes of the array. Write (arr, 5, 10); // Error! Out of index straight threshold, // Your attempt to write 10 bytes starting with your offset 5 Now you have fileOuputStream.write (bFile, 15732781577427) in your code; and 1573278 + 1577427 = 3150705 , as you would 3150705 & gt; can see. 3145728. So you have to take out an index limit because either your offset or your limit is high I do not know the meaning of why you have chosen those 2 numbers, but you can do something like that. fileOuputStream.write (bFile, 1573278, bFile.length - 1573278);
Comments
Post a Comment