Fixes issue 18

This commit is contained in:
Taro L. Saito 2011-06-20 13:35:13 +09:00
parent 6fcda4dd88
commit dd7c61a4f3
6 changed files with 34 additions and 12 deletions

View File

@ -166,6 +166,16 @@ public class Snappy
return SnappyNative.isValidCompressedBuffer(input, offset, length); return SnappyNative.isValidCompressedBuffer(input, offset, length);
} }
/**
* Returns true iff the contents of compressed buffer [offset,
* offset+length) can be uncompressed successfully. Does not return the
* uncompressed data. Takes time proportional to the input length, but is
* usually at least a factor of four faster than actual decompression.
*/
public static boolean isValidCompressedBuffer(byte[] input) throws SnappyException {
return isValidCompressedBuffer(input, 0, input.length);
}
/** /**
* Returns true iff the contents of compressed buffer [pos() ... limit()) * Returns true iff the contents of compressed buffer [pos() ... limit())
* can be uncompressed successfully. Does not return the uncompressed data. * can be uncompressed successfully. Does not return the uncompressed data.

View File

@ -37,7 +37,8 @@ public enum SnappyErrorCode {
FAILED_TO_LOAD_NATIVE_LIBRARY(1), FAILED_TO_LOAD_NATIVE_LIBRARY(1),
PARSING_ERROR(2), PARSING_ERROR(2),
NOT_A_DIRECT_BUFFER(3), NOT_A_DIRECT_BUFFER(3),
OUT_OF_MEMORY(4); OUT_OF_MEMORY(4),
FAILED_TO_UNCOMPRESS(5);
public final int id; public final int id;

View File

@ -19,19 +19,18 @@
void throw_exception(JNIEnv *env, jclass self, int errorCode) void throw_exception(JNIEnv *env, jclass self, int errorCode)
{ {
jmethodID mth_throwex = 0; jmethodID mth_throwex = env->GetStaticMethodID(self, "throw_error", "(I)V");
if (!mth_throwex) if(mth_throwex == 0)
mth_throwex = env->GetMethodID(self, "throw_error", "(I)V"); return;
env->CallStaticVoidMethod(self, mth_throwex, (jint) errorCode);
env->CallVoidMethod(self, mth_throwex, (jint) errorCode);
} }
JNIEXPORT jstring JNICALL Java_org_xerial_snappy_SnappyNative_nativeLibraryVersion JNIEXPORT jstring JNICALL Java_org_xerial_snappy_SnappyNative_nativeLibraryVersion
(JNIEnv * env, jclass self) (JNIEnv * env, jclass self)
{ {
return env->NewStringUTF("1.0.1"); return env->NewStringUTF("1.0.3");
} }
/* /*
@ -94,7 +93,7 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompress__Ljava_
env->ReleasePrimitiveArrayCritical((jarray) output, out, 0); env->ReleasePrimitiveArrayCritical((jarray) output, out, 0);
if(!ret) { if(!ret) {
throw_exception(env, self, 2); throw_exception(env, self, 5);
return 0; return 0;
} }
@ -121,7 +120,7 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompress__Ljava_
snappy::GetUncompressedLength(compressedBuffer + cpos, (size_t) clen, &decompressedLength); snappy::GetUncompressedLength(compressedBuffer + cpos, (size_t) clen, &decompressedLength);
bool ret = snappy::RawUncompress(compressedBuffer + cpos, (size_t) clen, decompressedBuffer + dpos); bool ret = snappy::RawUncompress(compressedBuffer + cpos, (size_t) clen, decompressedBuffer + dpos);
if(!ret) { if(!ret) {
throw_exception(env, self, 2); throw_exception(env, self, 5);
return 0; return 0;
} }
@ -180,6 +179,11 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_uncompressedLength__L
bool ret = snappy::GetUncompressedLength(in + offset, (size_t) length, &result); bool ret = snappy::GetUncompressedLength(in + offset, (size_t) length, &result);
env->ReleasePrimitiveArrayCritical((jarray) input, in, 0); env->ReleasePrimitiveArrayCritical((jarray) input, in, 0);
if(!ret) {
throw_exception(env, self, 2);
return 0;
}
return (jint) result; return (jint) result;
} }

View File

@ -287,8 +287,15 @@ public class SnappyTest
public void isValidCompressedData() throws Exception { public void isValidCompressedData() throws Exception {
byte[] b = new byte[] { (byte) 91, (byte) 34, (byte) 80, (byte) 73, (byte) 34, (byte) 93 }; byte[] b = new byte[] { (byte) 91, (byte) 34, (byte) 80, (byte) 73, (byte) 34, (byte) 93 };
if (Snappy.isValidCompressedBuffer(b, 0, b.length)) {
assertFalse(Snappy.isValidCompressedBuffer(b));
try {
byte[] uncompressed = Snappy.uncompress(b); byte[] uncompressed = Snappy.uncompress(b);
fail("cannot reach here since the input is invalid data");
}
catch (SnappyException e) {
assertEquals(SnappyErrorCode.FAILED_TO_UNCOMPRESS, e.errorCode);
} }
} }