Add Hexidecimal Header Info to JPEG File Using Java

Posted by jboyd on Stack Overflow See other posts from Stack Overflow or by jboyd
Published on 2010-03-15T19:36:28Z Indexed on 2010/03/15 19:39 UTC
Read the original article Hit count: 449

I need to add header info to a JPEG file in order to get it to work properly when shared on some websites, I've tracked down the correct info through a lot of Hex digging, but now I'm kind of stuck trying to get it into the file. I know where in the file it needs to go, and I know how long it is, my problem is that RandomAccessFile just overwrites existing data in the file and FileOutputStream appends the data to the end. I don't want either, I want to INSERT data starting at the third byte.

My example code:

File fileToChange = new File("someimage.jpg");

byte[] i = new byte[2];
i[0] = (byte)Integer.decode("0xcc");
i[1] = (byte)Integer.decode("0xcc");

RandomAccessFile f = 
    new RandomAccessFile(new File("videothing.jpg"), "rw");
long aPositionWhereIWantToGo = 2;
f.seek(aPositionWhereIWantToGo); // this basically reads n bytes in the file
f.write((byte[])i);
f.close();

So this doesn't work because it overwrites, and does not insert, I can't find any way to just insert data into a file

© Stack Overflow or respective owner

Related posts about byte

Related posts about hex