How to return byte array from alchemy C

1k views Asked by At

I have written a alchemy code for reading the byte array that i have passed from flex.

When i print the value i get the error

cannot convert "OggS" to flash.utils.ByteArray

Alchemy Code

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

    unsigned int e_length;  
    AS3_Val e_data = AS3_Undefined();   
    AS3_ArrayValue( args, "IntType, AS3ValType", &e_length, &e_data );  
    //unsigned char * data = (unsigned char *)malloc(sizeof(unsigned char)  * (e_length +  1));
    char *buffer;
    buffer = (char *)malloc(e_length+ 3);
    AS3_ByteArray_seek(e_data, 0, SEEK_SET);
    AS3_ByteArray_readBytes(buffer, e_data, e_length);
    free(buffer);
    return AS3_String(buffer);

}

Flex 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.length +" ::  " +contents.length);

        var file:File = File.desktopDirectory.resolvePath("Files/test.spx");
        stream = new FileStream();
        stream.open(file, FileMode.WRITE);

    var byteArr:ByteArray;
        var loader:CLibInit = new CLibInit();
        var lib:Object = loader.init();

        var byteStr:String;

    byteArr = lib.readFile( byteData.length, byteData);

    stream.writeBytes(byteArr);
        stream.close();

    }
    catch (ex: ErrorEvent){
        Alert.show("error");

    }
}
1

There are 1 answers

2
paleozogt On

Your problem is that your Alchemy code returns a String, but your Flex code is trying to assign it to a ByteArray. Here's the AS3 equivalent of what you're doing:

var ba:ByteArray= new String();     // bad

If you want to return a ByteArray from Alchemy you need to create it in and fill it with data.

Passing ByteArrays like this is probably inefficient. See this SO question (and the links the answers point to) on how to pass data directly via Alchemy's RAM.