Skip to content

Commit 051e8de

Browse files
danila-schelkovluben
authored andcommitted
refactor: throwing an exception on findFrameCompressedSize0 error
1 parent ed71d28 commit 051e8de

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/main/java/com/github/luben/zstd/Zstd.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class Zstd {
1414

1515
/**
1616
* Note: This enum controls features which are conditionally beneficial.
17-
* Zstd typically will make a final decision on whether or not to enable the
17+
* Zstd typically will make a final decision on whether to enable the
1818
* feature ({@link AUTO}), but setting the switch to {@link ENABLE} or
1919
* {@link DISABLE} allows for force enabling/disabling the feature.
2020
*/
@@ -633,7 +633,7 @@ public static long decompressDirectByteBufferFastDict(ByteBuffer dst, int dstOff
633633
* @param srcPosition offset of the compressed frame inside the src buffer
634634
* @param srcSize length of the compressed data inside the src buffer
635635
* @return the number of bytes of the compressed frame
636-
* negative if there is an error decoding the frame header
636+
* @throws ZstdException if there is an error decoding the frame
637637
*/
638638
public static long findFrameCompressedSize(byte[] src, int srcPosition, int srcSize) {
639639
if (srcPosition >= src.length) {
@@ -642,7 +642,13 @@ public static long findFrameCompressedSize(byte[] src, int srcPosition, int srcS
642642
if (srcPosition + srcSize > src.length) {
643643
throw new ArrayIndexOutOfBoundsException(srcPosition + srcSize);
644644
}
645-
return findFrameCompressedSize0(src, srcPosition, srcSize);
645+
646+
long size = findFrameCompressedSize0(src, srcPosition, srcSize);
647+
if (Zstd.isError(size)) {
648+
throw new ZstdException(size);
649+
}
650+
651+
return size;
646652
}
647653

648654
private static native long findFrameCompressedSize0(byte[] src, int srcPosition, int srcSize);
@@ -824,7 +830,7 @@ public static long getFrameContentSize(byte[] src) {
824830
@Deprecated
825831
public static long decompressedSize(byte[] src) {
826832
return decompressedSize(src, 0);
827-
};
833+
}
828834

829835
/**
830836
* Return the original size of a compressed buffer (if known)
@@ -1684,9 +1690,9 @@ public static int decompress(ByteBuffer dstBuff, ByteBuffer srcBuff, ZstdDictDec
16841690
* </p>
16851691
* @param dict the dictionary used in the compression
16861692
* @param originalSize the maximum size of the uncompressed data
1687-
* @return A newly-allocated ByteBuffer containing the decompressed data. The position() of this buffer will be 0,
1688-
* and the limit() will be the size of the decompressed data. In other words the buffer is ready to be used for
1689-
* reading. Note that this is different behavior from the other decompress() overload which takes as a parameter
1693+
* @return A newly-allocated ByteBuffer containing the decompressed data. The position() of this buffer will be 0,
1694+
* and the limit() will be the size of the decompressed data. In other words the buffer is ready to be used for
1695+
* reading. Note that this is different behavior from the other decompress() overload which takes as a parameter
16901696
* the destination ByteBuffer.
16911697
*/
16921698
public static ByteBuffer decompress(ByteBuffer srcBuff, ZstdDictDecompress dict, int originalSize) {

0 commit comments

Comments
 (0)