Skip to content

Commit 07fe11c

Browse files
committed
add Exception class
1 parent fa2fcab commit 07fe11c

File tree

6 files changed

+107
-10
lines changed

6 files changed

+107
-10
lines changed

Makefile.common

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ SNAPPY_FLAGS := $($(os_arch)_SNAPPY_FLAGS)
109109

110110
SNAPPY_OUT:=$(TARGET)/$(snappy)-$(os_arch)
111111

112-
CXXFLAGS := $(CXXFLAGS) -I$(SNAPPY_OUT) -I$(SNAPPY_SRC)
112+
CXXFLAGS := $(CXXFLAGS) -Ilib/include -I$(SNAPPY_OUT) -I$(SNAPPY_SRC)
113113
ifneq ($(jni_include),)
114114
CXXFLAGS := $(CXXFLAGS) -I"$(jni_include)"
115115
endif

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

+17-8
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static String getNativeLibraryVersion() {
2222
* @param uncompressed
2323
* input is at buffer[pos() ... limit())
2424
* @param compressed
25-
* output compressed data to buffer[pos()]
25+
* output compressed data to buffer[pos()..]
2626
* @return byte size of the compressed data
2727
*/
2828
public static int compress(ByteBuffer uncompressed, ByteBuffer compressed) {
@@ -38,11 +38,6 @@ public static int compress(ByteBuffer uncompressed, ByteBuffer compressed) {
3838
int uLen = uncompressed.remaining();
3939
int compressedSize = SnappyNative.rawCompress(uncompressed, uPos, uLen, compressed, compressed.position());
4040

41-
// // pos limit
42-
// // [ ....XXXXXX.........]
43-
// uncompressed.limit(uncompressed.capacity());
44-
// uncompressed.position(uPos + uLen);
45-
4641
// pos limit
4742
// [ ......BBBBBBB.........]
4843
compressed.limit(compressed.position() + compressedSize);
@@ -53,11 +48,11 @@ public static int compress(ByteBuffer uncompressed, ByteBuffer compressed) {
5348
/**
5449
* @param compressed
5550
* input is at buffer[pos() ... limit())
56-
* @param decompressed
51+
* @param uncompressed
5752
* output decompressed data to buffer[pot())
5853
* @return
5954
*/
60-
public static boolean decompress(ByteBuffer compressed, ByteBuffer decompressed) {
55+
public static boolean uncompress(ByteBuffer compressed, ByteBuffer decompressed) {
6156

6257
if (!compressed.isDirect())
6358
throw new IllegalArgumentException("input is not a direct buffer");
@@ -75,13 +70,27 @@ public static boolean decompress(ByteBuffer compressed, ByteBuffer decompressed)
7570
return ret;
7671
}
7772

73+
/**
74+
* Get the uncompressed size of the compressed input
75+
*
76+
* @param compressed
77+
* data [pos() ... limit())
78+
* @return
79+
*/
7880
public static int getUncompressedLength(ByteBuffer compressed) {
7981
if (!compressed.isDirect())
8082
throw new IllegalArgumentException("input is not a direct buffer");
8183

8284
return SnappyNative.getUncompressedLength(compressed, compressed.position(), compressed.remaining());
8385
}
8486

87+
/**
88+
* Get the maximum size of the compressed data of a given byte size
89+
*
90+
* @param byteSize
91+
* byte size of the data to compress
92+
* @return maxmum byte size of the compressed data
93+
*/
8594
public static int getMaxCompressedLength(int byteSize) {
8695
return SnappyNative.maxCompressedLength(byteSize);
8796
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
// SnappyErrorCode.java
20+
// Since: 2011/03/30 14:56:50
21+
//
22+
// $URL$
23+
// $Author$
24+
//--------------------------------------
25+
package org.xerial.snappy;
26+
27+
/**
28+
* Error codes of snappy-java
29+
*
30+
* @author leo
31+
*
32+
*/
33+
public enum SnappyErrorCode {
34+
35+
FAILED_TO_LOAD_NATIVE_LIBRARY, PARSING_ERROR, ;
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
// SnappyException.java
20+
// Since: 2011/03/30 14:56:14
21+
//
22+
// $URL$
23+
// $Author$
24+
//--------------------------------------
25+
package org.xerial.snappy;
26+
27+
/**
28+
* Exception in snappy-java
29+
*
30+
* @author leo
31+
*
32+
*/
33+
public class SnappyException extends Exception
34+
{
35+
private static final long serialVersionUID = 1L;
36+
37+
public final SnappyErrorCode errorCode;
38+
39+
public SnappyException(SnappyErrorCode errorCode) {
40+
this.errorCode = errorCode;
41+
}
42+
43+
public SnappyErrorCode getErrorCode() {
44+
return errorCode;
45+
}
46+
47+
@Override
48+
public String getMessage() {
49+
return String.format("[%s] %s", errorCode.name(), super.getMessage());
50+
}
51+
}
Binary file not shown.

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void load() throws Exception {
6868
int uncompressedLen = Snappy.getUncompressedLength(compressed);
6969
_logger.info("uncompressed length: " + uncompressedLen);
7070
ByteBuffer extract = ByteBuffer.allocateDirect(uncompressedLen);
71-
Snappy.decompress(compressed, extract);
71+
Snappy.uncompress(compressed, extract);
7272
extract.limit(uncompressedLen);
7373

7474
byte[] b = new byte[uncompressedLen];

0 commit comments

Comments
 (0)