After discovering how to compress / decompress swf files via the Java Deflater / Inflater api, I’ve abandoned the RandomAccessFile method of reading the swf and moved to just using a FileInputStream to create a byte array which can then be examined byte by byte, or, in some cases, bit by bit. The end result is a lightweight Java solution for parsing the header of any swf irregardless of whether the swf is compressed or uncompressed.
Another significant change has been reading the swf size from the header rather than just looking at the number of bytes in the file. This is critical when reading a compressed swf since the header contains information about the number of bytes the file will consume when uncompressed.
As with the previous version you are able extract the following information from the header:
I’m again providing both a jar an java source files:
I’m really interested in tackling reading / writing metadata in the swf format, so if anyone has ideas or suggestions give me a yell.
I found a bug which effects reading the frame rate and frame count. Because the swf width and height are stored in a “packed” format (the number of bytes needed to store the dimensions varies) I couldn’t use an arbitrary count to read frame rate and frame count. Instead the parseHeader method now stores an iterator that keeps track of the next byte that needs to be read. The jar and source files available above have been updated accordingly.
Several users noted a bug where large dimension swf files (width & height) were failing to be read. Anson, noted that the using the >>> operator to deal with right shifts does not work as defined in the Java documentation. Instead, every time a right shift is needed we to mask and do a standard right shift – (someByte & 0xff) >> 3. The jar and source files have been updated in the links above.