Can't embed assets in a SWF file when content is less than 4 bytes long

401 views Asked by At

I am trying to compile a SWF file, using the mxmlc compiler from Apache Flex SDK 4.10.0. The program simply loads and shows text from a file called asset1.txt.

Here is my code:

package
{
    import flash.events.Event;
    import flash.display.Sprite;
    import flash.text.TextField;
    import mx.core.ByteArrayAsset;

    [SWF(width="20", height="20", backgroundColor="#ff00fa")]
    public class Main extends Sprite
    {
        [Embed("asset1.txt", mimeType="application/octet-stream")]
        private static var asset:Class;

        public function Main()
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void
        {
            var assetBytes:ByteArrayAsset = ByteArrayAsset(new asset());
            var assetString:String = assetBytes.readUTFBytes(assetBytes.length);

            removeEventListener(Event.ADDED_TO_STAGE, init);
            var tf:TextField = new TextField();
            tf.text = assetString;
            addChild(tf);
        }
    }
}

The problem is that when the text contained in asset1.txt is less than 4 bytes long, all bytes from the embedded file are replaced by null bytes, otherwise everything works. For example, the text file is correctly embedded in the SWF when it contains "abcd" or "abcde", but not when it contains "ab" or "abc". Any idea?

1

There are 1 answers

0
Jesus Boadas On

Maybe is a bug of the version of Flex, Im using Flex 4.6 and test your code and is running just fine, try with a different version of the compiler. Hope that helps