Skip to content

Commit f0e0e97

Browse files
committed
xerial#25 Add raw compress methods that take memory addresses
1 parent dd0f4d9 commit f0e0e97

File tree

5 files changed

+114
-1
lines changed

5 files changed

+114
-1
lines changed

src/main/java/org/xerial/snappy/Snappy.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,31 @@ public static int maxCompressedLength(int byteSize) {
320320
return ((SnappyNativeAPI) impl).maxCompressedLength(byteSize);
321321
}
322322

323+
/**
324+
* Zero-copy compress using memory addresses.
325+
* @param inputAddr input memory address
326+
* @param inputSize input byte size
327+
* @param destAddr destination address of the compressed data
328+
* @return the compressed data size
329+
* @throws IOException
330+
*/
331+
public static long rawCompress(long inputAddr, long inputSize, long destAddr) throws IOException {
332+
return ((SnappyNativeAPI) impl).rawCompress(inputAddr, inputSize, destAddr);
333+
}
334+
335+
/**
336+
* Zero-copy decompress using memory addresses.
337+
* @param inputAddr input memory address
338+
* @param inputSize input byte size
339+
* @param destAddr destination address of the uncompressed data
340+
* @return the uncompressed data size
341+
* @throws IOException
342+
*/
343+
public static long rawUncompress(long inputAddr, long inputSize, long destAddr) throws IOException {
344+
return ((SnappyNativeAPI) impl).rawUncompress(inputAddr, inputSize, destAddr);
345+
}
346+
347+
323348
/**
324349
* Compress the input data and produce a byte array of the uncompressed data
325350
*
@@ -547,7 +572,7 @@ public static int uncompressedLength(byte[] input, int offset, int length) throw
547572

548573
/**
549574
* Get the uncompressed byte size of the given compressed input. This
550-
* operation taks O(1) time.
575+
* operation takes O(1) time.
551576
*
552577
* @param compressed
553578
* input data [pos() ... limit())
@@ -565,6 +590,18 @@ public static int uncompressedLength(ByteBuffer compressed) throws IOException {
565590
return ((SnappyNativeAPI) impl).uncompressedLength(compressed, compressed.position(), compressed.remaining());
566591
}
567592

593+
/**
594+
* Get the uncompressed byte size of the given compressed input. This operation takes O(1) time.
595+
* @param inputAddr compressed data address
596+
* @param len byte length of the input
597+
* @return uncompressed byte length of the given input
598+
* @throws IOException when failed to uncompress the given input. The error code is
599+
* {@link SnappyErrorCode#PARSING_ERROR}
600+
*/
601+
public static long uncompressedLength(long inputAddr, long len) throws IOException {
602+
return ((SnappyNativeAPI) impl).uncompressedLength(inputAddr, len);
603+
}
604+
568605
/**
569606
* Uncompress the input as a float array
570607
*

src/main/java/org/xerial/snappy/SnappyNative.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,32 @@ JNIEXPORT jstring JNICALL Java_org_xerial_snappy_SnappyNative_nativeLibraryVersi
3636
return env->NewStringUTF("1.0.4");
3737
}
3838

39+
JNIEXPORT jlong JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress__JJJ
40+
(JNIEnv* env, jobject self, jlong srcAddr, jlong length, jlong destAddr) {
41+
size_t compressedLength;
42+
snappy::RawCompress((char*) srcAddr, (size_t) length, (char*) destAddr, &compressedLength);
43+
return (jlong) compressedLength;
44+
}
45+
46+
47+
48+
JNIEXPORT jlong JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompress__JJJ
49+
(JNIEnv* env, jobject self, jlong srcAddr, jlong length, jlong destAddr) {
50+
51+
size_t uncompressedLength;
52+
snappy::GetUncompressedLength((char*) srcAddr, (size_t) length, &uncompressedLength);
53+
bool ret = snappy::RawUncompress((char*) srcAddr, (size_t) length, (char*) destAddr);
54+
55+
if(!ret) {
56+
throw_exception(env, self, 5);
57+
return 0;
58+
}
59+
60+
return (jlong) uncompressedLength;
61+
}
62+
63+
64+
3965
/*
4066
* Class: org_xerial_snappy_Snappy
4167
* Method: compress
@@ -190,6 +216,21 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_uncompressedLength__L
190216
return (jint) result;
191217
}
192218

219+
JNIEXPORT jlong JNICALL Java_org_xerial_snappy_SnappyNative_uncompressedLength__JJ
220+
(JNIEnv *env, jobject self, jlong inputAddr, jlong len) {
221+
222+
223+
size_t result;
224+
bool ret = snappy::GetUncompressedLength((char*) inputAddr, (size_t) len, &result);
225+
if(!ret) {
226+
throw_exception(env, self, 2);
227+
return 0;
228+
}
229+
230+
return (jint) result;
231+
}
232+
233+
193234
JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_SnappyNative_isValidCompressedBuffer__Ljava_nio_ByteBuffer_2II
194235
(JNIEnv * env, jobject self, jobject compressed, jint cpos, jint clen)
195236
{

src/main/java/org/xerial/snappy/SnappyNative.h

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/org/xerial/snappy/SnappyNative.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ public class SnappyNative implements SnappyNativeAPI
4848
// ------------------------------------------------------------------------
4949
// Generic compression/decompression routines.
5050
// ------------------------------------------------------------------------
51+
public native long rawCompress(long inputAddr, long inputSize, long destAddr) throws IOException;
52+
public native long rawUncompress(long inputAddr, long inputSize, long destAddr) throws IOException;
53+
5154
public native int rawCompress(ByteBuffer input, int inputOffset, int inputLength, ByteBuffer compressed,
5255
int outputOffset) throws IOException;
5356

@@ -68,6 +71,8 @@ public native int rawUncompress(Object input, int inputOffset, int inputLength,
6871

6972
public native int uncompressedLength(Object input, int offset, int len) throws IOException;
7073

74+
public native long uncompressedLength(long inputAddr, long len) throws IOException;
75+
7176
public native boolean isValidCompressedBuffer(ByteBuffer compressed, int offset, int len) throws IOException;
7277

7378
public native boolean isValidCompressedBuffer(Object input, int offset, int len) throws IOException;

src/main/java/org/xerial/snappy/SnappyNativeAPI.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ public interface SnappyNativeAPI
4545
// ------------------------------------------------------------------------
4646
// Generic compression/decompression routines.
4747
// ------------------------------------------------------------------------
48+
public long rawCompress(long inputAddr, long inputSize, long destAddr) throws IOException;
49+
public long rawUncompress(long inputAddr, long inputSize, long destAddr) throws IOException;
50+
4851
public int rawCompress(ByteBuffer input, int inputOffset, int inputLength, ByteBuffer compressed, int outputOffset)
4952
throws IOException;
5053

@@ -65,6 +68,9 @@ public int rawUncompress(Object input, int inputOffset, int inputLength, Object
6568

6669
public int uncompressedLength(Object input, int offset, int len) throws IOException;
6770

71+
public long uncompressedLength(long inputAddr, long len) throws IOException;
72+
73+
6874
public boolean isValidCompressedBuffer(ByteBuffer compressed, int offset, int len) throws IOException;
6975

7076
public boolean isValidCompressedBuffer(Object input, int offset, int len) throws IOException;

0 commit comments

Comments
 (0)