I have the following struct in C:
typedef struct KFMutableBytes {
uint8_t * _Nullable bytes;
size_t length;
const size_t capacity;
} KFMutableBytes;
This struct is passed as a pointer to functions like so:
KFMutableBytes bytes = ...;
someFunc(&bytes);
The function writes into bytes.bytes up to bytes.capacity, and stores the written length in bytes.length.
So far I've got this:
%typemap(jni) KFMutableBytes * "jbyteArray"
%typemap(jtype) KFMutableBytes * "byte[]"
%typemap(jstype) KFMutableBytes * "byte[]"
%typemap(in) KFMutableBytes * {
KFMutableBytes *bytes = (KFBytes *)malloc(sizeof(KFMutableBytes));
if(bytes == NULL) {
jclass clazz = (*jenv)->FindClass(jenv, "java/lang/OutOfMemoryError");
(*jenv)->ThrowNew(jenv, clazz, "Not enough memory");
return $null;
}
KFMutableBytes b = KFMutableBytesCreate((uint8_t *) JCALL2(GetByteArrayElements, jenv, $input, 0), 0, (size_t) JCALL1(GetArrayLength, jenv, $input));
memcpy(bytes, &b, sizeof(b));
$1 = bytes;
}
%typemap(javain) KFMutableBytes * "$javainput"
/* Prevent default freearg typemap from being used */
%typemap(freearg) KFMutableBytes * {
JCALL3(ReleaseByteArrayElements, jenv, $input, (jbyte *) $1->bytes, 0);
free($1);
}
This means in Java, someFunc is someFunc(byte[] bytes). The problem is that you can't get the written length out, and the byte array length can't be modified in C. So really I just need to map byte[] to the bytes and capacity members, and map the length member to long. But I'm not sure how to map the byte array to struct members?