diff --git a/.settings/org.eclipse.jdt.ui.prefs b/.settings/org.eclipse.jdt.ui.prefs
new file mode 100755
index 0000000..dad45ba
--- /dev/null
+++ b/.settings/org.eclipse.jdt.ui.prefs
@@ -0,0 +1,4 @@
+#Wed Mar 30 14:56:03 JST 2011
+eclipse.preferences.version=1
+org.eclipse.jdt.ui.javadoc=false
+org.eclipse.jdt.ui.text.custom_code_templates=/**\r\n * @return Returns the ${bare_field_name}.\r\n *//**\r\n * @param ${param} The ${bare_field_name} to set.\r\n *//**\r\n * ${tags}\r\n *//**\r\n * \r\n *//**\r\n * @author ${user}\r\n *\r\n * ${tags}\r\n *//**\r\n * \r\n *//**\r\n * ${tags}\r\n *//* (non-Javadoc)\r\n * ${see_to_overridden}\r\n *//**\r\n * ${tags}\r\n * ${see_to_target}\r\n *//*--------------------------------------------------------------------------\r\n * Copyright ${year} Taro L. Saito\r\n *\r\n * Licensed under the Apache License, Version 2.0 (the "License");\r\n * you may not use this file except in compliance with the License.\r\n * You may obtain a copy of the License at\r\n *\r\n * http\://www.apache.org/licenses/LICENSE-2.0\r\n *\r\n * Unless required by applicable law or agreed to in writing, software\r\n * distributed under the License is distributed on an "AS IS" BASIS,\r\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n * See the License for the specific language governing permissions and\r\n * limitations under the License.\r\n *--------------------------------------------------------------------------*/\r\n//--------------------------------------\r\n// XerialJ\r\n//\r\n// ${file_name}\r\n// Since\: ${date} ${time}\r\n//\r\n// $$URL$$\r\n// $$Author$$\r\n//--------------------------------------\r\n${filecomment}\r\n${package_declaration}\r\n\r\n${typecomment}\r\n${type_declaration}\r\n\r\n\r\n\r\n// ${todo} Auto-generated catch block\r\n${exception_var}.printStackTrace();// ${todo} Auto-generated method stub\r\n${body_statement}${body_statement}\r\n// ${todo} Auto-generated constructor stubreturn ${field};${field} \= ${param};
diff --git a/src/main/java/org/xerial/snappy/LoadSnappy.java b/src/main/java/org/xerial/snappy/LoadSnappy.java
index fa20286..10d3c6f 100755
--- a/src/main/java/org/xerial/snappy/LoadSnappy.java
+++ b/src/main/java/org/xerial/snappy/LoadSnappy.java
@@ -140,8 +140,7 @@ public class LoadSnappy
return true;
}
catch (UnsatisfiedLinkError e) {
- System.err.println(e);
- return false;
+ throw new SnappyError(SnappyErrorCode.FAILED_TO_LOAD_NATIVE_LIBRARY, e);
}
}
diff --git a/src/main/java/org/xerial/snappy/Snappy.java b/src/main/java/org/xerial/snappy/Snappy.java
index 4530934..ce00df5 100755
--- a/src/main/java/org/xerial/snappy/Snappy.java
+++ b/src/main/java/org/xerial/snappy/Snappy.java
@@ -11,6 +11,12 @@ package org.xerial.snappy;
import java.nio.ByteBuffer;
+/**
+ * Snappy API
+ *
+ * @author leo
+ *
+ */
public class Snappy
{
@@ -19,11 +25,13 @@ public class Snappy
}
/**
+ * Compress the content of the given input, then output the compressed data.
+ *
* @param uncompressed
* input is at buffer[pos() ... limit())
* @param compressed
* 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) {
@@ -50,9 +58,10 @@ public class Snappy
* input is at buffer[pos() ... limit())
* @param uncompressed
* 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())
throw new IllegalArgumentException("input is not a direct buffer");
@@ -62,12 +71,13 @@ public class Snappy
int cPos = compressed.position();
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());
- // compressed.position(cPos + cLen);
-
- return ret;
+ return decompressedSize;
}
/**
@@ -77,7 +87,7 @@ public class Snappy
* data [pos() ... limit())
* @return
*/
- public static int getUncompressedLength(ByteBuffer compressed) {
+ public static int uncompressedLength(ByteBuffer compressed) throws SnappyException {
if (!compressed.isDirect())
throw new IllegalArgumentException("input is not a direct buffer");
@@ -91,7 +101,7 @@ public class Snappy
* byte size of the data to compress
* @return maxmum byte size of the compressed data
*/
- public static int getMaxCompressedLength(int byteSize) {
+ public static int maxCompressedLength(int byteSize) {
return SnappyNative.maxCompressedLength(byteSize);
}
diff --git a/src/main/java/org/xerial/snappy/SnappyError.java b/src/main/java/org/xerial/snappy/SnappyError.java
new file mode 100755
index 0000000..124281a
--- /dev/null
+++ b/src/main/java/org/xerial/snappy/SnappyError.java
@@ -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;
+ }
+}
diff --git a/src/main/java/org/xerial/snappy/SnappyErrorCode.java b/src/main/java/org/xerial/snappy/SnappyErrorCode.java
index b0fe529..a1de884 100755
--- a/src/main/java/org/xerial/snappy/SnappyErrorCode.java
+++ b/src/main/java/org/xerial/snappy/SnappyErrorCode.java
@@ -32,6 +32,11 @@ package org.xerial.snappy;
*/
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;
+ }
}
diff --git a/src/main/java/org/xerial/snappy/SnappyException.java b/src/main/java/org/xerial/snappy/SnappyException.java
index 8ffabda..b95fc4e 100755
--- a/src/main/java/org/xerial/snappy/SnappyException.java
+++ b/src/main/java/org/xerial/snappy/SnappyException.java
@@ -36,10 +36,30 @@ public class SnappyException extends Exception
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) {
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() {
return errorCode;
}
diff --git a/src/main/java/org/xerial/snappy/SnappyNative.cpp b/src/main/java/org/xerial/snappy/SnappyNative.cpp
index 446b00d..3a69cd0 100755
--- a/src/main/java/org/xerial/snappy/SnappyNative.cpp
+++ b/src/main/java/org/xerial/snappy/SnappyNative.cpp
@@ -2,6 +2,17 @@
#include
#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
(JNIEnv * env, jclass self)
{
@@ -29,7 +40,7 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress
* Method: uncompress
* 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)
{
char* compressedBuffer = (char*) env->GetDirectBufferAddress(compressed) + cpos;
@@ -38,8 +49,12 @@ JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_SnappyNative_rawDecompress
size_t decompressedLength;
snappy::GetUncompressedLength(compressedBuffer, (size_t) clen, &decompressedLength);
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;
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;
}
diff --git a/src/main/java/org/xerial/snappy/SnappyNative.h b/src/main/java/org/xerial/snappy/SnappyNative.h
index a4287c8..6bc43e1 100755
--- a/src/main/java/org/xerial/snappy/SnappyNative.h
+++ b/src/main/java/org/xerial/snappy/SnappyNative.h
@@ -25,10 +25,10 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress
/*
* Class: org_xerial_snappy_SnappyNative
- * Method: rawDecompress
- * Signature: (Ljava/nio/ByteBuffer;IILjava/nio/ByteBuffer;I)Z
+ * Method: rawUncompress
+ * 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);
/*
diff --git a/src/main/java/org/xerial/snappy/SnappyNative.java b/src/main/java/org/xerial/snappy/SnappyNative.java
index 2195a21..943bc16 100755
--- a/src/main/java/org/xerial/snappy/SnappyNative.java
+++ b/src/main/java/org/xerial/snappy/SnappyNative.java
@@ -25,14 +25,18 @@ public class SnappyNative
public native static int rawCompress(ByteBuffer input, int inputOffset, int inputLength, ByteBuffer compressed,
int outputOffset);
- public native static boolean rawDecompress(ByteBuffer compressed, int inputOffset, int inputLength,
- ByteBuffer uncompressed, int outputOffset);
+ public native static int rawUncompress(ByteBuffer compressed, int inputOffset, int inputLength,
+ ByteBuffer uncompressed, 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);
// 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);
+ }
}
diff --git a/src/main/resources/org/xerial/snappy/native/Windows/amd64/snappy.dll b/src/main/resources/org/xerial/snappy/native/Windows/amd64/snappy.dll
index 0aedf65..e053ecf 100755
Binary files a/src/main/resources/org/xerial/snappy/native/Windows/amd64/snappy.dll and b/src/main/resources/org/xerial/snappy/native/Windows/amd64/snappy.dll differ
diff --git a/src/test/java/org/xerial/snappy/SnappyTest.java b/src/test/java/org/xerial/snappy/SnappyTest.java
index f59819e..6271552 100755
--- a/src/test/java/org/xerial/snappy/SnappyTest.java
+++ b/src/test/java/org/xerial/snappy/SnappyTest.java
@@ -58,14 +58,14 @@ public class SnappyTest
src.put(orig);
src.flip();
_logger.info("input size: " + src.remaining());
- int maxCompressedLen = Snappy.getMaxCompressedLength(src.remaining());
+ int maxCompressedLen = Snappy.maxCompressedLength(src.remaining());
_logger.info("max compressed length:" + maxCompressedLen);
ByteBuffer compressed = ByteBuffer.allocateDirect(maxCompressedLen);
int compressedSize = Snappy.compress(src, compressed);
_logger.info("compressed length: " + compressedSize);
- int uncompressedLen = Snappy.getUncompressedLength(compressed);
+ int uncompressedLen = Snappy.uncompressedLength(compressed);
_logger.info("uncompressed length: " + uncompressedLen);
ByteBuffer extract = ByteBuffer.allocateDirect(uncompressedLen);
Snappy.uncompress(compressed, extract);