diff --git a/rust/src/decoder.rs b/rust/src/decoder.rs index abd8b64..7114e04 100644 --- a/rust/src/decoder.rs +++ b/rust/src/decoder.rs @@ -59,8 +59,12 @@ pub unsafe extern "system" fn Java_com_plasmoverse_opus_OpusDecoder_decodeNative match decoder_decode(&mut env, decoder, encoded) { Ok(decoded) => decoded, Err(exception) => { + let result = env.new_short_array(0) + .expect("Couldn't create java short array"); + env.throw_new_exception(exception); - env.new_short_array(0).expect("") // todo: ??? + + result } } } diff --git a/rust/src/encoder.rs b/rust/src/encoder.rs index fdeb7f3..21afabe 100644 --- a/rust/src/encoder.rs +++ b/rust/src/encoder.rs @@ -61,8 +61,12 @@ pub unsafe extern "system" fn Java_com_plasmoverse_opus_OpusEncoder_encodeNative match encoder_encode(&mut env, encoder, samples) { Ok(decoded) => decoded, Err(exception) => { + let result = env.new_byte_array(0) + .expect("Couldn't create java byte array"); + env.throw_new_exception(exception); - env.new_byte_array(0).expect("") // todo: ??? + + result } } } diff --git a/rust/src/util/exception.rs b/rust/src/util/exception.rs index a4a4416..78827b9 100644 --- a/rust/src/util/exception.rs +++ b/rust/src/util/exception.rs @@ -37,6 +37,7 @@ pub trait JavaExceptions { impl<'local> JavaExceptions for JNIEnv<'local> { fn throw_new_exception(&mut self, exception: JavaException) { + println!("{} {}", exception.class, exception.message); let _ = self.throw_new(exception.class, exception.message); } } diff --git a/src/test/java/com/plasmoverse/opus/OpusTest.java b/src/test/java/com/plasmoverse/opus/OpusTest.java index b073009..efaee8b 100644 --- a/src/test/java/com/plasmoverse/opus/OpusTest.java +++ b/src/test/java/com/plasmoverse/opus/OpusTest.java @@ -2,6 +2,8 @@ import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertThrows; + public final class OpusTest { @Test @@ -42,4 +44,30 @@ public void decode() throws Exception { // Closes the decoder, releasing allocated resources decoder.close(); } + + @Test + public void encodeBadFrame() throws Exception { + short[] badFrame = new short[444]; + + OpusEncoder encoder = OpusEncoder.create(48_000, false, 960, OpusMode.VOIP); + + assertThrows(OpusException.class, () -> { + encoder.encode(badFrame); + }); + + encoder.close(); + } + + @Test + public void decodeBadFrame() throws Exception { + byte[] badFrame = new byte[3000]; + + OpusDecoder decoder = OpusDecoder.create(48_000, false, 960); + + assertThrows(OpusException.class, () -> { + decoder.decode(badFrame); + }); + + decoder.close(); + } }