How 2 fast determine the graphics format?

I’ve recently had to **fast **determine what is the exact format of the loaded graphic before loading it via Loader class (gif, png, jpg, swf).

After surfing the net, reading about fileformats, parsing the binary data i came to one fast work-arround.

Here’s the class:


     * Class FileFormat

    public class FileFormat 
        public static const SWF:String = "swf";
        public static const PNG:String = "png";
        public static const GIF:String = "gif";
        public static const JPG:String = "jpg";
        private static const _SWF_C:String = "CWS";
        private static const _SWF_U:String = "FWS";
        private static const _GIF:String = "GIF";
        private static const _PNG:String = "PNG";
        private static const _JPG:String = "ÿØÿ";
        public static function getFormat(rawData:String):String
            var fileFormat:String;
            if(rawData.indexOf(FileFormat._SWF_C) == 0 || rawData.indexOf(FileFormat._SWF_U) == 0)
                fileFormat = FileFormat.SWF;
            if(rawData.indexOf(FileFormat._GIF) == 0)
                fileFormat = FileFormat.GIF;
            if(rawData.indexOf(FileFormat._PNG) == 0)
                fileFormat = FileFormat.PNG;
            if(rawData.indexOf(FileFormat._JPG) == 0)
                fileFormat = FileFormat.JPG;
            return fileFormat;

Sample usage:

var l:URLLoader = new URLLoader();
l.dataFormat = URLLoaderDataFormat.BINARY;
l.addEventListener(Event.COMPLETE, graphicsLoaded);
l.load(new URLRequest("test_swf.gif"));

function graphicsLoaded(event:Event):void

I know it looks really buggy, yet it works with all images.
Tested with: gif, progressive gif, animated gif, jpg (different quality), png (png8, png24, png32), swf (compressed, uncompressed, all versions).
All works fine.

Any thoughts?