Adobe Alchemy Byte Array reading problem

884 views Asked by At

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);
}
1

There are 1 answers

0
hooleyhoop On

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:

public function actionscriptFunction() {

    var myInputData:ByteArray = new ByteArray();
    -- maybe load a file or something into myInputData

    var myOutputData:ByteArray = new ByteArray();

    var loader:CLibInit = new CLibInit();
    var lib:Object = loader.init();
    lib.readFile( myInputData, myOutputData );

    -- do something with the output data
}

C Code:

static AS3_Val readFile( void *self, AS3_Val args ) {

    AS3_Val inData_arg = AS3_Undefined();
    AS3_Val outData_arg = AS3_Undefined();
    AS3_ArrayValue( args, "AS3ValType, AS3ValType", &inData_arg, &outData_arg );

    AS3_Val inDataLength = AS3_GetS( inData_arg, "length" );

    -- do something with input data, fill output data array

    // try to stop it leaking    
    // AS3_ReleaseX( wavData_arg ); AS3_ReleaseX( dstData_arg ); AS3_ReleaseX( logFile_arg );
    // AS3_ReleaseX( args );
    return AS3_Null;
}

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,

    static private var _myLibLoader = new CLibInit();
    static private var _myLib = _myLibLoader.init();