From dbb8bcd62c158b8b71e86ba1a946192d8835914f Mon Sep 17 00:00:00 2001 From: Luben Karavelov Date: Mon, 25 Sep 2023 17:15:29 +0100 Subject: [PATCH] Add tests, fix spelling and omissions --- src/main/java/com/github/luben/zstd/Zstd.java | 27 +++++++++++++++---- src/test/scala/Zstd.scala | 9 ++++--- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/github/luben/zstd/Zstd.java b/src/main/java/com/github/luben/zstd/Zstd.java index f1129bd..147be2c 100644 --- a/src/main/java/com/github/luben/zstd/Zstd.java +++ b/src/main/java/com/github/luben/zstd/Zstd.java @@ -724,7 +724,7 @@ public static long decompressedSize(byte[] src) { * @return the number of bytes of the original buffer * 0 if the original size is not known * @deprecated - * Use `getDirectByteBuferFrameContentSize` that return also the errors + * Use `getDirectByteBufferFrameContentSize` that return also the errors */ @Deprecated public static native long decompressedDirectByteBufferSize(ByteBuffer src, int srcPosition, int srcSize, boolean magicless); @@ -740,7 +740,7 @@ public static long decompressedSize(byte[] src) { * 0 if the original size is not known * negative if there is an error decoding the frame header */ - public static native long getDirectByteBuferFrameContentSize(ByteBuffer src, int srcPosition, int srcSize, boolean magicless); + public static native long getDirectByteBufferFrameContentSize(ByteBuffer src, int srcPosition, int srcSize, boolean magicless); /** * Return the original size of a compressed buffer (if known) @@ -751,7 +751,7 @@ public static long decompressedSize(byte[] src) { * @return the number of bytes of the original buffer * 0 if the original size is not known * @deprecated - * Use `getDirectByteBuferFrameContentSize` that return also the errors + * Use `getDirectByteBufferFrameContentSize` that return also the errors */ @Deprecated public static long decompressedDirectByteBufferSize(ByteBuffer src, int srcPosition, int srcSize) { @@ -768,8 +768,8 @@ public static long decompressedDirectByteBufferSize(ByteBuffer src, int srcPosit * 0 if the original size is not known * negative if there is an error decoding the frame header */ - public static long getDirectByteBuferFrameContentSize(ByteBuffer src, int srcPosition, int srcSize) { - return getDirectByteBuferFrameContentSize(src, srcPosition, srcSize, false); + public static long getDirectByteBufferFrameContentSize(ByteBuffer src, int srcPosition, int srcSize) { + return getDirectByteBufferFrameContentSize(src, srcPosition, srcSize, false); } /** @@ -1368,11 +1368,28 @@ public static byte[] decompress(byte[] src, byte[] dict, int originalSize) { * end. * @return the number of bytes of the original buffer * 0 if the original size is not known + * @deprecated + * Use `getDirectByteBufferFrameContentSize` that return also the errors */ + @Deprecated public static long decompressedSize(ByteBuffer srcBuf) { return decompressedDirectByteBufferSize(srcBuf, srcBuf.position(), srcBuf.limit() - srcBuf.position()); } + /** + * Return the original size of a compressed buffer (if known) + * + * @param srcBuf the compressed buffer. must be direct. It is assumed that the position() of this buffer marks the beginning of the + * compressed data whose decompressed size is being queried, and that the limit() of this buffer marks its + * end. + * @return the number of bytes of the original buffer + * 0 if the original size is not known + * negative if there is an error decoding the frame header + */ + public static long getFrameContentSize(ByteBuffer srcBuf) { + return getDirectByteBufferFrameContentSize(srcBuf, srcBuf.position(), srcBuf.limit() - srcBuf.position()); + } + /** * Decompress data * diff --git a/src/test/scala/Zstd.scala b/src/test/scala/Zstd.scala index 2b2c78b..b73f07f 100644 --- a/src/test/scala/Zstd.scala +++ b/src/test/scala/Zstd.scala @@ -28,7 +28,7 @@ class ZstdSpec extends AnyFlatSpec with ScalaCheckPropertyChecks { // Assumes that `Zstd.defaultCompressionLevel() == 3`. val compressed = if (level != 3) Zstd.compress(input, level) else Zstd.compress(input) val decompressed= Zstd.decompress(compressed, size) - input.toSeq == decompressed.toSeq && size == Zstd.decompressedSize(compressed) + input.toSeq == decompressed.toSeq && size == Zstd.decompressedSize(compressed) && size == Zstd.getFrameContentSize(compressed) } } } @@ -45,7 +45,7 @@ class ZstdSpec extends AnyFlatSpec with ScalaCheckPropertyChecks { if (Zstd.isError(csize)) sys.error(Zstd.getErrorName(csize)) val dsize = Zstd.decompressByteArray(decompressed, 0, size, compressed, 0, csize.toInt) if (Zstd.isError(dsize)) sys.error(Zstd.getErrorName(dsize)) - size == dsize && input.toSeq == decompressed.toSeq && size == Zstd.decompressedSize(compressed) + size == dsize && input.toSeq == decompressed.toSeq && size == Zstd.decompressedSize(compressed) && size == Zstd.getFrameContentSize(compressed) } } } @@ -73,7 +73,7 @@ class ZstdSpec extends AnyFlatSpec with ScalaCheckPropertyChecks { decompressedBuffer.flip() val comparison = inputBuffer.compareTo(decompressedBuffer) - val result = comparison == 0 && Zstd.decompressedSize(compressedBuffer) == decompressedSize + val result = comparison == 0 && Zstd.decompressedSize(compressedBuffer) == decompressedSize && Zstd.getFrameContentSize(compressedBuffer) == decompressedSize result } } @@ -1149,7 +1149,7 @@ class ZstdSpec extends AnyFlatSpec with ScalaCheckPropertyChecks { decompressedBuffer.flip() val comparison = inputBuffer.compareTo(decompressedBuffer) - comparison == 0 && Zstd.decompressedSize(compressedBuffer) == size + comparison == 0 && Zstd.decompressedSize(compressedBuffer) == size && Zstd.getFrameContentSize(compressedBuffer) == size } } }.get @@ -1167,6 +1167,7 @@ class ZstdSpec extends AnyFlatSpec with ScalaCheckPropertyChecks { val compressedMagicless = cctx.compress(input) assert(compressedMagicless.length == (compressedMagic.length - 4)) assert(input.length == Zstd.decompressedSize(compressedMagicless, 0, compressedMagicless.length, true)) + assert(input.length == Zstd.getFrameContentSize(compressedMagicless, 0, compressedMagicless.length, true)) dctx.reset() dctx.setMagicless(true)