From c5aeca69f106002c53e3bac4330b836ce4b6aacc Mon Sep 17 00:00:00 2001 From: Matteo Merli Date: Thu, 4 Oct 2018 14:43:24 -0700 Subject: [PATCH] Added option to compress/decompress using memory pointers --- .../net/jpountz/lz4/LZ4HCJNICompressor.java | 4 ++ src/java/net/jpountz/lz4/LZ4JNI.java | 5 +- .../net/jpountz/lz4/LZ4JNICompressor.java | 4 ++ .../jpountz/lz4/LZ4JNIFastDecompressor.java | 4 ++ .../jpountz/lz4/LZ4JNISafeDecompressor.java | 4 ++ src/jni/net_jpountz_lz4_LZ4JNI.c | 71 +++++++++++++++---- 6 files changed, 76 insertions(+), 16 deletions(-) diff --git a/src/java/net/jpountz/lz4/LZ4HCJNICompressor.java b/src/java/net/jpountz/lz4/LZ4HCJNICompressor.java index da721e31..bcc3ae90 100644 --- a/src/java/net/jpountz/lz4/LZ4HCJNICompressor.java +++ b/src/java/net/jpountz/lz4/LZ4HCJNICompressor.java @@ -85,4 +85,8 @@ public int compress(ByteBuffer src, int srcOff, int srcLen, ByteBuffer dest, int return safeInstance.compress(src, srcOff, srcLen, dest, destOff, maxDestLen); } } + + public int compress(long srcAddr, int srcLen, long destAddr, int maxDestLen, int compressionLevel) { + return LZ4JNI.LZ4_compressHC_addr(srcAddr, srcLen, destAddr, maxDestLen, compressionLevel); + } } diff --git a/src/java/net/jpountz/lz4/LZ4JNI.java b/src/java/net/jpountz/lz4/LZ4JNI.java index f083319a..032a6150 100644 --- a/src/java/net/jpountz/lz4/LZ4JNI.java +++ b/src/java/net/jpountz/lz4/LZ4JNI.java @@ -37,5 +37,8 @@ enum LZ4JNI { static native int LZ4_decompress_safe(byte[] srcArray, ByteBuffer srcBuffer, int srcOff, int srcLen, byte[] destArray, ByteBuffer destBuffer, int destOff, int maxDestLen); static native int LZ4_compressBound(int len); + static native int LZ4_compress_addr(long srcAddr, int srcLen, long destAddr, int maxDestLen); + static native int LZ4_compressHC_addr(long srcAddr, int srcLen, long destAddr, int maxDestLen, int compressionLevel); + static native int LZ4_decompress_fast_addr(long srcAddr, long destAddr, int maxDestLen); + static native int LZ4_decompress_safe_addr(long srcAddr, int srcLen, long destAddr, int maxDestLen); } - diff --git a/src/java/net/jpountz/lz4/LZ4JNICompressor.java b/src/java/net/jpountz/lz4/LZ4JNICompressor.java index 18971a33..20fb8da7 100644 --- a/src/java/net/jpountz/lz4/LZ4JNICompressor.java +++ b/src/java/net/jpountz/lz4/LZ4JNICompressor.java @@ -77,4 +77,8 @@ public int compress(ByteBuffer src, int srcOff, int srcLen, ByteBuffer dest, int return safeInstance.compress(src, srcOff, srcLen, dest, destOff, maxDestLen); } } + + public int compress(long srcAddr, int srcLen, long destAddr, int maxDestLen) { + return LZ4JNI.LZ4_compress_addr(srcAddr, srcLen, destAddr, maxDestLen); + } } diff --git a/src/java/net/jpountz/lz4/LZ4JNIFastDecompressor.java b/src/java/net/jpountz/lz4/LZ4JNIFastDecompressor.java index 5c355d85..05df2a03 100644 --- a/src/java/net/jpountz/lz4/LZ4JNIFastDecompressor.java +++ b/src/java/net/jpountz/lz4/LZ4JNIFastDecompressor.java @@ -79,4 +79,8 @@ public int decompress(ByteBuffer src, int srcOff, ByteBuffer dest, int destOff, } } + public int decompress(long srcAddr, long destAddr, int maxDestLen) { + return LZ4JNI.LZ4_decompress_fast_addr(srcAddr, destAddr, maxDestLen); + } + } diff --git a/src/java/net/jpountz/lz4/LZ4JNISafeDecompressor.java b/src/java/net/jpountz/lz4/LZ4JNISafeDecompressor.java index d948c988..6821bbac 100644 --- a/src/java/net/jpountz/lz4/LZ4JNISafeDecompressor.java +++ b/src/java/net/jpountz/lz4/LZ4JNISafeDecompressor.java @@ -78,4 +78,8 @@ public int decompress(ByteBuffer src, int srcOff, int srcLen, ByteBuffer dest, i } } + public int decompress(long srcAddr, long srcLen, long destAddr, int maxDestLen) { + return LZ4JNI.LZ4_decompress_safe_addr(srcAddr, srcLen, destAddr, maxDestLen); + } + } diff --git a/src/jni/net_jpountz_lz4_LZ4JNI.c b/src/jni/net_jpountz_lz4_LZ4JNI.c index 44c30d9f..434cb602 100644 --- a/src/jni/net_jpountz_lz4_LZ4JNI.c +++ b/src/jni/net_jpountz_lz4_LZ4JNI.c @@ -94,7 +94,7 @@ JNIEXPORT jint JNICALL Java_net_jpountz_lz4_LZ4JNI_LZ4_1compressHC char* in; char* out; jint compressed; - + if (srcArray != NULL) { in = (char*) (*env)->GetPrimitiveArrayCritical(env, srcArray, 0); } else { @@ -104,7 +104,7 @@ JNIEXPORT jint JNICALL Java_net_jpountz_lz4_LZ4JNI_LZ4_1compressHC if (in == NULL) { throw_OOM(env); return 0; - } + } if (destArray != NULL) { out = (char*) (*env)->GetPrimitiveArrayCritical(env, destArray, 0); @@ -141,24 +141,24 @@ JNIEXPORT jint JNICALL Java_net_jpountz_lz4_LZ4JNI_LZ4_1decompress_1fast char* in; char* out; jint compressed; - + if (srcArray != NULL) { in = (char*) (*env)->GetPrimitiveArrayCritical(env, srcArray, 0); } else { in = (char*) (*env)->GetDirectBufferAddress(env, srcBuffer); - } - + } + if (in == NULL) { throw_OOM(env); return 0; - } - + } + if (destArray != NULL) { out = (char*) (*env)->GetPrimitiveArrayCritical(env, destArray, 0); } else { out = (char*) (*env)->GetDirectBufferAddress(env, destBuffer); - } - + } + if (out == NULL) { throw_OOM(env); return 0; @@ -177,6 +177,47 @@ JNIEXPORT jint JNICALL Java_net_jpountz_lz4_LZ4JNI_LZ4_1decompress_1fast } +/* + * Class: net_jpountz_lz4_LZ4JNI + * Method: LZ4_compress_addr + * Signature: (JIJI)I + */ +JNIEXPORT jint JNICALL Java_net_jpountz_lz4_LZ4JNI_LZ4_1compress_1addr + (JNIEnv *env, jclass cls, jlong srcAddr, jint srcLen, jlong destAddr, jint maxDestLen) { + return LZ4_compress_default((const char*) srcAddr, (char*) destAddr, srcLen, maxDestLen); +} + +/* + * Class: net_jpountz_lz4_LZ4JNI + * Method: LZ4_compressHC_addr + * Signature: (JIJII)I + */ +JNIEXPORT jint JNICALL Java_net_jpountz_lz4_LZ4JNI_LZ4_1compressHC_1addr + (JNIEnv *env, jclass cls, jlong srcAddr, jint srcLen, jlong destAddr, jint maxDestLen, + int compressionLevel) { + return LZ4_compress_HC((const char*) srcAddr, (char*) destAddr, srcLen, maxDestLen, compressionLevel); +} + +/* + * Class: net_jpountz_lz4_LZ4JNI + * Method: LZ4_decompress_fast_addr + * Signature: (JJI)I + */ +JNIEXPORT jint JNICALL Java_net_jpountz_lz4_LZ4JNI_LZ4_1decompress_1fast_1addr + (JNIEnv *env, jclass cls, jlong srcAddr, jlong destAddr, jint maxDestLen) { + return LZ4_decompress_fast((const char*) srcAddr, (char*) destAddr, maxDestLen); +} + +/* + * Class: net_jpountz_lz4_LZ4JNI + * Method: LZ4_decompress_safe_addr + * Signature: (JIJI)I + */ +JNIEXPORT jint JNICALL Java_net_jpountz_lz4_LZ4JNI_LZ4_1decompress_1safe_1addr + (JNIEnv *env, jclass cls, jlong srcAddr, jint srcLen, jlong destAddr, jint maxDestLen) { + return LZ4_decompress_safe((const char*) srcAddr, (char*) destAddr, srcLen, maxDestLen); +} + /* * Class: net_jpountz_lz4_LZ4JNI * Method: LZ4_decompress_safe @@ -193,19 +234,19 @@ JNIEXPORT jint JNICALL Java_net_jpountz_lz4_LZ4JNI_LZ4_1decompress_1safe in = (char*) (*env)->GetPrimitiveArrayCritical(env, srcArray, 0); } else { in = (char*) (*env)->GetDirectBufferAddress(env, srcBuffer); - } - + } + if (in == NULL) { throw_OOM(env); return 0; - } - + } + if (destArray != NULL) { out = (char*) (*env)->GetPrimitiveArrayCritical(env, destArray, 0); } else { out = (char*) (*env)->GetDirectBufferAddress(env, destBuffer); - } - + } + if (out == NULL) { throw_OOM(env); return 0;