Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to compress/decompress using memory pointers #127

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/java/net/jpountz/lz4/LZ4HCJNICompressor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
5 changes: 4 additions & 1 deletion src/java/net/jpountz/lz4/LZ4JNI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

4 changes: 4 additions & 0 deletions src/java/net/jpountz/lz4/LZ4JNICompressor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
4 changes: 4 additions & 0 deletions src/java/net/jpountz/lz4/LZ4JNIFastDecompressor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
4 changes: 4 additions & 0 deletions src/java/net/jpountz/lz4/LZ4JNISafeDecompressor.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,8 @@ public int decompress(ByteBuffer src, int srcOff, int srcLen, ByteBuffer dest, i
}
}

public int decompress(long srcAddr, int srcLen, long destAddr, int maxDestLen) {
return LZ4JNI.LZ4_decompress_safe_addr(srcAddr, srcLen, destAddr, maxDestLen);
}

}
71 changes: 56 additions & 15 deletions src/jni/net_jpountz_lz4_LZ4JNI.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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;
Expand Down