Permalink
Browse files

#25 Add raw compress methods that take memory addresses

  • Loading branch information...
1 parent dd0f4d9 commit f0e0e9727e4d727c36ab514fdd6e775bf3f02128 @xerial committed Mar 22, 2013
@@ -321,6 +321,31 @@ public static int maxCompressedLength(int 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
*
* @param data
@@ -547,7 +572,7 @@ public static int uncompressedLength(byte[] input, int offset, int length) throw
/**
* 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())
@@ -566,6 +591,18 @@ public static int uncompressedLength(ByteBuffer compressed) throws IOException {
}
/**
+ * 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
*
* @param input
@@ -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)
{
Oops, something went wrong.
@@ -48,6 +48,9 @@
// ------------------------------------------------------------------------
// 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 native int rawUncompress(Object input, int inputOffset, int inputLength,
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;
@@ -45,6 +45,9 @@
// ------------------------------------------------------------------------
// 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 int rawUncompress(Object input, int inputOffset, int inputLength, Object
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;

0 comments on commit f0e0e97

Please sign in to comment.