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

江枫思渺然 提交于 2019-12-07 20:18:13

问题


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?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/2921708/how-to-get-a-bytearray-from-file-stream-in-adobe-air

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!