diff --git a/src/main/java/org/xerial/snappy/Snappy.java b/src/main/java/org/xerial/snappy/Snappy.java index 4c62d5a..ffb004d 100755 --- a/src/main/java/org/xerial/snappy/Snappy.java +++ b/src/main/java/org/xerial/snappy/Snappy.java @@ -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()); } diff --git a/src/main/java/org/xerial/snappy/SnappyError.java b/src/main/java/org/xerial/snappy/SnappyError.java index 124281a..b377470 100755 --- a/src/main/java/org/xerial/snappy/SnappyError.java +++ b/src/main/java/org/xerial/snappy/SnappyError.java @@ -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; + } + } diff --git a/src/main/java/org/xerial/snappy/SnappyErrorCode.java b/src/main/java/org/xerial/snappy/SnappyErrorCode.java index a1de884..2798fde 100755 --- a/src/main/java/org/xerial/snappy/SnappyErrorCode.java +++ b/src/main/java/org/xerial/snappy/SnappyErrorCode.java @@ -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; diff --git a/src/test/java/org/xerial/snappy/SnappyTest.java b/src/test/java/org/xerial/snappy/SnappyTest.java index ce00f29..17899a8 100755 --- a/src/test/java/org/xerial/snappy/SnappyTest.java +++ b/src/test/java/org/xerial/snappy/SnappyTest.java @@ -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); } }