Skip to content

Commit

Permalink
Added option to compress/decompress using memory pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
merlimat committed Oct 4, 2018
1 parent d08f929 commit c5aeca6
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 16 deletions.
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, long 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

0 comments on commit c5aeca6

Please sign in to comment.