Embedding assets with application/octet-stream

Awhile back one of the guys on my team discovered an interesting way to embed files in ActionScript using the “application/octet-stream” mimeType with the Embed metadata tag.

When using the Embed tag it is not common to explicitly assign a specific mimeType to an asset. Because of this Flex uses heuristics to determine the appropriate mimeType based on the extension of the embedded asset (i.e. file).

The Adobe Flex documentation states:

“You can use the [Embed] metadata tag to import JPEG, GIF, PNG, SVG, SWF, TTF, and MP3 files.”

However when specifying application/octet-stream (which is essentially an arbitrary byte stream ) as the assets mimeType it is also possible to embed a file of any type. If the file type is not supported natively, such as XML, developers can write custom parsers which can read the file utilizing the ByteArray (Flex 2) or ByteArrayAsset (Flex 3).

Below I have provided a basic example which demonstrates how the application/octet-stream mimeType can be utilized to embed a file, in this case an external xml document which serves as a config file.

In the above example an XML document is embedded as a Class object from which the contents of the file are read as a string via ByteArrayAsset and converted to an XML object.

Based on this example it would also be possible to create arbitrary custom types in conjunction with custom parsers to allow additional support for numerous other file types in Flex as well.

{ 2 comments to read ... please submit one more! }

  1. I’ve successfully used this technique to embed JavaScript code in a Flex application. The JavaScript is then deployed to the browser via ExternalInterface. It’s useful when your Flex/Flash application depends on some JavaScript to do what it does because it’s one less external dependency.

    Here’s my description of what I did:
    http://blog.iconara.net/2007/12/28/deep-linking-in-flex-using-swfaddress-and-browsermanager/

  2. thats a really kick ass technique… thanks for sharing…

{ 0 Pingbacks/Trackbacks }