jni-rs icon indicating copy to clipboard operation
jni-rs copied to clipboard

Byte arrays in signature

Open dbrgn opened this issue 8 years ago • 2 comments

Hello

I'm following your example. It has a signature like this:

pub extern "system" fn Java_HelloWorld_hello(env: JNIEnv,
                                             class: JClass,
                                             input: JString)
                                             -> jstring {
...

From looking at the signature, it seems that parameter types come from the objects module while the return value is a sys type.

How would I accept a byte array as input value instead of a JString? There's sys::jbyteArray but there doesn't seem to be a matching objects type?

dbrgn avatar Sep 25 '17 12:09 dbrgn

For now, you have to take the sys type and work with the helper methods on the JNIEnv object (i.e. the _byte_array methods).

Ideally, there should be wrappers for the *Array types like everything else, but I haven't had the time or motivation to implement them yet since we haven't been doing anything with arrays here. However, this isn't the first request for some improvement in this aspect of the crate, so I might see what I can do assuming I don't have more pressing things to do.

jrobsonchase avatar Sep 25 '17 14:09 jrobsonchase

Is passing jbyteArray GC-safe? (If so, why do we need JString rather than passing jstring?)

rnapier avatar Nov 21 '21 19:11 rnapier

This will hopefully be addressed soon in https://github.com/jni-rs/jni-rs/pull/400 by adding JObject wrapper types for all the array types instead of having the array APIs all use sys types.

Having all these array APIs based on sys types is pretty unsafe since it makes it easy to end up copying and holding on to local references that may become invalid pointers if copied and held for too long.

Since the wrapper types in #400 are transparent, like JString they would also be usable in native method signatures.

rib avatar Jan 18 '23 01:01 rib