5

I am having one requirement where i need to identify the type of the image not by the files extension but by validating the header. I am trying to validate a JPEG file by magic number validation.

File imgFile =
   new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Chrysanthemum.jpg");
byte[] ba = Files.toByteArray(imgFile); //Its a google guava library
int i = 0; 
if(ba [i] == 0xFF && ba[i+1] == 0xD8 && ba[i+2] == 0xFF && ba[i+3] == 0xE0){
   System.out.println("isJPEG");
}

But the condition is not getting satisfied at all. ba[i] returns -1.

What am I doing wrong?

Michaël
  • 3,679
  • 7
  • 39
  • 64
karthick
  • 11,998
  • 6
  • 56
  • 88

1 Answers1

3

In Java the byte primitive is signed and has a range from -128 to 127, which means your ba[0] comes out as -1, whereas 0xFF is interpreted as an int as 255. If you do a bitwise AND with 0xFF using the & operator this converts the signed byte to the correct value and your code will work, as follows:

    File imgFile =
           new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Chrysanthemum.jpg");
        byte[] ba = Files.toByteArray(imgFile); //Its a google guava library
        int i = 0; 
        if((ba [i] & 0xFF) == 0xFF && (ba[i+1] & 0xFF) == 0xD8 && (ba[i+2] & 0xFF) == 0xFF 
           && (ba[i+3] & 0xFF) == 0xE0) {
           System.out.println("isJPEG");
        }

I'm not sure about the exact logic you are using however, as wikipedia states that the magic numbers for JPEGs are as follows:

JPEG image files begin with FF D8 and end with FF D9

FF E0 is JFIF specific, so you may want to double check why you are checking for this

The following would be the code to use to make the checks as per Wikipedia:

        File imgFile =                 
                    new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Chrysanthemum.jpg");
        byte[] ba = Files.toByteArray(imgFile); //Its a google guava library
        int i = 0; 
        if((ba [i] & 0xFF) == 0xFF && (ba[i+1] & 0xFF) == 0xD8 && (ba[ba.length - 2] & 0xFF) == 0xFF 
           && (ba[ba.length - 1] & 0xFF) == 0xD9) {
           System.out.println("isJPEG");
        }
Joe Elleson
  • 1,353
  • 10
  • 16
  • Thank you.It worked. I guess i need to look more into ditwise operations. – karthick Mar 21 '13 at 09:29
  • You're welcome. All primitive numeric data types in Java are signed and represented in Two's complement notation, see here: http://en.wikipedia.org/wiki/Two's_complement. You will notice that the unsigned 255 is -1 when represented in Two's complement – Joe Elleson Mar 21 '13 at 09:31