add SnappyException/SnappyError

This commit is contained in:
Taro L. Saito 2011-03-30 15:26:53 +09:00
parent 07fe11c360
commit 960481b965
11 changed files with 131 additions and 24 deletions

File diff suppressed because one or more lines are too long

View File

@ -140,8 +140,7 @@ public class LoadSnappy
return true; return true;
} }
catch (UnsatisfiedLinkError e) { catch (UnsatisfiedLinkError e) {
System.err.println(e); throw new SnappyError(SnappyErrorCode.FAILED_TO_LOAD_NATIVE_LIBRARY, e);
return false;
} }
} }

View File

@ -11,6 +11,12 @@ package org.xerial.snappy;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
/**
* Snappy API
*
* @author leo
*
*/
public class Snappy public class Snappy
{ {
@ -19,11 +25,13 @@ public class Snappy
} }
/** /**
* Compress the content of the given input, then output the compressed data.
*
* @param uncompressed * @param uncompressed
* input is at buffer[pos() ... limit()) * input is at buffer[pos() ... limit())
* @param compressed * @param compressed
* output compressed data to buffer[pos()..] * output compressed data to buffer[pos()..]
* @return byte size of the compressed data * @return byte size of the compressed data.
*/ */
public static int compress(ByteBuffer uncompressed, ByteBuffer compressed) { public static int compress(ByteBuffer uncompressed, ByteBuffer compressed) {
@ -50,9 +58,10 @@ public class Snappy
* input is at buffer[pos() ... limit()) * input is at buffer[pos() ... limit())
* @param uncompressed * @param uncompressed
* output decompressed data to buffer[pot()) * output decompressed data to buffer[pot())
* @return * @return decompressed data size
*
*/ */
public static boolean uncompress(ByteBuffer compressed, ByteBuffer decompressed) { public static int uncompress(ByteBuffer compressed, ByteBuffer decompressed) throws SnappyException {
if (!compressed.isDirect()) if (!compressed.isDirect())
throw new IllegalArgumentException("input is not a direct buffer"); throw new IllegalArgumentException("input is not a direct buffer");
@ -62,12 +71,13 @@ public class Snappy
int cPos = compressed.position(); int cPos = compressed.position();
int cLen = compressed.remaining(); int cLen = compressed.remaining();
boolean ret = SnappyNative.rawDecompress(compressed, cPos, cLen, decompressed, decompressed.position()); // pos limit
// [ ......UUUUUU.........]
int decompressedSize = SnappyNative
.rawUncompress(compressed, cPos, cLen, decompressed, decompressed.position());
decompressed.limit(decompressed.position() + decompressedSize);
// compressed.limit(compressed.capacity()); return decompressedSize;
// compressed.position(cPos + cLen);
return ret;
} }
/** /**
@ -77,7 +87,7 @@ public class Snappy
* data [pos() ... limit()) * data [pos() ... limit())
* @return * @return
*/ */
public static int getUncompressedLength(ByteBuffer compressed) { public static int uncompressedLength(ByteBuffer compressed) throws SnappyException {
if (!compressed.isDirect()) if (!compressed.isDirect())
throw new IllegalArgumentException("input is not a direct buffer"); throw new IllegalArgumentException("input is not a direct buffer");
@ -91,7 +101,7 @@ public class Snappy
* byte size of the data to compress * byte size of the data to compress
* @return maxmum byte size of the compressed data * @return maxmum byte size of the compressed data
*/ */
public static int getMaxCompressedLength(int byteSize) { public static int maxCompressedLength(int byteSize) {
return SnappyNative.maxCompressedLength(byteSize); return SnappyNative.maxCompressedLength(byteSize);
} }

View File

@ -0,0 +1,46 @@
/*--------------------------------------------------------------------------
* Copyright 2011 Taro L. Saito
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*--------------------------------------------------------------------------*/
//--------------------------------------
// XerialJ
//
// SnappyError.java
// Since: 2011/03/30 15:22:43
//
// $URL$
// $Author$
//--------------------------------------
package org.xerial.snappy;
/**
* Used when serious errors (unchecked exception) in snappy-java are observed.
*
* @author leo
*
*/
public class SnappyError extends Error
{
/**
*
*/
private static final long serialVersionUID = 1L;
public final SnappyErrorCode errorCode;
public SnappyError(SnappyErrorCode code, Error e) {
super(e);
this.errorCode = code;
}
}

View File

@ -32,6 +32,11 @@ package org.xerial.snappy;
*/ */
public enum SnappyErrorCode { public enum SnappyErrorCode {
FAILED_TO_LOAD_NATIVE_LIBRARY, PARSING_ERROR, ; UNKNOWN(0), FAILED_TO_LOAD_NATIVE_LIBRARY(1), PARSING_ERROR(2);
public final int id;
private SnappyErrorCode(int id) {
this.id = id;
}
} }

View File

@ -36,10 +36,30 @@ public class SnappyException extends Exception
public final SnappyErrorCode errorCode; public final SnappyErrorCode errorCode;
public SnappyException(int code) {
SnappyErrorCode[] values = SnappyErrorCode.values();
if (code < 0 || code >= values.length) {
this.errorCode = SnappyErrorCode.UNKNOWN;
}
else {
this.errorCode = values[code];
}
}
public SnappyException(SnappyErrorCode errorCode) { public SnappyException(SnappyErrorCode errorCode) {
this.errorCode = errorCode; this.errorCode = errorCode;
} }
public SnappyException(SnappyErrorCode errorCode, Exception e) {
super(e);
this.errorCode = errorCode;
}
public SnappyException(SnappyErrorCode errorCode, String message) {
super(message);
this.errorCode = errorCode;
}
public SnappyErrorCode getErrorCode() { public SnappyErrorCode getErrorCode() {
return errorCode; return errorCode;
} }

View File

@ -2,6 +2,17 @@
#include <snappy.h> #include <snappy.h>
#include "SnappyNative.h" #include "SnappyNative.h"
void throw_exception(JNIEnv *env, jclass self, int errorCode)
{
jmethodID mth_throwex = 0;
if (!mth_throwex)
mth_throwex = env->GetMethodID(self, "throw_error", "(I)V");
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)
{ {
@ -29,7 +40,7 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress
* Method: uncompress * Method: uncompress
* Signature: (Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;)Z * Signature: (Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;)Z
*/ */
JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_SnappyNative_rawDecompress JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompress
(JNIEnv * env, jclass self, jobject compressed, jint cpos, jint clen, jobject decompressed, jint dpos) (JNIEnv * env, jclass self, jobject compressed, jint cpos, jint clen, jobject decompressed, jint dpos)
{ {
char* compressedBuffer = (char*) env->GetDirectBufferAddress(compressed) + cpos; char* compressedBuffer = (char*) env->GetDirectBufferAddress(compressed) + cpos;
@ -38,8 +49,12 @@ JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_SnappyNative_rawDecompress
size_t decompressedLength; size_t decompressedLength;
snappy::GetUncompressedLength(compressedBuffer, (size_t) clen, &decompressedLength); snappy::GetUncompressedLength(compressedBuffer, (size_t) clen, &decompressedLength);
bool ret = snappy::RawUncompress(compressedBuffer, (size_t) clen, decompressedBuffer); bool ret = snappy::RawUncompress(compressedBuffer, (size_t) clen, decompressedBuffer);
if(!ret) {
throw_exception(env, self, 2);
return 0;
}
return (jboolean) ret; return (jint) decompressedLength;
} }
/* /*
@ -64,7 +79,11 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_getUncompressedLength
{ {
char* compressedBuffer = (char*) env->GetDirectBufferAddress(compressed) + cpos; char* compressedBuffer = (char*) env->GetDirectBufferAddress(compressed) + cpos;
size_t result; size_t result;
snappy::GetUncompressedLength(compressedBuffer, (size_t) clen, &result); bool ret = snappy::GetUncompressedLength(compressedBuffer, (size_t) clen, &result);
if(!ret) {
throw_exception(env, self, 2);
return 0;
}
return (jint) result; return (jint) result;
} }

View File

@ -25,10 +25,10 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress
/* /*
* Class: org_xerial_snappy_SnappyNative * Class: org_xerial_snappy_SnappyNative
* Method: rawDecompress * Method: rawUncompress
* Signature: (Ljava/nio/ByteBuffer;IILjava/nio/ByteBuffer;I)Z * Signature: (Ljava/nio/ByteBuffer;IILjava/nio/ByteBuffer;I)I
*/ */
JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_SnappyNative_rawDecompress JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompress
(JNIEnv *, jclass, jobject, jint, jint, jobject, jint); (JNIEnv *, jclass, jobject, jint, jint, jobject, jint);
/* /*

View File

@ -25,14 +25,18 @@ public class SnappyNative
public native static int rawCompress(ByteBuffer input, int inputOffset, int inputLength, ByteBuffer compressed, public native static int rawCompress(ByteBuffer input, int inputOffset, int inputLength, ByteBuffer compressed,
int outputOffset); int outputOffset);
public native static boolean rawDecompress(ByteBuffer compressed, int inputOffset, int inputLength, public native static int rawUncompress(ByteBuffer compressed, int inputOffset, int inputLength,
ByteBuffer uncompressed, int outputOffset); ByteBuffer uncompressed, int outputOffset) throws SnappyException;
// Returns the maximal size of the compressed representation of // Returns the maximal size of the compressed representation of
// input data that is "source_bytes" bytes in length; // input data that is "source_bytes" bytes in length;
public native static int maxCompressedLength(int source_bytes); public native static int maxCompressedLength(int source_bytes);
// This operation takes O(1) time. // This operation takes O(1) time.
public native static int getUncompressedLength(ByteBuffer compressed, int offset, int len); public native static int getUncompressedLength(ByteBuffer compressed, int offset, int len) throws SnappyException;
public static void throw_error(int errorCode) throws SnappyException {
throw new SnappyException(errorCode);
}
} }

View File

@ -58,14 +58,14 @@ public class SnappyTest
src.put(orig); src.put(orig);
src.flip(); src.flip();
_logger.info("input size: " + src.remaining()); _logger.info("input size: " + src.remaining());
int maxCompressedLen = Snappy.getMaxCompressedLength(src.remaining()); int maxCompressedLen = Snappy.maxCompressedLength(src.remaining());
_logger.info("max compressed length:" + maxCompressedLen); _logger.info("max compressed length:" + maxCompressedLen);
ByteBuffer compressed = ByteBuffer.allocateDirect(maxCompressedLen); ByteBuffer compressed = ByteBuffer.allocateDirect(maxCompressedLen);
int compressedSize = Snappy.compress(src, compressed); int compressedSize = Snappy.compress(src, compressed);
_logger.info("compressed length: " + compressedSize); _logger.info("compressed length: " + compressedSize);
int uncompressedLen = Snappy.getUncompressedLength(compressed); int uncompressedLen = Snappy.uncompressedLength(compressed);
_logger.info("uncompressed length: " + uncompressedLen); _logger.info("uncompressed length: " + uncompressedLen);
ByteBuffer extract = ByteBuffer.allocateDirect(uncompressedLen); ByteBuffer extract = ByteBuffer.allocateDirect(uncompressedLen);
Snappy.uncompress(compressed, extract); Snappy.uncompress(compressed, extract);