0

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");

    }
}
karthick
  • 11,998
  • 6
  • 56
  • 88
  • In the alchemy code, why are you freeing the buffer before returning it? And why create an AS3_String from it when you want to return a ByteArray? – aaaidan Mar 10 '11 at 22:40

1 Answers1

1

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.

Community
  • 1
  • 1
paleozogt
  • 6,393
  • 11
  • 51
  • 94
  • You'll have to get a lot more specific as to what you tried (perhaps in another SO question). I have a project that's passing ByteArray data via Alchemy's RAM and it works fine. – paleozogt Mar 25 '11 at 17:04
  • I can't, but the links in the SO question I linked to have lots of example code that works. – paleozogt Mar 26 '11 at 15:28