How to get a bytearray from file stream in Adobe A

2019-05-22 14:32发布

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?

2条回答
倾城 Initia
2楼-- · 2019-05-22 15:09

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

查看更多
干净又极端
3楼-- · 2019-05-22 15:25

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.

查看更多
登录 后发表回答