Skip to content

Commit

Permalink
Add tests, fix spelling and omissions
Browse files Browse the repository at this point in the history
  • Loading branch information
luben committed Sep 25, 2023
1 parent 3f6c55e commit dbb8bcd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
27 changes: 22 additions & 5 deletions src/main/java/com/github/luben/zstd/Zstd.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
Expand All @@ -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) {
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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
*
Expand Down
9 changes: 5 additions & 4 deletions src/test/scala/Zstd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand All @@ -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)
}
}
}
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit dbb8bcd

Please sign in to comment.