So I'm reading JPG files, and I've finished reading the 'header' data and now I'm onto the actual image data. Thing is, I don't know the size of the image beforehand, so I cannot create an array to read from. What I could do, though, is read from the end of the 'header' until the end of the image (two bytes: FF and D9), ByteArrayOutputStream
to hold each value as it is read, until I encounter byte D9 after byte FF. How would I go about doing this?
My code so far, including JPG recognition just so you know the context:
// check header data, assign header data to important fields
// Start Of Image (SOI) must be FFD8 and the next marker must be FF
if(!(bData[0] == (byte) 0xFF && bData[1] == (byte) 0xD8
&& this.bData[2] == (byte) 0xFF))
this.isValid = false;
// check if file is not valid
if(!isValid) {
System.err.printf("ERROR: File %s is not"
+ " registered as a bitmap!\n", filename);
Logger.getLogger(Bitmap.class.getName()).log(Level.SEVERE, null, new IllegalArgumentException());
}
// If the next values are correct, then the data stream starts at SOI
// If not, the data stream is raw
this.isRawDataStream = !(bData[3] == (byte) 0xE0
&& bData[6] == (byte) 0x4A
&& bData[7] == (byte) 0x46
&& bData[8] == (byte) 0x49
&& bData[9] == (byte) 0x46
&& bData[10] == (byte) 0x00);
// get size of image
ByteArrayOutputStream iData = new ByteArrayOutputStream();
// start at index 20 of the file (end of 'header')
// read until End of Image
/* while(!(iData at i is FF and iData at i+1 is D9)) {
???
}
*/
edit I'm doing this as an exercise to better understand file formats among other things, and I may be horribly misinterpreting JFIF. If I am, don't hesitate to tell me.