开发者

Flex embedded string resource encoding

开发者 https://www.devze.com 2023-04-08 15:03 出处:网络
I embed a text file into my Flex project and read its contents using code like this: [Embed(source = \"../../data/abc.txt\", mimeType = \"application/octet-stream\")]

I embed a text file into my Flex project and read its contents using code like this:

[Embed(source = "../../data/abc.txt", mimeType = "application/octet-stream")]
private var r_Abc:Class;

...

var xx:ByteArray = new r_Abc();
var abc:String = xx.toString();

The contents of the file is abc. The problem is t开发者_运维知识库hat the string loaded from the file is not comparable to other strings even though when printed or viewed in the debugger (in FlashDevelop) it seems to be perfectly fine.

trace(abc);  // abc
trace("abc" == abc);   // false

How do I convert it into a proper string? I tried to use the string methods such as substring to create a copy, but that does not seem to be the solution.


Here's my sample:

<?xml version="1.0" encoding="utf-8"?>
<s:Application minWidth="955" minHeight="600"
               creationComplete="creationCompleteHandler(event)"
               xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx">

    <fx:Script>
        <![CDATA[
            import mx.core.ByteArrayAsset;
            import mx.events.FlexEvent;

            // my file is "ABC "
            // strangely enough if I remove the space at the end the string in code is empty
            [Embed(source="data/abc.txt", mimeType="application/octet-stream")]
            private var abcFile:Class;

            protected function creationCompleteHandler(event:FlexEvent):void
            {
                var abcByteArray:ByteArrayAsset = ByteArrayAsset(new abcFile());
                var abc:String = abcByteArray.readUTFBytes(abcByteArray.length);

                trace(abc); // ABC (has a space at the end)
                trace(abc == "ABC "); // true, but notice the space at the end
            }
        ]]>
    </fx:Script>
</s:Application>

My suggestion is to check for trailing spaces, new lines. Also try to place some sort of an EOF character at the end of the file.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号