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