Skip to content

Commit 960481b

Browse files
committed
add SnappyException/SnappyError
1 parent 07fe11c commit 960481b

File tree

11 files changed

+131
-24
lines changed

11 files changed

+131
-24
lines changed

.settings/org.eclipse.jdt.ui.prefs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#Wed Mar 30 14:56:03 JST 2011
2+
eclipse.preferences.version=1
3+
org.eclipse.jdt.ui.javadoc=false
4+
org.eclipse.jdt.ui.text.custom_code_templates=<?xml version\="1.0" encoding\="UTF-8" standalone\="no"?><templates><template autoinsert\="true" context\="gettercomment_context" deleted\="false" description\="Comment for getter method" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.gettercomment" name\="gettercomment">/**\r\n * @return Returns the ${bare_field_name}.\r\n */</template><template autoinsert\="true" context\="settercomment_context" deleted\="false" description\="Comment for setter method" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.settercomment" name\="settercomment">/**\r\n * @param ${param} The ${bare_field_name} to set.\r\n */</template><template autoinsert\="true" context\="constructorcomment_context" deleted\="false" description\="Comment for created constructors" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.constructorcomment" name\="constructorcomment">/**\r\n * ${tags}\r\n */</template><template autoinsert\="true" context\="filecomment_context" deleted\="false" description\="Comment for created Java files" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.filecomment" name\="filecomment">/**\r\n * \r\n */</template><template autoinsert\="true" context\="typecomment_context" deleted\="false" description\="Comment for created types" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.typecomment" name\="typecomment">/**\r\n * @author ${user}\r\n *\r\n * ${tags}\r\n */</template><template autoinsert\="true" context\="fieldcomment_context" deleted\="false" description\="Comment for fields" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.fieldcomment" name\="fieldcomment">/**\r\n * \r\n */</template><template autoinsert\="true" context\="methodcomment_context" deleted\="false" description\="Comment for non-overriding methods" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.methodcomment" name\="methodcomment">/**\r\n * ${tags}\r\n */</template><template autoinsert\="true" context\="overridecomment_context" deleted\="false" description\="Comment for overriding methods" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.overridecomment" name\="overridecomment">/* (non-Javadoc)\r\n * ${see_to_overridden}\r\n */</template><template autoinsert\="true" context\="delegatecomment_context" deleted\="false" description\="Comment for delegate methods" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.delegatecomment" name\="delegatecomment">/**\r\n * ${tags}\r\n * ${see_to_target}\r\n */</template><template autoinsert\="false" context\="newtype_context" deleted\="false" description\="Newly created files" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.newtype" name\="newtype">/*--------------------------------------------------------------------------\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}</template><template autoinsert\="true" context\="classbody_context" deleted\="false" description\="Code in new class type bodies" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.classbody" name\="classbody">\r\n</template><template autoinsert\="true" context\="interfacebody_context" deleted\="false" description\="Code in new interface type bodies" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.interfacebody" name\="interfacebody">\r\n</template><template autoinsert\="true" context\="enumbody_context" deleted\="false" description\="Code in new enum type bodies" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.enumbody" name\="enumbody">\r\n</template><template autoinsert\="true" context\="annotationbody_context" deleted\="false" description\="Code in new annotation type bodies" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.annotationbody" name\="annotationbody">\r\n</template><template autoinsert\="true" context\="catchblock_context" deleted\="false" description\="Code in new catch blocks" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.catchblock" name\="catchblock">// ${todo} Auto-generated catch block\r\n${exception_var}.printStackTrace();</template><template autoinsert\="true" context\="methodbody_context" deleted\="false" description\="Code in created method stubs" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.methodbody" name\="methodbody">// ${todo} Auto-generated method stub\r\n${body_statement}</template><template autoinsert\="true" context\="constructorbody_context" deleted\="false" description\="Code in created constructor stubs" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.constructorbody" name\="constructorbody">${body_statement}\r\n// ${todo} Auto-generated constructor stub</template><template autoinsert\="true" context\="getterbody_context" deleted\="false" description\="Code in created getters" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.getterbody" name\="getterbody">return ${field};</template><template autoinsert\="true" context\="setterbody_context" deleted\="false" description\="Code in created setters" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.setterbody" name\="setterbody">${field} \= ${param};</template></templates>

src/main/java/org/xerial/snappy/LoadSnappy.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ private static synchronized boolean loadNativeLibrary(String path, String name)
140140
return true;
141141
}
142142
catch (UnsatisfiedLinkError e) {
143-
System.err.println(e);
144-
return false;
143+
throw new SnappyError(SnappyErrorCode.FAILED_TO_LOAD_NATIVE_LIBRARY, e);
145144
}
146145

147146
}

src/main/java/org/xerial/snappy/Snappy.java

+20-10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111

1212
import java.nio.ByteBuffer;
1313

14+
/**
15+
* Snappy API
16+
*
17+
* @author leo
18+
*
19+
*/
1420
public class Snappy
1521
{
1622

@@ -19,11 +25,13 @@ public static String getNativeLibraryVersion() {
1925
}
2026

2127
/**
28+
* Compress the content of the given input, then output the compressed data.
29+
*
2230
* @param uncompressed
2331
* input is at buffer[pos() ... limit())
2432
* @param compressed
2533
* output compressed data to buffer[pos()..]
26-
* @return byte size of the compressed data
34+
* @return byte size of the compressed data.
2735
*/
2836
public static int compress(ByteBuffer uncompressed, ByteBuffer compressed) {
2937

@@ -50,9 +58,10 @@ public static int compress(ByteBuffer uncompressed, ByteBuffer compressed) {
5058
* input is at buffer[pos() ... limit())
5159
* @param uncompressed
5260
* output decompressed data to buffer[pot())
53-
* @return
61+
* @return decompressed data size
62+
*
5463
*/
55-
public static boolean uncompress(ByteBuffer compressed, ByteBuffer decompressed) {
64+
public static int uncompress(ByteBuffer compressed, ByteBuffer decompressed) throws SnappyException {
5665

5766
if (!compressed.isDirect())
5867
throw new IllegalArgumentException("input is not a direct buffer");
@@ -62,12 +71,13 @@ public static boolean uncompress(ByteBuffer compressed, ByteBuffer decompressed)
6271
int cPos = compressed.position();
6372
int cLen = compressed.remaining();
6473

65-
boolean ret = SnappyNative.rawDecompress(compressed, cPos, cLen, decompressed, decompressed.position());
66-
67-
// compressed.limit(compressed.capacity());
68-
// compressed.position(cPos + cLen);
74+
// pos limit
75+
// [ ......UUUUUU.........]
76+
int decompressedSize = SnappyNative
77+
.rawUncompress(compressed, cPos, cLen, decompressed, decompressed.position());
78+
decompressed.limit(decompressed.position() + decompressedSize);
6979

70-
return ret;
80+
return decompressedSize;
7181
}
7282

7383
/**
@@ -77,7 +87,7 @@ public static boolean uncompress(ByteBuffer compressed, ByteBuffer decompressed)
7787
* data [pos() ... limit())
7888
* @return
7989
*/
80-
public static int getUncompressedLength(ByteBuffer compressed) {
90+
public static int uncompressedLength(ByteBuffer compressed) throws SnappyException {
8191
if (!compressed.isDirect())
8292
throw new IllegalArgumentException("input is not a direct buffer");
8393

@@ -91,7 +101,7 @@ public static int getUncompressedLength(ByteBuffer compressed) {
91101
* byte size of the data to compress
92102
* @return maxmum byte size of the compressed data
93103
*/
94-
public static int getMaxCompressedLength(int byteSize) {
104+
public static int maxCompressedLength(int byteSize) {
95105
return SnappyNative.maxCompressedLength(byteSize);
96106
}
97107

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*--------------------------------------------------------------------------
2+
* Copyright 2011 Taro L. Saito
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*--------------------------------------------------------------------------*/
16+
//--------------------------------------
17+
// XerialJ
18+
//
19+
// SnappyError.java
20+
// Since: 2011/03/30 15:22:43
21+
//
22+
// $URL$
23+
// $Author$
24+
//--------------------------------------
25+
package org.xerial.snappy;
26+
27+
/**
28+
* Used when serious errors (unchecked exception) in snappy-java are observed.
29+
*
30+
* @author leo
31+
*
32+
*/
33+
public class SnappyError extends Error
34+
{
35+
/**
36+
*
37+
*/
38+
private static final long serialVersionUID = 1L;
39+
40+
public final SnappyErrorCode errorCode;
41+
42+
public SnappyError(SnappyErrorCode code, Error e) {
43+
super(e);
44+
this.errorCode = code;
45+
}
46+
}

src/main/java/org/xerial/snappy/SnappyErrorCode.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
*/
3333
public enum SnappyErrorCode {
3434

35-
FAILED_TO_LOAD_NATIVE_LIBRARY, PARSING_ERROR, ;
35+
UNKNOWN(0), FAILED_TO_LOAD_NATIVE_LIBRARY(1), PARSING_ERROR(2);
3636

37+
public final int id;
38+
39+
private SnappyErrorCode(int id) {
40+
this.id = id;
41+
}
3742
}

src/main/java/org/xerial/snappy/SnappyException.java

+20
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,30 @@ public class SnappyException extends Exception
3636

3737
public final SnappyErrorCode errorCode;
3838

39+
public SnappyException(int code) {
40+
SnappyErrorCode[] values = SnappyErrorCode.values();
41+
if (code < 0 || code >= values.length) {
42+
this.errorCode = SnappyErrorCode.UNKNOWN;
43+
}
44+
else {
45+
this.errorCode = values[code];
46+
}
47+
}
48+
3949
public SnappyException(SnappyErrorCode errorCode) {
4050
this.errorCode = errorCode;
4151
}
4252

53+
public SnappyException(SnappyErrorCode errorCode, Exception e) {
54+
super(e);
55+
this.errorCode = errorCode;
56+
}
57+
58+
public SnappyException(SnappyErrorCode errorCode, String message) {
59+
super(message);
60+
this.errorCode = errorCode;
61+
}
62+
4363
public SnappyErrorCode getErrorCode() {
4464
return errorCode;
4565
}

src/main/java/org/xerial/snappy/SnappyNative.cpp

+22-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22
#include <snappy.h>
33
#include "SnappyNative.h"
44

5+
void throw_exception(JNIEnv *env, jclass self, int errorCode)
6+
{
7+
jmethodID mth_throwex = 0;
8+
9+
if (!mth_throwex)
10+
mth_throwex = env->GetMethodID(self, "throw_error", "(I)V");
11+
12+
env->CallVoidMethod(self, mth_throwex, (jint) errorCode);
13+
}
14+
15+
516
JNIEXPORT jstring JNICALL Java_org_xerial_snappy_SnappyNative_nativeLibraryVersion
617
(JNIEnv * env, jclass self)
718
{
@@ -29,7 +40,7 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress
2940
* Method: uncompress
3041
* Signature: (Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;)Z
3142
*/
32-
JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_SnappyNative_rawDecompress
43+
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompress
3344
(JNIEnv * env, jclass self, jobject compressed, jint cpos, jint clen, jobject decompressed, jint dpos)
3445
{
3546
char* compressedBuffer = (char*) env->GetDirectBufferAddress(compressed) + cpos;
@@ -38,8 +49,12 @@ JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_SnappyNative_rawDecompress
3849
size_t decompressedLength;
3950
snappy::GetUncompressedLength(compressedBuffer, (size_t) clen, &decompressedLength);
4051
bool ret = snappy::RawUncompress(compressedBuffer, (size_t) clen, decompressedBuffer);
52+
if(!ret) {
53+
throw_exception(env, self, 2);
54+
return 0;
55+
}
4156

42-
return (jboolean) ret;
57+
return (jint) decompressedLength;
4358
}
4459

4560
/*
@@ -64,7 +79,11 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_getUncompressedLength
6479
{
6580
char* compressedBuffer = (char*) env->GetDirectBufferAddress(compressed) + cpos;
6681
size_t result;
67-
snappy::GetUncompressedLength(compressedBuffer, (size_t) clen, &result);
82+
bool ret = snappy::GetUncompressedLength(compressedBuffer, (size_t) clen, &result);
83+
if(!ret) {
84+
throw_exception(env, self, 2);
85+
return 0;
86+
}
6887
return (jint) result;
6988
}
7089

src/main/java/org/xerial/snappy/SnappyNative.h

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/org/xerial/snappy/SnappyNative.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,18 @@ public class SnappyNative
2525
public native static int rawCompress(ByteBuffer input, int inputOffset, int inputLength, ByteBuffer compressed,
2626
int outputOffset);
2727

28-
public native static boolean rawDecompress(ByteBuffer compressed, int inputOffset, int inputLength,
29-
ByteBuffer uncompressed, int outputOffset);
28+
public native static int rawUncompress(ByteBuffer compressed, int inputOffset, int inputLength,
29+
ByteBuffer uncompressed, int outputOffset) throws SnappyException;
3030

3131
// Returns the maximal size of the compressed representation of
3232
// input data that is "source_bytes" bytes in length;
3333
public native static int maxCompressedLength(int source_bytes);
3434

3535
// This operation takes O(1) time.
36-
public native static int getUncompressedLength(ByteBuffer compressed, int offset, int len);
36+
public native static int getUncompressedLength(ByteBuffer compressed, int offset, int len) throws SnappyException;
37+
38+
public static void throw_error(int errorCode) throws SnappyException {
39+
throw new SnappyException(errorCode);
40+
}
3741

3842
}
Binary file not shown.

src/test/java/org/xerial/snappy/SnappyTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ public void load() throws Exception {
5858
src.put(orig);
5959
src.flip();
6060
_logger.info("input size: " + src.remaining());
61-
int maxCompressedLen = Snappy.getMaxCompressedLength(src.remaining());
61+
int maxCompressedLen = Snappy.maxCompressedLength(src.remaining());
6262
_logger.info("max compressed length:" + maxCompressedLen);
6363

6464
ByteBuffer compressed = ByteBuffer.allocateDirect(maxCompressedLen);
6565
int compressedSize = Snappy.compress(src, compressed);
6666
_logger.info("compressed length: " + compressedSize);
6767

68-
int uncompressedLen = Snappy.getUncompressedLength(compressed);
68+
int uncompressedLen = Snappy.uncompressedLength(compressed);
6969
_logger.info("uncompressed length: " + uncompressedLen);
7070
ByteBuffer extract = ByteBuffer.allocateDirect(uncompressedLen);
7171
Snappy.uncompress(compressed, extract);

0 commit comments

Comments
 (0)