mirror of
https://github.com/xerial/snappy-java.git
synced 2025-07-22 05:24:27 +02:00
add Exception class
This commit is contained in:
parent
fa2fcabc43
commit
07fe11c360
@ -109,7 +109,7 @@ SNAPPY_FLAGS := $($(os_arch)_SNAPPY_FLAGS)
|
|||||||
|
|
||||||
SNAPPY_OUT:=$(TARGET)/$(snappy)-$(os_arch)
|
SNAPPY_OUT:=$(TARGET)/$(snappy)-$(os_arch)
|
||||||
|
|
||||||
CXXFLAGS := $(CXXFLAGS) -I$(SNAPPY_OUT) -I$(SNAPPY_SRC)
|
CXXFLAGS := $(CXXFLAGS) -Ilib/include -I$(SNAPPY_OUT) -I$(SNAPPY_SRC)
|
||||||
ifneq ($(jni_include),)
|
ifneq ($(jni_include),)
|
||||||
CXXFLAGS := $(CXXFLAGS) -I"$(jni_include)"
|
CXXFLAGS := $(CXXFLAGS) -I"$(jni_include)"
|
||||||
endif
|
endif
|
||||||
|
@ -22,7 +22,7 @@ public class Snappy
|
|||||||
* @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) {
|
||||||
@ -38,11 +38,6 @@ public class Snappy
|
|||||||
int uLen = uncompressed.remaining();
|
int uLen = uncompressed.remaining();
|
||||||
int compressedSize = SnappyNative.rawCompress(uncompressed, uPos, uLen, compressed, compressed.position());
|
int compressedSize = SnappyNative.rawCompress(uncompressed, uPos, uLen, compressed, compressed.position());
|
||||||
|
|
||||||
// // pos limit
|
|
||||||
// // [ ....XXXXXX.........]
|
|
||||||
// uncompressed.limit(uncompressed.capacity());
|
|
||||||
// uncompressed.position(uPos + uLen);
|
|
||||||
|
|
||||||
// pos limit
|
// pos limit
|
||||||
// [ ......BBBBBBB.........]
|
// [ ......BBBBBBB.........]
|
||||||
compressed.limit(compressed.position() + compressedSize);
|
compressed.limit(compressed.position() + compressedSize);
|
||||||
@ -53,11 +48,11 @@ public class Snappy
|
|||||||
/**
|
/**
|
||||||
* @param compressed
|
* @param compressed
|
||||||
* input is at buffer[pos() ... limit())
|
* input is at buffer[pos() ... limit())
|
||||||
* @param decompressed
|
* @param uncompressed
|
||||||
* output decompressed data to buffer[pot())
|
* output decompressed data to buffer[pot())
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static boolean decompress(ByteBuffer compressed, ByteBuffer decompressed) {
|
public static boolean uncompress(ByteBuffer compressed, ByteBuffer decompressed) {
|
||||||
|
|
||||||
if (!compressed.isDirect())
|
if (!compressed.isDirect())
|
||||||
throw new IllegalArgumentException("input is not a direct buffer");
|
throw new IllegalArgumentException("input is not a direct buffer");
|
||||||
@ -75,6 +70,13 @@ public class Snappy
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the uncompressed size of the compressed input
|
||||||
|
*
|
||||||
|
* @param compressed
|
||||||
|
* data [pos() ... limit())
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
public static int getUncompressedLength(ByteBuffer compressed) {
|
public static int getUncompressedLength(ByteBuffer compressed) {
|
||||||
if (!compressed.isDirect())
|
if (!compressed.isDirect())
|
||||||
throw new IllegalArgumentException("input is not a direct buffer");
|
throw new IllegalArgumentException("input is not a direct buffer");
|
||||||
@ -82,6 +84,13 @@ public class Snappy
|
|||||||
return SnappyNative.getUncompressedLength(compressed, compressed.position(), compressed.remaining());
|
return SnappyNative.getUncompressedLength(compressed, compressed.position(), compressed.remaining());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the maximum size of the compressed data of a given byte size
|
||||||
|
*
|
||||||
|
* @param byteSize
|
||||||
|
* byte size of the data to compress
|
||||||
|
* @return maxmum byte size of the compressed data
|
||||||
|
*/
|
||||||
public static int getMaxCompressedLength(int byteSize) {
|
public static int getMaxCompressedLength(int byteSize) {
|
||||||
return SnappyNative.maxCompressedLength(byteSize);
|
return SnappyNative.maxCompressedLength(byteSize);
|
||||||
}
|
}
|
||||||
|
37
src/main/java/org/xerial/snappy/SnappyErrorCode.java
Executable file
37
src/main/java/org/xerial/snappy/SnappyErrorCode.java
Executable file
@ -0,0 +1,37 @@
|
|||||||
|
/*--------------------------------------------------------------------------
|
||||||
|
* 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
|
||||||
|
//
|
||||||
|
// SnappyErrorCode.java
|
||||||
|
// Since: 2011/03/30 14:56:50
|
||||||
|
//
|
||||||
|
// $URL$
|
||||||
|
// $Author$
|
||||||
|
//--------------------------------------
|
||||||
|
package org.xerial.snappy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Error codes of snappy-java
|
||||||
|
*
|
||||||
|
* @author leo
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public enum SnappyErrorCode {
|
||||||
|
|
||||||
|
FAILED_TO_LOAD_NATIVE_LIBRARY, PARSING_ERROR, ;
|
||||||
|
|
||||||
|
}
|
51
src/main/java/org/xerial/snappy/SnappyException.java
Executable file
51
src/main/java/org/xerial/snappy/SnappyException.java
Executable file
@ -0,0 +1,51 @@
|
|||||||
|
/*--------------------------------------------------------------------------
|
||||||
|
* 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
|
||||||
|
//
|
||||||
|
// SnappyException.java
|
||||||
|
// Since: 2011/03/30 14:56:14
|
||||||
|
//
|
||||||
|
// $URL$
|
||||||
|
// $Author$
|
||||||
|
//--------------------------------------
|
||||||
|
package org.xerial.snappy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exception in snappy-java
|
||||||
|
*
|
||||||
|
* @author leo
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class SnappyException extends Exception
|
||||||
|
{
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
public final SnappyErrorCode errorCode;
|
||||||
|
|
||||||
|
public SnappyException(SnappyErrorCode errorCode) {
|
||||||
|
this.errorCode = errorCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SnappyErrorCode getErrorCode() {
|
||||||
|
return errorCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getMessage() {
|
||||||
|
return String.format("[%s] %s", errorCode.name(), super.getMessage());
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
@ -68,7 +68,7 @@ public class SnappyTest
|
|||||||
int uncompressedLen = Snappy.getUncompressedLength(compressed);
|
int uncompressedLen = Snappy.getUncompressedLength(compressed);
|
||||||
_logger.info("uncompressed length: " + uncompressedLen);
|
_logger.info("uncompressed length: " + uncompressedLen);
|
||||||
ByteBuffer extract = ByteBuffer.allocateDirect(uncompressedLen);
|
ByteBuffer extract = ByteBuffer.allocateDirect(uncompressedLen);
|
||||||
Snappy.decompress(compressed, extract);
|
Snappy.uncompress(compressed, extract);
|
||||||
extract.limit(uncompressedLen);
|
extract.limit(uncompressedLen);
|
||||||
|
|
||||||
byte[] b = new byte[uncompressedLen];
|
byte[] b = new byte[uncompressedLen];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user