Adobe Alchemy 字节数组读取问题

发布于 2024-10-15 16:30:14 字数 1980 浏览 2 评论 0原文

我正在尝试将从文件中提取的字节数组发送到 C 代码,并从 C 代码再次返回该字节数组。

我正在文本视图内容中打印 C 返回值的内容。文本,但我只能看到字节数组对象“OggS”。我看不到字节数组的内容。谁能说我哪里出错了?..我已经发布了C代码和AS代码。

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代码:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

筱武穆 2024-10-22 16:30:14

我也有点卡在这上面了。我想出的最好方法是将您需要的所有 byteArray 传递到 c 函数中。就我而言,我传递一个输入数据的字节数组,并传递另一个空的 byteArray 供 c 函数将输出数据放入其中。然后,c 函数不需要创建 byteArray (我无法让它工作),并且 c 函数不需要返回值。

AS 代码:

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 代码:

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

但是,无论我尝试什么,这都会泄漏 byteArrays,所以我认为必须有更好的方法。

** 更新

我发现,如果我多次创建新的 CLibInit,它只会泄漏 ByteArray。也就是说,如果使用静态实例并仅在一切正常后才初始化它。

例如,

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文