mirror of
https://github.com/xerial/snappy-java.git
synced 2025-07-26 23:44:10 +02:00
use SnappyError for unchecked exception instead of IllegalArgumentException
This commit is contained in:
parent
66e58cdf2b
commit
262222ea7e
@ -47,13 +47,16 @@ public class Snappy
|
||||
* @param compressed
|
||||
* output compressed data to buffer[pos()..]
|
||||
* @return byte size of the compressed data.
|
||||
*
|
||||
* @throws SnappyError
|
||||
* when the input is not a direct buffer
|
||||
*/
|
||||
public static int compress(ByteBuffer uncompressed, ByteBuffer compressed) {
|
||||
|
||||
if (!uncompressed.isDirect())
|
||||
throw new IllegalArgumentException("input is not a direct buffer");
|
||||
throw new SnappyError(SnappyErrorCode.NOT_A_DIRECT_BUFFER, "input is not a direct buffer");
|
||||
if (!compressed.isDirect())
|
||||
throw new IllegalArgumentException("destination is not a direct buffer");
|
||||
throw new SnappyError(SnappyErrorCode.NOT_A_DIRECT_BUFFER, "destination is not a direct buffer");
|
||||
|
||||
// input: uncompressed[pos(), limit())
|
||||
// output: compressed
|
||||
@ -77,13 +80,17 @@ public class Snappy
|
||||
* output the uncompressed data to buffer[pot())
|
||||
* @return uncompressed data size
|
||||
*
|
||||
* @throws SnappyException
|
||||
* when failed to uncompress the given input
|
||||
* @throws SnappyError
|
||||
* when the input is not a direct buffer
|
||||
*/
|
||||
public static int uncompress(ByteBuffer compressed, ByteBuffer uncompressed) throws SnappyException {
|
||||
|
||||
if (!compressed.isDirect())
|
||||
throw new IllegalArgumentException("input is not a direct buffer");
|
||||
throw new SnappyError(SnappyErrorCode.NOT_A_DIRECT_BUFFER, "input is not a direct buffer");
|
||||
if (!uncompressed.isDirect())
|
||||
throw new IllegalArgumentException("destination is not a direct buffer");
|
||||
throw new SnappyError(SnappyErrorCode.NOT_A_DIRECT_BUFFER, "destination is not a direct buffer");
|
||||
|
||||
int cPos = compressed.position();
|
||||
int cLen = compressed.remaining();
|
||||
@ -102,11 +109,15 @@ public class Snappy
|
||||
*
|
||||
* @param compressed
|
||||
* data [pos() ... limit())
|
||||
* @return
|
||||
* @return uncompressed byte length of the given input
|
||||
* @throws SnappyException
|
||||
* when failed to uncompress the given input
|
||||
* @throws SnappyError
|
||||
* when the input is not a direct buffer
|
||||
*/
|
||||
public static int uncompressedLength(ByteBuffer compressed) throws SnappyException {
|
||||
if (!compressed.isDirect())
|
||||
throw new IllegalArgumentException("input is not a direct buffer");
|
||||
throw new SnappyError(SnappyErrorCode.NOT_A_DIRECT_BUFFER, "input is not a direct buffer");
|
||||
|
||||
return SnappyNative.uncompressedLength(compressed, compressed.position(), compressed.remaining());
|
||||
}
|
||||
|
@ -43,4 +43,10 @@ public class SnappyError extends Error
|
||||
super(e);
|
||||
this.errorCode = code;
|
||||
}
|
||||
|
||||
public SnappyError(SnappyErrorCode code, String message) {
|
||||
super(message);
|
||||
this.errorCode = code;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -32,7 +32,7 @@ package org.xerial.snappy;
|
||||
*/
|
||||
public enum SnappyErrorCode {
|
||||
|
||||
UNKNOWN(0), FAILED_TO_LOAD_NATIVE_LIBRARY(1), PARSING_ERROR(2);
|
||||
UNKNOWN(0), FAILED_TO_LOAD_NATIVE_LIBRARY(1), PARSING_ERROR(2), NOT_A_DIRECT_BUFFER(3);
|
||||
|
||||
public final int id;
|
||||
|
||||
|
@ -28,6 +28,7 @@ import static org.junit.Assert.*;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.xerial.util.log.Logger;
|
||||
|
||||
@ -51,8 +52,8 @@ public class SnappyTest
|
||||
ByteBuffer dest = ByteBuffer.allocate(1024);
|
||||
int maxCompressedLen = Snappy.compress(src, dest);
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
// detected non-direct buffer. OK
|
||||
catch (SnappyError e) {
|
||||
Assert.assertTrue(e.errorCode == SnappyErrorCode.NOT_A_DIRECT_BUFFER);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -67,7 +68,8 @@ public class SnappyTest
|
||||
for (int i = 0; i < 20; ++i) {
|
||||
s.append("Hello world!");
|
||||
}
|
||||
byte[] orig = s.toString().getBytes();
|
||||
String origStr = s.toString();
|
||||
byte[] orig = origStr.getBytes();
|
||||
int BUFFER_SIZE = orig.length;
|
||||
ByteBuffer src = ByteBuffer.allocateDirect(orig.length * 2);
|
||||
src.put(orig);
|
||||
@ -89,5 +91,7 @@ public class SnappyTest
|
||||
extract.get(b);
|
||||
String decompressed = new String(b);
|
||||
_logger.info(decompressed);
|
||||
|
||||
assertEquals(origStr, decompressed);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user