diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2019-05-24 19:45:45 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2019-05-27 10:59:45 +0000 |
| commit | c1a43f630cbf8e71b676fae0c96ae071741a18d8 (patch) | |
| tree | 66de61ca1a08f1d738520ba74789f8c056a1e5b0 | |
| parent | 8b98f6b20fa4cddaba8742f753e549dc75b32fa8 (diff) | |
| download | mullvadvpn-c1a43f630cbf8e71b676fae0c96ae071741a18d8.tar.xz mullvadvpn-c1a43f630cbf8e71b676fae0c96ae071741a18d8.zip | |
Implement `IntoJava` for `&[u8]`
| -rw-r--r-- | mullvad-jni/src/into_java.rs | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/mullvad-jni/src/into_java.rs b/mullvad-jni/src/into_java.rs index 743328dbff..fea18df244 100644 --- a/mullvad-jni/src/into_java.rs +++ b/mullvad-jni/src/into_java.rs @@ -1,7 +1,7 @@ use crate::get_class; use jni::{ objects::{JList, JObject, JString, JValue}, - sys::jint, + sys::{jint, jsize}, JNIEnv, }; use mullvad_types::{ @@ -73,6 +73,24 @@ where } } +impl<'array, 'env> IntoJava<'env> for &'array [u8] { + type JavaType = JObject<'env>; + + fn into_java(self, env: &JNIEnv<'env>) -> Self::JavaType { + let size = self.len(); + let array = env + .new_byte_array(size as jsize) + .expect("Failed to create a Java array of bytes"); + + let data = unsafe { std::slice::from_raw_parts(self.as_ptr() as *const i8, size) }; + + env.set_byte_array_region(array, 0, data) + .expect("Failed to copy bytes to Java array"); + + JObject::from(array) + } +} + impl<'env> IntoJava<'env> for AccountData { type JavaType = JObject<'env>; |
