2

For example we have the following structure:

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

I want to set the data member with a Java byte array (byte[]). I only want set method not get. I've read the getting of a member of type char * to byte array, but I can't do the setting. Can anybody help me?

UPDATED:

I've created some type maps and I have a success:

%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"

Do I have some memory leaks? Have I made it wrong or is there more reliable way?

1
  • You've not got anything calling RelaseByteArrayElements ever. I think you probably want to do something using memcpy to copy the contents. Commented Nov 3, 2012 at 17:28

1 Answer 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.