From f0e0e9727e4d727c36ab514fdd6e775bf3f02128 Mon Sep 17 00:00:00 2001 From: "Taro L. Saito" Date: Fri, 22 Mar 2013 15:53:56 +0900 Subject: [PATCH] #25 Add raw compress methods that take memory addresses --- src/main/java/org/xerial/snappy/Snappy.java | 39 +++++++++++++++++- .../java/org/xerial/snappy/SnappyNative.cpp | 41 +++++++++++++++++++ .../java/org/xerial/snappy/SnappyNative.h | 24 +++++++++++ .../java/org/xerial/snappy/SnappyNative.java | 5 +++ .../org/xerial/snappy/SnappyNativeAPI.java | 6 +++ 5 files changed, 114 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/xerial/snappy/Snappy.java b/src/main/java/org/xerial/snappy/Snappy.java index e44ded2..2247498 100755 --- a/src/main/java/org/xerial/snappy/Snappy.java +++ b/src/main/java/org/xerial/snappy/Snappy.java @@ -320,6 +320,31 @@ public class Snappy return ((SnappyNativeAPI) impl).maxCompressedLength(byteSize); } + /** + * Zero-copy compress using memory addresses. + * @param inputAddr input memory address + * @param inputSize input byte size + * @param destAddr destination address of the compressed data + * @return the compressed data size + * @throws IOException + */ + public static long rawCompress(long inputAddr, long inputSize, long destAddr) throws IOException { + return ((SnappyNativeAPI) impl).rawCompress(inputAddr, inputSize, destAddr); + } + + /** + * Zero-copy decompress using memory addresses. + * @param inputAddr input memory address + * @param inputSize input byte size + * @param destAddr destination address of the uncompressed data + * @return the uncompressed data size + * @throws IOException + */ + public static long rawUncompress(long inputAddr, long inputSize, long destAddr) throws IOException { + return ((SnappyNativeAPI) impl).rawUncompress(inputAddr, inputSize, destAddr); + } + + /** * Compress the input data and produce a byte array of the uncompressed data * @@ -547,7 +572,7 @@ public class Snappy /** * Get the uncompressed byte size of the given compressed input. This - * operation taks O(1) time. + * operation takes O(1) time. * * @param compressed * input data [pos() ... limit()) @@ -565,6 +590,18 @@ public class Snappy return ((SnappyNativeAPI) impl).uncompressedLength(compressed, compressed.position(), compressed.remaining()); } + /** + * Get the uncompressed byte size of the given compressed input. This operation takes O(1) time. + * @param inputAddr compressed data address + * @param len byte length of the input + * @return uncompressed byte length of the given input + * @throws IOException when failed to uncompress the given input. The error code is + * {@link SnappyErrorCode#PARSING_ERROR} + */ + public static long uncompressedLength(long inputAddr, long len) throws IOException { + return ((SnappyNativeAPI) impl).uncompressedLength(inputAddr, len); + } + /** * Uncompress the input as a float array * diff --git a/src/main/java/org/xerial/snappy/SnappyNative.cpp b/src/main/java/org/xerial/snappy/SnappyNative.cpp index 1059b36..386b039 100755 --- a/src/main/java/org/xerial/snappy/SnappyNative.cpp +++ b/src/main/java/org/xerial/snappy/SnappyNative.cpp @@ -36,6 +36,32 @@ JNIEXPORT jstring JNICALL Java_org_xerial_snappy_SnappyNative_nativeLibraryVersi return env->NewStringUTF("1.0.4"); } +JNIEXPORT jlong JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress__JJJ + (JNIEnv* env, jobject self, jlong srcAddr, jlong length, jlong destAddr) { + size_t compressedLength; + snappy::RawCompress((char*) srcAddr, (size_t) length, (char*) destAddr, &compressedLength); + return (jlong) compressedLength; + } + + + +JNIEXPORT jlong JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompress__JJJ + (JNIEnv* env, jobject self, jlong srcAddr, jlong length, jlong destAddr) { + + size_t uncompressedLength; + snappy::GetUncompressedLength((char*) srcAddr, (size_t) length, &uncompressedLength); + bool ret = snappy::RawUncompress((char*) srcAddr, (size_t) length, (char*) destAddr); + + if(!ret) { + throw_exception(env, self, 5); + return 0; + } + + return (jlong) uncompressedLength; + } + + + /* * Class: org_xerial_snappy_Snappy * Method: compress @@ -190,6 +216,21 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_uncompressedLength__L return (jint) result; } +JNIEXPORT jlong JNICALL Java_org_xerial_snappy_SnappyNative_uncompressedLength__JJ + (JNIEnv *env, jobject self, jlong inputAddr, jlong len) { + + + size_t result; + bool ret = snappy::GetUncompressedLength((char*) inputAddr, (size_t) len, &result); + if(!ret) { + throw_exception(env, self, 2); + return 0; + } + + return (jint) result; + } + + JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_SnappyNative_isValidCompressedBuffer__Ljava_nio_ByteBuffer_2II (JNIEnv * env, jobject self, jobject compressed, jint cpos, jint clen) { diff --git a/src/main/java/org/xerial/snappy/SnappyNative.h b/src/main/java/org/xerial/snappy/SnappyNative.h index 37452d9..477d30a 100755 --- a/src/main/java/org/xerial/snappy/SnappyNative.h +++ b/src/main/java/org/xerial/snappy/SnappyNative.h @@ -15,6 +15,22 @@ extern "C" { JNIEXPORT jstring JNICALL Java_org_xerial_snappy_SnappyNative_nativeLibraryVersion (JNIEnv *, jobject); +/* + * Class: org_xerial_snappy_SnappyNative + * Method: rawCompress + * Signature: (JJJ)J + */ +JNIEXPORT jlong JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress__JJJ + (JNIEnv *, jobject, jlong, jlong, jlong); + +/* + * Class: org_xerial_snappy_SnappyNative + * Method: rawUncompress + * Signature: (JJJ)J + */ +JNIEXPORT jlong JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompress__JJJ + (JNIEnv *, jobject, jlong, jlong, jlong); + /* * Class: org_xerial_snappy_SnappyNative * Method: rawCompress @@ -71,6 +87,14 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_uncompressedLength__L JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_uncompressedLength__Ljava_lang_Object_2II (JNIEnv *, jobject, jobject, jint, jint); +/* + * Class: org_xerial_snappy_SnappyNative + * Method: uncompressedLength + * Signature: (JJ)J + */ +JNIEXPORT jlong JNICALL Java_org_xerial_snappy_SnappyNative_uncompressedLength__JJ + (JNIEnv *, jobject, jlong, jlong); + /* * Class: org_xerial_snappy_SnappyNative * Method: isValidCompressedBuffer diff --git a/src/main/java/org/xerial/snappy/SnappyNative.java b/src/main/java/org/xerial/snappy/SnappyNative.java index 9b42064..3c9acf5 100755 --- a/src/main/java/org/xerial/snappy/SnappyNative.java +++ b/src/main/java/org/xerial/snappy/SnappyNative.java @@ -48,6 +48,9 @@ public class SnappyNative implements SnappyNativeAPI // ------------------------------------------------------------------------ // Generic compression/decompression routines. // ------------------------------------------------------------------------ + public native long rawCompress(long inputAddr, long inputSize, long destAddr) throws IOException; + public native long rawUncompress(long inputAddr, long inputSize, long destAddr) throws IOException; + public native int rawCompress(ByteBuffer input, int inputOffset, int inputLength, ByteBuffer compressed, int outputOffset) throws IOException; @@ -68,6 +71,8 @@ public class SnappyNative implements SnappyNativeAPI public native int uncompressedLength(Object input, int offset, int len) throws IOException; + public native long uncompressedLength(long inputAddr, long len) throws IOException; + public native boolean isValidCompressedBuffer(ByteBuffer compressed, int offset, int len) throws IOException; public native boolean isValidCompressedBuffer(Object input, int offset, int len) throws IOException; diff --git a/src/main/java/org/xerial/snappy/SnappyNativeAPI.java b/src/main/java/org/xerial/snappy/SnappyNativeAPI.java index 6bf9fcc..902e22e 100755 --- a/src/main/java/org/xerial/snappy/SnappyNativeAPI.java +++ b/src/main/java/org/xerial/snappy/SnappyNativeAPI.java @@ -45,6 +45,9 @@ public interface SnappyNativeAPI // ------------------------------------------------------------------------ // Generic compression/decompression routines. // ------------------------------------------------------------------------ + public long rawCompress(long inputAddr, long inputSize, long destAddr) throws IOException; + public long rawUncompress(long inputAddr, long inputSize, long destAddr) throws IOException; + public int rawCompress(ByteBuffer input, int inputOffset, int inputLength, ByteBuffer compressed, int outputOffset) throws IOException; @@ -65,6 +68,9 @@ public interface SnappyNativeAPI public int uncompressedLength(Object input, int offset, int len) throws IOException; + public long uncompressedLength(long inputAddr, long len) throws IOException; + + public boolean isValidCompressedBuffer(ByteBuffer compressed, int offset, int len) throws IOException; public boolean isValidCompressedBuffer(Object input, int offset, int len) throws IOException;