Adobe Alchemy 字节数组读取问题
我正在尝试将从文件中提取的字节数组发送到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也有点卡在这上面了。我想出的最好方法是将您需要的所有 byteArray 传递到 c 函数中。就我而言,我传递一个输入数据的字节数组,并传递另一个空的 byteArray 供 c 函数将输出数据放入其中。然后,c 函数不需要创建 byteArray (我无法让它工作),并且 c 函数不需要返回值。
AS 代码:
C 代码:
但是,无论我尝试什么,这都会泄漏 byteArrays,所以我认为必须有更好的方法。
** 更新
我发现,如果我多次创建新的 CLibInit,它只会泄漏 ByteArray。也就是说,如果使用静态实例并仅在一切正常后才初始化它。
例如,
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,