AS3 fileStream appears to read the file into memory

送分小仙女□ 提交于 2019-12-12 18:29:27

问题


I am writing a process where users will need to select a file that far exceeds their availble RAM and have that file broken up into small chunks (for upload).

I'm able to create a File reference to said file, but when I try to pass it to the fileStream, it appears to try to read the thing into memory before acting on it.

Is there a way to get fileStream to just take the reference to the file and then utilize readBytes the way it's documented?

Here is my code... it's called when the user selects the File in the browser dialogue.

private function selectHandler(event:Event):void {
        var file:File = File(event.target);
        trace("selectHandler: name=" + file.name );

        var stream:FileStream = new FileStream();

        var f:File = event.target as File;
        stream.open(f, FileMode.READ);  //here the process will lock up if the file you pass it is too large.
        var bytes:ByteArray = new ByteArray();
        stream.readBytes(bytes,0,1024);
        trace(bytes);
        stream.close();
}

Much obliged, in advance.


回答1:


So, the solution...

 stream.readAhead = 10000;// some reasonable number
 stream.openAsync(f, FileMode.READ);  //here the process will no longer lock up, if the above chunk is set to a number that Flash can handle.



   //then in your PROGRESS listener you read the bytes into a byteArray
  if(target.availableBytes >= 10000){//you need this because progress gets called many times before the full chunk is read. You only want to use it when you have the full chunk. (you also will want to keep track of the total read and when that total + chunk > fileSize, you'll want to adjust your chunk to read in that last bunch of bytes.
    var bytes:ByteArray = new ByteArray();
    stream.readBytes(bytes,0,event.target.availableBytes); //this will provoke the next segment to get read as well
  }



回答2:


There's an openAsync method in the FileStream class, which, of course, asynchronously sends you parts of the file, but I don't know if it will be useful to you, since you will still have to store the parts in memory for later upload. The alternative I think would be to use NativeProcess to retrieve determined parts of the file, through for example, Java.

I had a similar situation where I needed to retrieve the hash of the first and last 64 bytes of some fairly big files (up to 10gb) and the latter technique (NativeProcess>Java) worked like a charm. I'm a complete n00b in Java, but if you want I can post said code (I copied/pasted snippets until it worked, so it's most likely quite rough).



来源:https://stackoverflow.com/questions/4787805/as3-filestream-appears-to-read-the-file-into-memory

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