-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy paths003_reverse_bytes.rs
54 lines (51 loc) · 1.75 KB
/
s003_reverse_bytes.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use jni::objects::{AutoElements, JByteArray, JClass, ReleaseMode};
use jni::sys::jbyteArray;
use jni::JNIEnv;
use log::info;
#[no_mangle]
pub extern "system" fn Java_sample_s003_ReverseBytes_reverseBytes<'local>(
mut env: JNIEnv<'local>,
// This is the class that owns our static method. It's not going to be used,
// but still must be present to match the expected signature of a static
// native method.
_class: JClass<'local>,
input: JByteArray,
) -> jbyteArray {
let rs = env.get_array_length(&input);
let array_size = rs.unwrap();
let rs = unsafe { env.get_array_elements(&input, ReleaseMode::CopyBack) };
if rs.is_err() {
panic!("something bad happened");
}
let array = rs.unwrap();
info!("received [{}]/[{}]", array_size, array.len());
conv2bytes(env, &array, array_size as usize)
}
fn conv2bytes(env: JNIEnv, data: &AutoElements<i8>, len: usize) -> jbyteArray {
info!("convert to [{}]", len);
let mut u8vec: Vec<u8> = Vec::with_capacity(len);
for i in 0..len {
u8vec.push(data[len - i - 1] as u8);
}
return env
.byte_array_from_slice(u8vec.as_slice())
.unwrap()
.into_raw();
}
#[no_mangle]
pub extern "system" fn Java_sample_s003_ReverseBytes_reverseBytes2<'local>(
mut env: JNIEnv<'local>,
// This is the class that owns our static method. It's not going to be used,
// but still must be present to match the expected signature of a static
// native method.
_class: JClass<'local>,
input: JByteArray,
) -> jbyteArray {
let rs = unsafe { env.get_array_elements(&input, ReleaseMode::CopyBack) };
if rs.is_err() {
panic!("something bad happened");
}
let mut array = rs.unwrap();
array.reverse();
input.as_raw()
}