add byte[] based compress/decompress methods

This commit is contained in:
Taro L. Saito 2011-03-31 15:49:51 +09:00
parent 4fbb0db44f
commit 4935319db1
6 changed files with 153 additions and 9 deletions

View File

@ -32,7 +32,7 @@ package org.xerial.snappy;
*/
public enum SnappyErrorCode {
UNKNOWN(0), FAILED_TO_LOAD_NATIVE_LIBRARY(1), PARSING_ERROR(2), NOT_A_DIRECT_BUFFER(3);
UNKNOWN(0), FAILED_TO_LOAD_NATIVE_LIBRARY(1), PARSING_ERROR(2), NOT_A_DIRECT_BUFFER(3), OUT_OF_MEMORY(4);
public final int id;

View File

@ -39,7 +39,7 @@ JNIEXPORT jstring JNICALL Java_org_xerial_snappy_SnappyNative_nativeLibraryVersi
* Method: compress
* Signature: (Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;)J
*/
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress__Ljava_nio_ByteBuffer_2IILjava_nio_ByteBuffer_2I
(JNIEnv* env, jclass self, jobject uncompressed, jint upos, jint ulen, jobject compressed, jint cpos)
{
char* uncompressedBuffer = (char*) env->GetDirectBufferAddress(uncompressed) + upos;
@ -50,12 +50,39 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress
return (jint) compressedLength;
}
/*
* Class: org_xerial_snappy_SnappyNative
* Method: rawCompress
* Signature: ([BII[BI)I
*/
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress___3BII_3BI
(JNIEnv * env, jclass self, jbyteArray input, jint inputOffset, jint inputLen, jbyteArray output, jint outputOffset)
{
char* in = (char*) env->GetPrimitiveArrayCritical(input, 0);
char* out = (char*) env->GetPrimitiveArrayCritical(output, 0);
if(in == 0 || out == 0) {
// out of memory
throw_exception(env, self, 4);
return 0;
}
size_t compressedLength;
snappy::RawCompress(in + inputOffset, (size_t) inputLen, out + outputOffset, &compressedLength);
env->ReleasePrimitiveArrayCritical(input, in, 0);
env->ReleasePrimitiveArrayCritical(output, out, 0);
return (jint) compressedLength;
}
/*
* Class: org_xerial_snappy_Snappy
* Method: uncompress
* Signature: (Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;)Z
*/
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompress
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompress__Ljava_nio_ByteBuffer_2IILjava_nio_ByteBuffer_2I
(JNIEnv * env, jclass self, jobject compressed, jint cpos, jint clen, jobject decompressed, jint dpos)
{
char* compressedBuffer = (char*) env->GetDirectBufferAddress(compressed) + cpos;
@ -72,11 +99,39 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompress
return (jint) decompressedLength;
}
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompress___3BII_3BI
(JNIEnv * env, jclass self, jbyteArray input, jint inputOffset, jint inputLength, jbyteArray output, jint outputOffset)
{
char* in = (char*) env->GetPrimitiveArrayCritical(input, 0);
char* out = (char*) env->GetPrimitiveArrayCritical(output, 0);
if(in == 0 || out == 0) {
// out of memory
throw_exception(env, self, 4);
return 0;
}
size_t uncompressedLength;
snappy::GetUncompressedLength(in + inputOffset, (size_t) inputLength, &uncompressedLength);
bool ret = snappy::RawUncompress(in + inputOffset, (size_t) inputLength, out + outputOffset);
env->ReleasePrimitiveArrayCritical(input, in, 0);
env->ReleasePrimitiveArrayCritical(output, out, 0);
if(!ret) {
throw_exception(env, self, 2);
return 0;
}
return (jint) uncompressedLength;
}
/*
* Class: org_xerial_snappy_Snappy
* Method: maxCompressedLength
* Signature: (J)J
*/
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_maxCompressedLength
(JNIEnv *, jclass, jint size)
{
@ -89,7 +144,7 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_maxCompressedLength
* Method: getUncompressedLength
* Signature: (Ljava/nio/ByteBuffer;)J
*/
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_uncompressedLength
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_uncompressedLength__Ljava_nio_ByteBuffer_2II
(JNIEnv * env, jclass self, jobject compressed, jint cpos, jint clen)
{
char* compressedBuffer = (char*) env->GetDirectBufferAddress(compressed) + cpos;
@ -102,7 +157,24 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_uncompressedLength
return (jint) result;
}
JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_SnappyNative_isValidCompressedBuffer
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_uncompressedLength___3BII
(JNIEnv * env, jclass self, jbyteArray input, jint offset, jint length)
{
char* in = (char*) env->GetPrimitiveArrayCritical(input, 0);
if(in == 0) {
// out of memory
throw_exception(env, self, 4);
return 0;
}
size_t result;
bool ret = snappy::GetUncompressedLength(in + offset, (size_t) length, &result);
env->ReleasePrimitiveArrayCritical(input, in, 0);
return (jint) result;
}
JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_SnappyNative_isValidCompressedBuffer__Ljava_nio_ByteBuffer_2II
(JNIEnv * env, jclass self, jobject compressed, jint cpos, jint clen)
{
char* compressedBuffer = (char*) env->GetDirectBufferAddress(compressed) + cpos;
@ -111,3 +183,17 @@ JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_SnappyNative_isValidCompressed
}
JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_SnappyNative_isValidCompressedBuffer___3BII
(JNIEnv * env, jclass self, jbyteArray input, jint offset, jint length)
{
char* in = (char*) env->GetPrimitiveArrayCritical(input, 0);
if(in == 0) {
// out of memory
throw_exception(env, self, 4);
return 0;
}
bool ret = snappy::IsValidCompressedBuffer(in + offset, (size_t) length);
env->ReleasePrimitiveArrayCritical(input, in, 0);
return ret;
}

View File

@ -20,17 +20,33 @@ JNIEXPORT jstring JNICALL Java_org_xerial_snappy_SnappyNative_nativeLibraryVersi
* Method: rawCompress
* Signature: (Ljava/nio/ByteBuffer;IILjava/nio/ByteBuffer;I)I
*/
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress__Ljava_nio_ByteBuffer_2IILjava_nio_ByteBuffer_2I
(JNIEnv *, jclass, jobject, jint, jint, jobject, jint);
/*
* Class: org_xerial_snappy_SnappyNative
* Method: rawCompress
* Signature: ([BII[BI)I
*/
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress___3BII_3BI
(JNIEnv *, jclass, jbyteArray, jint, jint, jbyteArray, jint);
/*
* Class: org_xerial_snappy_SnappyNative
* Method: rawUncompress
* Signature: (Ljava/nio/ByteBuffer;IILjava/nio/ByteBuffer;I)I
*/
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompress
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompress__Ljava_nio_ByteBuffer_2IILjava_nio_ByteBuffer_2I
(JNIEnv *, jclass, jobject, jint, jint, jobject, jint);
/*
* Class: org_xerial_snappy_SnappyNative
* Method: rawUncompress
* Signature: ([BII[BI)I
*/
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompress___3BII_3BI
(JNIEnv *, jclass, jbyteArray, jint, jint, jbyteArray, jint);
/*
* Class: org_xerial_snappy_SnappyNative
* Method: maxCompressedLength
@ -44,17 +60,33 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_maxCompressedLength
* Method: uncompressedLength
* Signature: (Ljava/nio/ByteBuffer;II)I
*/
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_uncompressedLength
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_uncompressedLength__Ljava_nio_ByteBuffer_2II
(JNIEnv *, jclass, jobject, jint, jint);
/*
* Class: org_xerial_snappy_SnappyNative
* Method: uncompressedLength
* Signature: ([BII)I
*/
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_uncompressedLength___3BII
(JNIEnv *, jclass, jbyteArray, jint, jint);
/*
* Class: org_xerial_snappy_SnappyNative
* Method: isValidCompressedBuffer
* Signature: (Ljava/nio/ByteBuffer;II)Z
*/
JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_SnappyNative_isValidCompressedBuffer
JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_SnappyNative_isValidCompressedBuffer__Ljava_nio_ByteBuffer_2II
(JNIEnv *, jclass, jobject, jint, jint);
/*
* Class: org_xerial_snappy_SnappyNative
* Method: isValidCompressedBuffer
* Signature: ([BII)Z
*/
JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_SnappyNative_isValidCompressedBuffer___3BII
(JNIEnv *, jclass, jbyteArray, jint, jint);
#ifdef __cplusplus
}
#endif

View File

@ -46,9 +46,15 @@ public class SnappyNative
public native static int rawCompress(ByteBuffer input, int inputOffset, int inputLength, ByteBuffer compressed,
int outputOffset);
public native static int rawCompress(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset)
throws SnappyException;
public native static int rawUncompress(ByteBuffer compressed, int inputOffset, int inputLength,
ByteBuffer uncompressed, int outputOffset) throws SnappyException;
public native static int rawUncompress(byte[] input, int inputOffset, int inputLength, byte[] output,
int outputOffset) throws SnappyException;
// Returns the maximal size of the compressed representation of
// input data that is "source_bytes" bytes in length;
public native static int maxCompressedLength(int source_bytes);
@ -56,8 +62,12 @@ public class SnappyNative
// This operation takes O(1) time.
public native static int uncompressedLength(ByteBuffer compressed, int offset, int len) throws SnappyException;
public native static int uncompressedLength(byte[] input, int offset, int len) throws SnappyException;
public native static boolean isValidCompressedBuffer(ByteBuffer compressed, int offset, int len);
public native static boolean isValidCompressedBuffer(byte[] input, int offset, int len);
public static void throw_error(int errorCode) throws SnappyException {
throw new SnappyException(errorCode);
}

View File

@ -145,4 +145,20 @@ public class SnappyTest
assertEquals(m, m2);
}
@Test
public void rawCompress() throws Exception {
String m = "ACCAGGGGGGGGGGGGGGGGGGGGATAGATATTTCCCGAGATATTTTATATAAAAAAA";
byte[] input = m.getBytes();
byte[] output = new byte[Snappy.maxCompressedLength(input.length)];
int compressedSize = SnappyNative.rawCompress(input, 0, input.length, output, 0);
byte[] uncompressed = new byte[input.length];
assertTrue(SnappyNative.isValidCompressedBuffer(output, 0, compressedSize));
int uncompressedSize = SnappyNative.rawUncompress(output, 0, compressedSize, uncompressed, 0);
String m2 = new String(uncompressed);
assertEquals(m, m2);
}
}