jeudi 16 mars 2017

Actionscript 3, can only part of a file be read without loading the whole file into memory

I am trying to load a 10mb chunk of a 600mb file in an Air desktop app. At first I tried loading the entire file however I got a "system out of memory" error, or the SWF would just quit without error.

I have not moved to reading only part of the file into a byte array, clearing the array and then reading the next part. This works better but it still gives memory issues towards the end of the file.

It is my understanding that the below method should only read the part of the file requested. But I think for some reason the file from the start to end of the chunk I want is actually read. My cut down code looks like this:

var file:File = File.desktopDirectory.resolvePath('input.txt');
var fileStream:FileStream = new FileStream();
fileStream.addEventListener(Event.COMPLETE, onLoaded);
fileStream.openAsync(file, FileMode.READ);  
fileStream.position = _currentPos;

on load:

var fileStream:FileStream = as FileStream;
_fileSize = fileStream.bytesAvailable;
fileStream.readBytes(bytes, 0, 1000000); 

Then I do something with the bytes and call the above code again, this time the position is previous position plus length of the previous read.

When I examine the byte array I can see the size is equal to the start of the file to the end of the read. The byte array contains no data in elements before the start of the read, which is what I expected.

So am I only reading the chunk I want or the entire file?

If I am not can I do so?

How can I solve the memory issue?

Aucun commentaire:

Enregistrer un commentaire