How to identify contents of a byte[] is a JPEG?

Kylar picture Kylar · Dec 29, 2010 · Viewed 40k times · Source

I have a small byte array (under 25K) that I receive and decode as part of a larger message envelope. Sometimes this is an image, furthermore it is a JPG. I have no context information other than the byte array, and need to identify both if it IS an image, and if the image is of type JPG.

Is there some magic number, or magic bytes that exist at the beginning, end or at some offset that I can look at to identify it?

An example of my code looks like this (from memory, not c/p):

byte[] messageBytesAfterDecode = retrieveBytesFromEnvelope();
if(null != messageBytesAfterDecode && messageBytesAfterDecode > 0){
    if(areTheseBytesAJpeg(messageBytesAfterDecode)){
        doSomethingWithAJpeg(messageBytesAfterDecode)
    }else{
        flagEnvelopeAsHavingBadContentInTheField();
    }
}

I really need what would go into the

areTheseBytesAJpeg(byte[] mBytes){}

method, or even a pointer to a spec that details it. I'm hoping there is a very quick way to make this determination, since I don't really want to read them into an Image, etc.

Answer

zsalzbank picture zsalzbank · Dec 29, 2010

From wikipedia:

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

http://en.wikipedia.org/wiki/Magic_number_(programming)