2

例如我们有以下结构:

typedef struct Foo
{
    unsigned char * data;
} Foo;

我想data用 Java 字节数组 ( byte[]) 设置成员。我只想设置方法而不是获取。我已经阅读了将 char * 类型的成员获取到字节数组的内容的内容,但我无法进行设置。有谁能够帮我?

更新

我创建了一些类型映射并且取得了成功:

%typemap(jni) unsigned char * data "jbyteArray"
%typemap(jtype) unsigned char * data "byte[]"
%typemap(jstype) unsigned char * data "byte[]"
%typemap(javaout) unsigned char * data {
    return $jnicall;
}
%typemap(out) signed char * data {
    $result = JCALL1(NewByteArray, jenv, arg1->contentLength);
    JCALL4(SetByteArrayRegion, jenv, $result, 0, arg1->contentLength, $1);
}
%typemap(in) unsigned char *data {
    $1 = (unsigned char *)JCALL2(GetByteArrayElements, jenv, $input, 0);
}
%typemap(javain) unsigned char *data "$javainput"

我有一些内存泄漏吗?我做错了还是有更可靠的方法?

4

1 回答 1

0

Or we could do it with JavaCPP instead. Something like this should work:

public static class Foo extends Pointer {
    static { Loader.load(); }
    public Foo() { allocate(); }
    public Foo(Pointer p) { super(p); }
    private native void allocate();

    public native @MemberSetter void data(@Cast("unsigned char*") BytePointer data);
}

That is IMO much more readable.

于 2012-11-06T10:33:22.157 回答