Skip to content

Commit

Permalink
Merge pull request #41 from EliseChouleur/jbyte-type
Browse files Browse the repository at this point in the history
Add capability to call java byte array
  • Loading branch information
EliseChouleur authored Oct 30, 2023
2 parents 40b8762 + 1631adc commit 449a4b1
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ You can make a Rust native method raise a Java exception simply by returning a `
| i16 | short |
| String | String |
| Vec\<T\>| ArrayList\<T\> |
| Box<[u8]> | byte[] |
| [jni::JObject<'env>](https://docs.rs/jni/0.17.0/jni/objects/struct.JObject.html)| *(any Java object as input type)* |
| [jni::jobject](https://docs.rs/jni/0.17.0/jni/sys/type.jobject.html) | *(any Java object as output)* |

Expand Down
24 changes: 23 additions & 1 deletion src/convert/safe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

use jni::errors::{Error, Result};
use jni::objects::{JList, JObject, JString, JValue};
use jni::sys::{jboolean, jbooleanArray, jchar, jobject};
use jni::sys::{jboolean, jbooleanArray, jbyteArray, jchar, jobject};
use jni::JNIEnv;

use crate::convert::unchecked::{FromJavaValue, IntoJavaValue};
Expand Down Expand Up @@ -247,6 +247,28 @@ where
}
}

impl Signature for Box<[u8]> {
const SIG_TYPE: &'static str = "[B";
}

impl<'env> TryIntoJavaValue<'env> for Box<[u8]> {
type Target = jbyteArray;

fn try_into(self, env: &JNIEnv<'env>) -> Result<Self::Target> {
env.byte_array_from_slice(self.as_ref())
}
}

impl<'env: 'borrow, 'borrow> TryFromJavaValue<'env, 'borrow> for Box<[u8]> {
type Source = jbyteArray;

fn try_from(s: Self::Source, env: &'borrow JNIEnv<'env>) -> Result<Box<[u8]>> {
let buf = env.convert_byte_array(s)?;
let boxed_slice = buf.into_boxed_slice();
Ok(boxed_slice)
}
}

/// When returning a [`jni::errors::Result`], if the returned variant is `Ok(v)` then the value `v` is returned as usual.
///
/// If the returned value is `Err`, the Java exception specified in the `#[call_type(safe)]` attribute is thrown
Expand Down
8 changes: 8 additions & 0 deletions tests/driver/native/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ pub mod jni {
v
}

pub extern "jni" fn getByteArray(self, v: Box<[u8]>) -> Box<[u8]> {
v
}

pub extern "jni" fn intToString(self, v: i32) -> String {
format!("{}", v)
}
Expand Down Expand Up @@ -131,6 +135,10 @@ pub mod jni {
format!("{:?}", v)
}

pub extern "jni" fn byteArrayToString(self, v: Box<[u8]>) -> String {
format!("{:?}", v)
}

pub extern "java" fn getPassword(
&self,
env: &JNIEnv,
Expand Down
4 changes: 4 additions & 0 deletions tests/driver/src/main/java/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class User {

public native List<String> getStringArray(List<String> x);

public native byte[] getByteArray(byte[] x);

public native String intToString(int x);

public native String boolToString(boolean x);
Expand All @@ -53,6 +55,8 @@ public class User {

public native String stringArrayToString(List<String> x);

public native String byteArrayToString(byte[] x);

private native static void initNative();

public native static String userCountStatus();
Expand Down
12 changes: 12 additions & 0 deletions tests/driver/src/test/java/UserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.function.Function;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;

public class UserTest {
private User u;
Expand Down Expand Up @@ -135,6 +136,12 @@ public void stringArrayTest() {
assertValueRoundTrip(u::getStringArray, u::stringArrayToString, List.of("a", "b", "c"), "[\"a\", \"b\", \"c\"]");
}

@Test
public void byteArrayTest() {
assertArrayValueRoundTrip(u::getByteArray, u::byteArrayToString, new byte[0], "[]");
assertArrayValueRoundTrip(u::getByteArray, u::byteArrayToString, new byte[] {1, 2, 3}, "[1, 2, 3]");
}

@Test
public void staticMethod() {
assertEquals(String.valueOf(User.getTotalUsersCount()), User.userCountStatus());
Expand All @@ -144,4 +151,9 @@ private <T> void assertValueRoundTrip(Function<T, T> func, Function<T, String> t
assertEquals(value, func.apply(value));
assertEquals(text, toString.apply(value));
}

private <T> void assertArrayValueRoundTrip(Function<byte[], byte[]> func, Function<byte[], String> toString, byte[] value, String text) {
assertArrayEquals(value, func.apply(value));
assertEquals(text, toString.apply(value));
}
}

0 comments on commit 449a4b1

Please sign in to comment.