0

I've been making an image rescaler that uses the ImageIO library in Java to convert them to a buffered image. Unfortunately it doesn't recognise every type of JPEG that I may pass to it and so I need to "convert" these other types. The way I'm converting them is to take an existing APP0 tag from a standard JFIF JPEG and what I want to do is on the 3rd byte into the file insert 18 bytes of data (the FFE0 marker and the 16 byte APP0 tag) and then I want to add the rest of the file to the end of that.

So to generalise, what's the most efficient way to add/insert bytes of data mid way through a stream/file?

Thanks in advanced, Alexei Blue.

This question is linked to a previous question of mine and so I'd like to thank onemasse for the answer given there. Java JPEG Converter for Odd Image Types

Community
  • 1
  • 1
Alexei Blue
  • 1,762
  • 3
  • 21
  • 36

4 Answers4

1

If you are reading your images from a stream, you could make a proxy which acts like an inputstream and takes an outputstream. Override the read method so it returns the extra missing bytes when they are missing.

A proxy can be made by extending FilterInputStream http://download.oracle.com/javase/6/docs/api/java/io/FilterInputStream.html

Janoz
  • 953
  • 4
  • 9
0

If it is a file, the recommended way to do this is to copy the existing file to a new one, inserting, changing or removing bytes at the appropriate points. Then rename the new file to the old one.

In theory you could try to use RandomAccessFile (or equivalent) perform an in-place update of an existing file. However, it is a bit tricky, not as efficient as you might imagine and ... most important ... it is unsafe. (If your application or the system dies at an inopportune moment, you are left with a broken file, and no way to recover it.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

A PushbackInputStream might be what you need.

Matthew Cline
  • 2,312
  • 1
  • 19
  • 36
0

Thanks for the suggestion guys, I used a FilterInputStream at first but then saw there was no need to, I used the following piece of code to enter my APP0 Hex tag in:

private static final String APP0Marker = "FF E0 00 10 4A 46 49 46 00 01 01 01 00 8B 00 8B 00 00";

And in the desired converter method:

        if (isJPEG(path))
        {
            fis = new FileInputStream(path);
            bytes = new byte[(int)(new File(path).length())];
            APP0 = hexStringToByteArray(APP0Marker.replaceAll(" ", ""));

            for (int index = 0; index < bytes.length; index++)
            {
                if (index >= 2 && index <= (2 + APP0.length - 1))
                {
                    b = APP0[index-2];
                }
                else
                {
                    b = (byte) fis.read();
                }//if-else

                bytes[index] = b;
            }//for

            //Write new image file
            out = new FileOutputStream(path);
            out.write(bytes);
            out.flush();
        }//if

Hope this helps anyone having the a similar problem :)

Alexei Blue
  • 1,762
  • 3
  • 21
  • 36