Prettify JNI stuff.

This commit is contained in:
Andrew Steinborn
2019-01-11 14:13:13 -05:00
parent 1ddeb85f60
commit 9daa6c3a64
9 changed files with 87 additions and 76 deletions

View File

@@ -1,8 +1,10 @@
#include <assert.h>
#include <jni.h>
#include <stdbool.h>
#include <stdlib.h>
#include <zlib.h>
#include "jni_util.h"
#include "jni_zlib_common.h"
static jfieldID finishedID;
static jfieldID consumedID;
@@ -26,25 +28,23 @@ Java_com_velocitypowered_natives_compression_NativeZlibInflate_init(JNIEnv *env,
return 0;
}
char *msg;
int ret = inflateInit(stream);
switch (ret) {
case Z_OK:
return (jlong) stream;
case Z_MEM_ERROR:
free(stream);
throwException(env, "java/lang/OutOfMemoryError", "zlib init");
return 0;
case Z_STREAM_ERROR:
free(stream);
throwException(env, "java/lang/IllegalArgumentException", "stream clobbered?");
return 0;
default:
msg = stream->msg;
free(stream);
throwException(env, "java/util/zip/DataFormatException", msg);
return 0;
if (ret == Z_OK) {
return (jlong) stream;
} else {
char *zlib_msg = stream->msg;
free(stream);
switch (ret) {
case Z_MEM_ERROR:
throwException(env, "java/lang/OutOfMemoryError", "zlib init");
return 0;
case Z_STREAM_ERROR:
throwException(env, "java/lang/IllegalArgumentException", "stream clobbered?");
return 0;
default:
throwException(env, "java/util/zip/DataFormatException", zlib_msg);
return 0;
}
}
}
@@ -54,24 +54,7 @@ Java_com_velocitypowered_natives_compression_NativeZlibInflate_free(JNIEnv *env,
jlong ctx)
{
z_stream* stream = (z_stream*) ctx;
int ret = inflateEnd(stream);
char *msg = stream->msg;
free((void*) ctx);
switch (ret) {
case Z_OK:
break;
case Z_STREAM_ERROR:
if (msg == NULL) {
msg = "stream state inconsistent";
}
case Z_DATA_ERROR:
if (msg == NULL) {
msg = "data was discarded";
}
throwException(env, "java/lang/IllegalArgumentException", msg);
break;
}
check_zlib_free(env, stream, false);
}
JNIEXPORT int JNICALL