0

Short version:

I see there is an alchemy method called supplyFile(), but the second arg is a ByteArray... what in the world is that for?!?!

Longer version:

I would like to do the following:

  1. Open a file and assign it to a global variable on the C side of alchemy.

  2. Read in (and process) a chunk of bytes on the C side

  3. Send them to Actionscript for display

  4. Go to step 2 until EOF or notification from Actionscript

  5. Close the file

I'm able to do this right now by opening the file and reading the chunks into a ByteArray on the Actionscript side and then passing it to C for processing and back- but this calls some unnecessary overhead by needing to do AS3_ByteArray_writeBytes()/AS3_ByteArray_readBytes() on the C side in order to manipulate the data in a char *.

Is there any way to simply fopen() a file on the C side and read in some bytes?

Asssume we are talking about a very large file on disk (>4GB).

davidkomer
  • 3,020
  • 2
  • 23
  • 58

1 Answers1

1

From the docs:

supplyFile(path:String, data:ByteArray): This method allows you to provide a file at a specified path for the C code. This is useful if your C code expects a configuration file at a specific location that is not accessible to the runtime due to security restrictions.

In other words, supplyFile lets you create a "virtual file" that can be accessed from C with fopen.

You call supplyFile with a string that represents the path to the virtual file (this can be anything you want it to be) and a ByteArray that is the contents of that virtual file. Then, on the C side you call fopen with the virtual file path and use fread and friends just like an ordinary file.

paleozogt
  • 6,393
  • 11
  • 51
  • 94
  • Oh... so is there no way to fread a real file on disk on the C side? – davidkomer Jul 10 '12 at 15:37
  • Can you read a real file on disk with ActionScript? As the alchemy C code is running within the Flash VM, you can't do anything with C that you can't already do with ActionScript. – paleozogt Jul 10 '12 at 15:39
  • Yes... I can read a file into a ByteArray, but C can't process a ByteArray directly. It needs to then read that ByteArray into a char *buffer. That means the usual C "read into buffer" is turned into a "read into bytearray then read that into buffer." The second read is probably much faster since its in memory, but since this is for something which will repeated often in a loop... there's got to be a better way which avoids two reads? – davidkomer Jul 10 '12 at 16:17
  • Are you using AIR? Anyway, if you have the file in a ByteArray-- you can pass it into C via supplyFile, where C can then use fopen/fread to read it. – paleozogt Jul 10 '12 at 16:26
  • If you're using AIR, you can use funopen to bind fread and friends to AIR's file io stuff: http://stackoverflow.com/a/4368865/89218 – paleozogt Jul 10 '12 at 16:29
  • Thanks- but let me try explaining again: I've already had to do one read (in Actionscript) to get the data from the file into a ByteArray. Is another fread() or AS3_ByteArray_readBytes() *really* necessary just to work with the data on the C side? – davidkomer Jul 10 '12 at 17:41
  • You have to get the data into to Alchemy's virtual RAM somehow. Another possibility is to copy it in directly: http://stackoverflow.com/questions/4891641/how-to-pass-bytearray-to-c-code-in-alchemy – paleozogt Jul 10 '12 at 18:46
  • Check the comments on the first link: "However iterating over a large array like this can be slow. painfully slow… So I returned to AS3_ByteArray_readBytes()" and the second link is closer to what I'm after in example #2, but it relies on Strings-only with StrType, it won't work with passing a ByteArray like that. Thanks though! – davidkomer Jul 10 '12 at 19:48
  • OK, I think there is a solution- the domain memory is actually seen as a ByteArray on the actionscript side. So it's maybe possible to FileStream.readBytes() directly into the domain memory space by allocating the pointer as a char * on the C side, passing that pointer to the actionscript side, and reading it directly into that pointer (an offset of AS3_Ram()) But with Alchemy2 coming out, I'm not going to check this out at the moment to make sure. Thanks! – davidkomer Jul 11 '12 at 09:57