开发者

How to get a bytearray from file stream in Adobe AIR?

开发者 https://www.devze.com 2023-01-01 01:44 出处:网络
I read limited (small - 15 - 500 mb files). I need to be able to put all file bytes into one 开发者_开发知识库single bytearray.

I read limited (small - 15 - 500 mb files). I need to be able to put all file bytes into one 开发者_开发知识库single bytearray. So I have a function:

        [Bindable]
        public var ba:ByteArray = new ByteArray;
        //.... code ....//
        protected function fileOpenSelected(event:Event):void
        {
            currentFile = event.target as File;
            stream = new FileStream();
            stream.openAsync(currentFile, FileMode.READ);
            stream.readBytes(ba);
            stream.close();

                            MyFunction(ba);
        }

But it does not work=( - gives me Error: Error #2030: End of file was encountered.

How to get a full bytearray from stream to use it as normal bytearray?


I finally figured this out, after looking through the documentation for a good while. Whew!

In my case, I was having to read a wav file as a bytesArray for a class I was using, so I could use it on demand at the public scope.

var file:File = File.applicationDirectory.resolvePath("blip.wav");
var fileStream:FileStream = new FileStream(); 
fileStream.open(file, FileMode.READ);
var bytes:ByteArray = new ByteArray
fileStream.readBytes(bytes);
fileStream.close();

Hope this helps you as much as it helped me. I've tested it and confirmed that it works.


isn't the point of a FileStream that you don't have a normal ByteArray, but read asynchronously? It implements IDataInput, allowing you to read from it as long as bytesAvailable is bigger than 0.

on every progress event, you can just readBytes into an output ByteArray and once you get a complete event, you can use it.

greetz
back2dos

0

精彩评论

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