Update issue 10
compress(float[]) and uncompressFloat(byte[])
This commit is contained in:
parent
a1f39c8b5a
commit
b82d1b9999
|
@ -67,6 +67,15 @@ public class Snappy
|
|||
return result;
|
||||
}
|
||||
|
||||
public static byte[] compress(float[] data) {
|
||||
int floatArraySize = data.length * 4;// float use 4 bytes
|
||||
byte[] buf = new byte[Snappy.maxCompressedLength(floatArraySize)];
|
||||
int compressedByteSize = SnappyNative.rawCompressFloat(data, 0, floatArraySize, buf, 0);
|
||||
byte[] result = new byte[compressedByteSize];
|
||||
System.arraycopy(buf, 0, result, 0, compressedByteSize);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* High-level API for uncompressing the input byte array.
|
||||
*
|
||||
|
@ -83,6 +92,15 @@ public class Snappy
|
|||
return result;
|
||||
}
|
||||
|
||||
public static float[] uncompressFloat(byte[] input) throws SnappyException {
|
||||
int uncompressedLength = Snappy.uncompressedLength(input, 0, input.length);
|
||||
float[] result = new float[uncompressedLength / 4];
|
||||
int byteSize = SnappyNative.rawUncompressFloat(input, 0, input.length, result, 0);
|
||||
if (byteSize != uncompressedLength)
|
||||
throw new SnappyException(SnappyErrorCode.INVALID_DECOMPRESSION);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compress the content in the given input buffer. After the compression,
|
||||
* you can retrieve the compressed data from the output buffer [pos() ...
|
||||
|
|
|
@ -54,14 +54,9 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress__Ljava_ni
|
|||
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)
|
||||
jint snappyRawCompress
|
||||
(JNIEnv * env, jclass self, jarray input, jint inputOffset, jint inputLen, jarray output, jint outputOffset)
|
||||
{
|
||||
char* in = (char*) env->GetPrimitiveArrayCritical(input, 0);
|
||||
char* out = (char*) env->GetPrimitiveArrayCritical(output, 0);
|
||||
|
@ -80,6 +75,52 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress___3BII_3B
|
|||
return (jint) compressedLength;
|
||||
}
|
||||
|
||||
jint snappyRawUncompress
|
||||
(JNIEnv * env, jclass self, jarray input, jint inputOffset, jint inputLength, jarray 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_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)
|
||||
{
|
||||
return snappyRawCompress(env, self, input, inputOffset, inputLen, output, outputOffset);
|
||||
}
|
||||
|
||||
|
||||
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompressFloat
|
||||
(JNIEnv * env, jclass self, jfloatArray input, jint inputOffset, jint inputLen, jbyteArray output, jint outputOffset)
|
||||
{
|
||||
return snappyRawCompress(env, self, input, inputOffset, inputLen, output, outputOffset);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Class: org_xerial_snappy_Snappy
|
||||
|
@ -110,27 +151,14 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompress__Ljava_
|
|||
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;
|
||||
}
|
||||
snappyRawUncompress(env, self, input, inputOffset, inputLength, output, outputOffset);
|
||||
}
|
||||
|
||||
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;
|
||||
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompressFloat
|
||||
(JNIEnv * env, jclass self, jbyteArray input, jint inputOffset, jint inputLength, jfloatArray output, jint outputOffset)
|
||||
{
|
||||
snappyRawUncompress(env, self, input, inputOffset, inputLength, output, outputOffset);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -31,6 +31,14 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress__Ljava_ni
|
|||
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress___3BII_3BI
|
||||
(JNIEnv *, jclass, jbyteArray, jint, jint, jbyteArray, jint);
|
||||
|
||||
/*
|
||||
* Class: org_xerial_snappy_SnappyNative
|
||||
* Method: rawCompressFloat
|
||||
* Signature: ([FII[BI)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompressFloat
|
||||
(JNIEnv *, jclass, jfloatArray, jint, jint, jbyteArray, jint);
|
||||
|
||||
/*
|
||||
* Class: org_xerial_snappy_SnappyNative
|
||||
* Method: rawUncompress
|
||||
|
@ -47,6 +55,14 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompress__Ljava_
|
|||
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompress___3BII_3BI
|
||||
(JNIEnv *, jclass, jbyteArray, jint, jint, jbyteArray, jint);
|
||||
|
||||
/*
|
||||
* Class: org_xerial_snappy_SnappyNative
|
||||
* Method: rawUncompressFloat
|
||||
* Signature: ([BII[FI)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompressFloat
|
||||
(JNIEnv *, jclass, jbyteArray, jint, jint, jfloatArray, jint);
|
||||
|
||||
/*
|
||||
* Class: org_xerial_snappy_SnappyNative
|
||||
* Method: maxCompressedLength
|
||||
|
|
|
@ -46,12 +46,18 @@ public class SnappyNative
|
|||
public native static int rawCompress(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset)
|
||||
throws SnappyException;
|
||||
|
||||
public native static int rawCompressFloat(float[] input, int inputOffset, int inputLength, byte[] output,
|
||||
int outputOffset);
|
||||
|
||||
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;
|
||||
|
||||
public native static int rawUncompressFloat(byte[] input, int inputOffset, int inputLength, float[] 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);
|
||||
|
|
Binary file not shown.
|
@ -215,8 +215,11 @@ public class SnappyTest
|
|||
}
|
||||
|
||||
@Test
|
||||
public void loadByDifferentClassLoader() throws Exception {
|
||||
|
||||
public void floatArray() throws Exception {
|
||||
float[] data = new float[] { 1.0f, -0.3f, 1.3f, 234.4f, 34 };
|
||||
byte[] compressed = Snappy.compress(data);
|
||||
float[] result = Snappy.uncompressFloat(compressed);
|
||||
assertArrayEquals(data, result, 0.0f);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue