Skip to content

Commit

Permalink
Improve exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
henkelmax committed Mar 23, 2023
1 parent 68cdbf6 commit 7500710
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 21 deletions.
15 changes: 6 additions & 9 deletions rust/src/opus/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use jni::{JNIEnv};
use jni::objects::{JByteArray, JClass, JObject, JShortArray, JValue};
use jni::sys::{jboolean, jint, jlong};
use opus::{Channels, Decoder};
use crate::opus::exceptions::{throw_illegal_argument_exception, throw_illegal_state_exception, throw_io_exception, throw_runtime_exception};

const DEFAULT_FRAME_SIZE: u32 = 960;

Expand All @@ -17,14 +18,14 @@ pub extern "C" fn Java_de_maxhenkel_opus4j_OpusDecoder_createDecoder(mut env: JN
1 => Channels::Mono,
2 => Channels::Stereo,
_ => {
let _ = env.throw(("java/lang/IllegalArgumentException", format!("Invalid number of channels: {}", channels)));
throw_illegal_argument_exception(&mut env, format!("Invalid number of channels: {}", channels));
return 0;
}
};
let decoder = match Decoder::new(sample_rate as u32, channels) {
Ok(decoder) => decoder,
Err(e) => {
throw_runtime_exception(&mut env, format!("Failed to create decoder: {}", e.description()));
throw_io_exception(&mut env, format!("Failed to create decoder: {}", e.description()));
return 0;
}
};
Expand All @@ -38,7 +39,7 @@ pub extern "C" fn Java_de_maxhenkel_opus4j_OpusDecoder_createDecoder(mut env: JN
#[no_mangle]
pub extern "C" fn Java_de_maxhenkel_opus4j_OpusDecoder_setFrameSize(mut env: JNIEnv, obj: JObject, frame_size: jint) {
if frame_size <= 0 {
let _ = env.throw(("java/lang/IllegalArgumentException", format!("Invalid frame size: {}", frame_size)));
throw_illegal_argument_exception(&mut env, format!("Invalid frame size: {}", frame_size));
return;
}

Expand All @@ -57,7 +58,7 @@ pub extern "C" fn Java_de_maxhenkel_opus4j_OpusDecoder_getFrameSize(mut env: JNI
let decoder = match get_decoder(&mut env, &obj) {
Some(encoder) => encoder,
None => {
throw_runtime_exception(&mut env, "Failed to read frame size".to_owned());
throw_runtime_exception(&mut env, "Failed to read frame size");
return 0;
}
};
Expand Down Expand Up @@ -181,7 +182,7 @@ fn get_decoder_from_pointer(pointer: jlong) -> &'static mut DecoderWrapper {
fn get_decoder(env: &mut JNIEnv, obj: &JObject) -> Option<&'static mut DecoderWrapper> {
let pointer = get_pointer(env, obj);
if pointer == 0 {
let _ = env.throw(("java/lang/IllegalStateException", "Decoder is closed"));
throw_illegal_state_exception(env, "Decoder is closed");
return None;
}
return Some(get_decoder_from_pointer(pointer));
Expand All @@ -191,8 +192,4 @@ fn create_pointer(decoder: DecoderWrapper) -> jlong {
let decoder = Box::new(decoder);
let raw = Box::into_raw(decoder);
return raw as jlong;
}

fn throw_runtime_exception(env: &mut JNIEnv, message: String) {
let _ = env.throw(("java/lang/RuntimeException", message));
}
17 changes: 7 additions & 10 deletions rust/src/opus/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use jni::{JNIEnv};
use jni::objects::{JByteArray, JClass, JObject, JShortArray, JValue};
use jni::sys::{jbyte, jint, jlong, jshort};
use opus::{Application, Channels, Encoder};
use crate::opus::exceptions::{throw_illegal_argument_exception, throw_illegal_state_exception, throw_io_exception, throw_runtime_exception};

const DEFAULT_PAYLOAD_SIZE: u32 = 1024;

Expand All @@ -16,23 +17,23 @@ pub extern "C" fn Java_de_maxhenkel_opus4j_OpusEncoder_createEncoder(mut env: JN
1 => Channels::Mono,
2 => Channels::Stereo,
_ => {
let _ = env.throw(("java/lang/IllegalArgumentException", format!("Invalid number of channels: {}", channels)));
throw_illegal_argument_exception(&mut env, format!("Invalid number of channels: {}", channels));
return 0;
}
};

let application = match env.get_field(application, "value", "I") {
Ok(application) => application,
Err(e) => {
throw_runtime_exception(&mut env, format!("Failed to get application: {}", e));
throw_io_exception(&mut env, format!("Failed to get application: {}", e));
return 0;
}
};

let application = match application.i() {
Ok(application) => application,
Err(e) => {
throw_runtime_exception(&mut env, format!("Failed to convert application to int: {}", e));
throw_io_exception(&mut env, format!("Failed to get application: {}", e));
return 0;
}
};
Expand All @@ -46,7 +47,7 @@ pub extern "C" fn Java_de_maxhenkel_opus4j_OpusEncoder_createEncoder(mut env: JN
let encoder = match Encoder::new(sample_rate as u32, channels, application) {
Ok(encoder) => encoder,
Err(e) => {
throw_runtime_exception(&mut env, format!("Failed to create encoder: {}", e.description()));
throw_io_exception(&mut env, format!("Failed to create encoder: {}", e.description()));
return 0;
}
};
Expand All @@ -59,7 +60,7 @@ pub extern "C" fn Java_de_maxhenkel_opus4j_OpusEncoder_createEncoder(mut env: JN
#[no_mangle]
pub extern "C" fn Java_de_maxhenkel_opus4j_OpusEncoder_setMaxPayloadSize(mut env: JNIEnv, obj: JObject, max_payload_size: jint) {
if max_payload_size <= 0 {
let _ = env.throw(("java/lang/IllegalArgumentException", format!("Invalid maximum payload size: {}", max_payload_size)));
throw_illegal_argument_exception(&mut env, format!("Invalid maximum payload size: {}", max_payload_size));
return;
}

Expand Down Expand Up @@ -200,7 +201,7 @@ fn get_encoder_from_pointer(pointer: jlong) -> &'static mut EncoderWrapper {
fn get_encoder(env: &mut JNIEnv, obj: &JObject) -> Option<&'static mut EncoderWrapper> {
let pointer = get_pointer(env, obj);
if pointer == 0 {
let _ = env.throw(("java/lang/IllegalStateException", "Encoder is closed"));
throw_illegal_state_exception(env, "Encoder is closed");
return None;
}
return Some(get_encoder_from_pointer(pointer));
Expand All @@ -210,8 +211,4 @@ fn create_pointer(encoder: EncoderWrapper) -> jlong {
let encoder = Box::new(encoder);
let raw = Box::into_raw(encoder);
return raw as jlong;
}

fn throw_runtime_exception(env: &mut JNIEnv, message: String) {
let _ = env.throw(("java/lang/RuntimeException", message));
}
17 changes: 17 additions & 0 deletions rust/src/opus/exceptions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use jni::JNIEnv;

pub fn throw_runtime_exception<T: AsRef<str>>(env: &mut JNIEnv, message: T) {
let _ = env.throw(("java/lang/RuntimeException", message));
}

pub fn throw_illegal_state_exception<T: AsRef<str>>(env: &mut JNIEnv, message: T) {
let _ = env.throw(("java/lang/IllegalStateException", message));
}

pub fn throw_io_exception<T: AsRef<str>>(env: &mut JNIEnv, message: T) {
let _ = env.throw(("java/io/IOException", message));
}

pub fn throw_illegal_argument_exception<T: AsRef<str>>(env: &mut JNIEnv, message: T) {
let _ = env.throw(("java/lang/IllegalArgumentException", message));
}
1 change: 1 addition & 0 deletions rust/src/opus/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod encoder;
pub mod decoder;
mod exceptions;
2 changes: 1 addition & 1 deletion src/main/java/de/maxhenkel/opus4j/OpusDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public OpusDecoder(int sampleRate, int channels) throws IOException, UnknownPlat
decoder = createDecoder(sampleRate, channels);
}

private static native long createDecoder(int sampleRate, int channels);
private static native long createDecoder(int sampleRate, int channels) throws IOException;

public native void setFrameSize(int frameSize);

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/de/maxhenkel/opus4j/OpusEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public OpusEncoder(int sampleRate, int channels, Application application) throws
encoder = createEncoder(sampleRate, channels, application);
}

private static native long createEncoder(int sampleRate, int channels, Application application);
private static native long createEncoder(int sampleRate, int channels, Application application) throws IOException;

public native void setMaxPayloadSize(int maxPayloadSize);

Expand Down

0 comments on commit 7500710

Please sign in to comment.