Can get_byte_array_region() method have a new variant where I can pass the buff length instead of taking buf.len()? #525
-
Hi, I am currently porting some JNI code from C++ to RUST in an Android app.
This is forcing me to create a buffer array inside my JNI function with the size equals to In my C++ implementation I was using the JNI tips from Android developer docs: Is there a way for me to achieve this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The If you have a buffer and you want to copy into only part of it, take a slice of that part, using slice notation. Something like this: // I assume you have these variables, or something similar, already.
let env: JNIEnv;
let mut local_byte_buffer: Vec<u8>;
let bytesCount: usize;
let byte_buffer: JByteArray;
// Take a slice of the first `bytesCount` bytes of `local_byte_buffer`.
let local_byte_buffer_slice = &mut local_byte_buffer[..bytesCount];
// Now, do the copying.
env.get_byte_array_region(&byte_buffer, 0, local_byte_buffer_slice)?; This will copy into the first The slice doesn't have to be at the beginning of the buffer: // Take a slice that's `bytesCount` bytes long, starting at offset 42.
let local_byte_buffer_slice = &mut local_byte_buffer[42..(42 + bytesCount)];
// Now, do the copying.
env.get_byte_array_region(&byte_buffer, 0, local_byte_buffer_slice)?; For more information about slices in general, see The Rust Programming Language chapter about slices. |
Beta Was this translation helpful? Give feedback.
The
buf
parameter toJNIEnv::get_*_array_region
is a slice reference, a data structure consisting of a pointer and length. That's why there's only the one parameter instead of two.If you have a buffer and you want to copy into only part of it, take a slice of that part, using slice notation. Something like this: