I am trying to send a bytearray extracted from a file to the C code and return that bytearray again from the C code..
I am printing the contents of the return value from C in a text view contents.text but all i can see is the byte array object "OggS". I can't see the contents of the byte array. Can anyone say where i am going wrong?.. I have posted the C code and the AS code.
AS Code:
private function copyByteArray(content:String):void{
try{
byteData = new ByteArray();
//byteData.writeUTFBytes(contents);
var dec:Base64Decoder = new Base64Decoder();
dec.decode(content);
byteData = dec.toByteArray();
//Alert.show("byte Array " + byteData +" :: " +contents.length);
var file:File = File.desktopDirectory.resolvePath("Files/test.spx");
stream = new FileStream();
stream.open(file, FileMode.WRITE);
/* var loader:CLibInit = new CLibInit();
var lib:Object = loader.init();
loader.supplyFile("testFile.txt", byteData);
contents.text = lib.sqre(5);*/
var loader:CLibInit = new CLibInit();
var lib:Object = loader.init();
// var result:String = lib.doMagic(byteData);
var byteArr:ByteArray;
var byteStr:String;
//byteArr.writeBytes(byteData, 0 , byteData.length);
loader.supplyFile("test1.txt" , byteData);
byteStr = lib.readFile("test1.txt");
contents.text = byteStr;
//stream.writeBytes();
stream.close();
}
catch (ex: ErrorEvent){
Alert.show("error");
}
}
C Code:
static AS3_Val readFile(void* self, AS3_Val args)
{
char * fileName;
FILE * file;
long fileSize;
char * buffer;
AS3_ArrayValue(args, "StrType", &fileName);
file = fopen(fileName,"rb");
//Get file size
fseek (file, 0, SEEK_END);
fileSize = ftell(file);
rewind(file);
//Allocate buffer
buffer = (char*) malloc(sizeof(char)*fileSize);
//Read file into buffer
fread(buffer, 1, fileSize, file);
//close file and free allocated buffer
fclose (file);
free (buffer);
return AS3_String((char*)buffer);
}
I'm a bit stuck on this too. This best i have come up with is to pass all the byteArrays you need into the c function. In my case i pass one byte array of input data and i pass in another empty byteArray for the c function to put output data into. The c function doesn't then need to create a byteArray (i couldn't get it to work) and the c function doesn't need to return a value.
AS Code:
C Code:
However, whatever i try, this leaks the byteArrays, so i think there must be a better way.
** UPDATE
I have found that it only leaks the ByteArrays if i create a new CLibInit more than once. That is, if use a static instance and initialize it only once everything is fine.
eg,