diff --git a/native/compile-native.sh b/native/compile-native.sh index 986d5595..f6e6a35a 100755 --- a/native/compile-native.sh +++ b/native/compile-native.sh @@ -1,6 +1,6 @@ #!/bin/sh -CXX="g++ -nostdlib -std=c++11 -shared -fPIC -O3 -Wall -Werror -I$JAVA_HOME/include/ -I$JAVA_HOME/include/linux/" +CXX="g++ -std=c++11 -shared -fPIC -O3 -Wall -Werror -I$JAVA_HOME/include/ -I$JAVA_HOME/include/linux/" $CXX src/main/c/NativeCipherImpl.cpp -o src/main/resources/native-cipher.so -lcrypto $CXX src/main/c/NativeCompressImpl.cpp -o src/main/resources/native-compress.so -lz diff --git a/native/src/main/c/NativeCompressImpl.cpp b/native/src/main/c/NativeCompressImpl.cpp index 0dc668c9..9bda6300 100644 --- a/native/src/main/c/NativeCompressImpl.cpp +++ b/native/src/main/c/NativeCompressImpl.cpp @@ -1,20 +1,9 @@ -#include +#include #include #include "net_md_5_bungee_jni_zlib_NativeCompressImpl.h" -using namespace std; typedef unsigned char byte; -jint throwException(JNIEnv *env, string message) { - jclass exClass = env->FindClass("java/lang/RuntimeException"); - // Can never actually happen on a sane JVM, but better be safe anyway - if (exClass == NULL) { - return -1; - } - - return env->ThrowNew(exClass, message.c_str()); -} - static jfieldID consumedID; static jfieldID finishedID; @@ -24,12 +13,23 @@ void JNICALL Java_net_md_15_bungee_jni_zlib_NativeCompressImpl_initFields(JNIEnv finishedID = env->GetFieldID(clazz, "finished", "Z"); } +jint throwException(JNIEnv *env, const char* message, int err) { + // These can't be static for some unknown reason + jclass exceptionClass = env->FindClass("net/md_5/bungee/jni/NativeCodeException"); + jmethodID exceptionInitID = env->GetMethodID(exceptionClass, "", "(Ljava/lang/String;I)V"); + + jstring jMessage = env->NewStringUTF(message); + + jthrowable throwable = (jthrowable) env->NewObject(exceptionClass, exceptionInitID, jMessage, err); + return env->Throw(throwable); +} + void JNICALL Java_net_md_15_bungee_jni_zlib_NativeCompressImpl_reset(JNIEnv* env, jobject obj, jlong ctx, jboolean compress) { z_stream* stream = (z_stream*) ctx; int ret = (compress) ? deflateReset(stream) : inflateReset(stream); if (ret != Z_OK) { - throwException(env, "Could not reset z_stream: " + to_string(ret)); + throwException(env, "Could not reset z_stream", ret); } } @@ -40,7 +40,7 @@ void JNICALL Java_net_md_15_bungee_jni_zlib_NativeCompressImpl_end(JNIEnv* env, free(stream); if (ret != Z_OK) { - throwException(env, "Could not free z_stream: " + to_string(ret)); + throwException(env, "Could not free z_stream: ", ret); } } @@ -49,7 +49,7 @@ jlong JNICALL Java_net_md_15_bungee_jni_zlib_NativeCompressImpl_init(JNIEnv* env int ret = (compress) ? deflateInit(stream, level) : inflateInit(stream); if (ret != Z_OK) { - throwException(env, "Could not init z_stream: " + to_string(ret)); + throwException(env, "Could not init z_stream", ret); } return (jlong) stream; @@ -73,7 +73,7 @@ jint JNICALL Java_net_md_15_bungee_jni_zlib_NativeCompressImpl_process(JNIEnv* e case Z_OK: break; default: - throwException(env, "Unknown z_stream return code: " + to_string(ret)); + throwException(env, "Unknown z_stream return code", ret); } env->SetIntField(obj, consumedID, inLength - stream->avail_in); diff --git a/native/src/main/java/net/md_5/bungee/jni/NativeCodeException.java b/native/src/main/java/net/md_5/bungee/jni/NativeCodeException.java new file mode 100644 index 00000000..0020c54b --- /dev/null +++ b/native/src/main/java/net/md_5/bungee/jni/NativeCodeException.java @@ -0,0 +1,10 @@ +package net.md_5.bungee.jni; + +public class NativeCodeException extends Exception +{ + + public NativeCodeException(String message, int reason) + { + super( message + " : " + reason ); + } +} diff --git a/native/src/main/resources/native-cipher.so b/native/src/main/resources/native-cipher.so index c954fa73..d50d7bbc 100755 Binary files a/native/src/main/resources/native-cipher.so and b/native/src/main/resources/native-cipher.so differ diff --git a/native/src/main/resources/native-compress.so b/native/src/main/resources/native-compress.so index 5b941648..c41bc583 100755 Binary files a/native/src/main/resources/native-compress.so and b/native/src/main/resources/native-compress.so differ