Skip to content

Can get_byte_array_region() method have a new variant where I can pass the buff length instead of taking buf.len()? #525

Answered by argv-minus-one
ghermaniov asked this question in Q&A
Discussion options

You must be logged in to vote

The buf parameter to JNIEnv::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:

// 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,

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@ghermaniov
Comment options

Answer selected by ghermaniov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants