Skip to content

Commit 3f6c55e

Browse files
committed
Restore decompressedSize behaviour...
and add new method `getFrameContentSize` that will return also the error.
1 parent e07f897 commit 3f6c55e

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed

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

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,28 @@ public static long decompressDirectByteBufferFastDict(ByteBuffer dst, int dstOff
579579
public static native int setRefMultipleDDicts(long stream, boolean useMultiple);
580580

581581
/* Utility methods */
582+
/**
583+
* Return the original size of a compressed buffer (if known)
584+
*
585+
* @param src the compressed buffer
586+
* @param srcPosition offset of the compressed data inside the src buffer
587+
* @param srcSize length of the compressed data inside the src buffer
588+
* @param magicless whether the buffer contains a magicless frame
589+
* @return the number of bytes of the original buffer
590+
* 0 if the original size is not known,
591+
* negative if there is an error decoding the frame header
592+
*/
593+
public static long getFrameContentSize(byte[] src, int srcPosition, int srcSize, boolean magicless) {
594+
if (srcPosition >= src.length) {
595+
throw new ArrayIndexOutOfBoundsException(srcPosition);
596+
}
597+
if (srcPosition + srcSize > src.length) {
598+
throw new ArrayIndexOutOfBoundsException(srcPosition + srcSize);
599+
}
600+
return getFrameContentSize0(src, srcPosition, srcSize, magicless);
601+
}
602+
603+
private static native long getFrameContentSize0(byte[] src, int srcPosition, int srcSize, boolean magicless);
582604

583605
/**
584606
* Return the original size of a compressed buffer (if known)
@@ -589,7 +611,10 @@ public static long decompressDirectByteBufferFastDict(ByteBuffer dst, int dstOff
589611
* @param magicless whether the buffer contains a magicless frame
590612
* @return the number of bytes of the original buffer
591613
* 0 if the original size is not known
614+
* @deprecated
615+
* Use `getFrameContentSize` that return also the errors
592616
*/
617+
@Deprecated
593618
public static long decompressedSize(byte[] src, int srcPosition, int srcSize, boolean magicless) {
594619
if (srcPosition >= src.length) {
595620
throw new ArrayIndexOutOfBoundsException(srcPosition);
@@ -602,6 +627,20 @@ public static long decompressedSize(byte[] src, int srcPosition, int srcSize, bo
602627

603628
private static native long decompressedSize0(byte[] src, int srcPosition, int srcSize, boolean magicless);
604629

630+
/**
631+
* Return the original size of a compressed buffer (if known)
632+
*
633+
* @param src the compressed buffer
634+
* @param srcPosition offset of the compressed data inside the src buffer
635+
* @param srcSize length of the compressed data inside the src buffer
636+
* @return the number of bytes of the original buffer
637+
* 0 if the original size is not known,
638+
* negative if there is an error decoding the frame header
639+
*/
640+
public static long getFrameContentSize(byte[] src, int srcPosition, int srcSize) {
641+
return getFrameContentSize(src, srcPosition, srcSize, false);
642+
}
643+
605644
/**
606645
* Return the original size of a compressed buffer (if known)
607646
*
@@ -610,30 +649,67 @@ public static long decompressedSize(byte[] src, int srcPosition, int srcSize, bo
610649
* @param srcSize length of the compressed data inside the src buffer
611650
* @return the number of bytes of the original buffer
612651
* 0 if the original size is not known
652+
* @deprecated
653+
* Use `getFrameContentSize` that return also the errors
613654
*/
655+
@Deprecated
614656
public static long decompressedSize(byte[] src, int srcPosition, int srcSize) {
615657
return decompressedSize(src, srcPosition, srcSize, false);
616658
}
617659

660+
/**
661+
* Return the original size of a compressed buffer (if known)
662+
*
663+
* @param src the compressed buffer
664+
* @param srcPosition offset of the compressed data inside the src buffer
665+
* @param srcSize length of the compressed data inside the src buffer
666+
* @return the number of bytes of the original buffer
667+
* 0 if the original size is not known,
668+
* negative if there is an error decoding the frame header
669+
*/
670+
public static long getFrameContentSize(byte[] src, int srcPosition) {
671+
return getFrameContentSize(src, srcPosition, src.length - srcPosition);
672+
}
673+
618674
/**
619675
* Return the original size of a compressed buffer (if known)
620676
*
621677
* @param src the compressed buffer
622678
* @param srcPosition offset of the compressed data inside the src buffer
623679
* @return the number of bytes of the original buffer
624680
* 0 if the original size is not known
681+
* @deprecated
682+
* Use `getFrameContentSize` that return also the errors
625683
*/
684+
@Deprecated
626685
public static long decompressedSize(byte[] src, int srcPosition) {
627686
return decompressedSize(src, srcPosition, src.length - srcPosition);
628687
}
629688

689+
/**
690+
* Return the original size of a compressed buffer (if known)
691+
*
692+
* @param src the compressed buffer
693+
* @param srcPosition offset of the compressed data inside the src buffer
694+
* @param srcSize length of the compressed data inside the src buffer
695+
* @return the number of bytes of the original buffer
696+
* 0 if the original size is not known,
697+
* negative if there is an error decoding the frame header
698+
*/
699+
public static long getFrameContentSize(byte[] src) {
700+
return getFrameContentSize(src, 0);
701+
}
702+
630703
/**
631704
* Return the original size of a compressed buffer (if known)
632705
*
633706
* @param src the compressed buffer
634707
* @return the number of bytes of the original buffer
635708
* 0 if the original size is not known
709+
* @deprecated
710+
* Use `getFrameContentSize` that return also the errors
636711
*/
712+
@Deprecated
637713
public static long decompressedSize(byte[] src) {
638714
return decompressedSize(src, 0);
639715
};
@@ -647,9 +723,25 @@ public static long decompressedSize(byte[] src) {
647723
* @param magicless whether the buffer contains a magicless frame
648724
* @return the number of bytes of the original buffer
649725
* 0 if the original size is not known
726+
* @deprecated
727+
* Use `getDirectByteBuferFrameContentSize` that return also the errors
650728
*/
729+
@Deprecated
651730
public static native long decompressedDirectByteBufferSize(ByteBuffer src, int srcPosition, int srcSize, boolean magicless);
652731

732+
/**
733+
* Return the original size of a compressed buffer (if known)
734+
*
735+
* @param src the compressed buffer
736+
* @param srcPosition offset of the compressed data inside the src buffer
737+
* @param srcSize length of the compressed data inside the src buffe
738+
* @param magicless whether the buffer contains a magicless frame
739+
* @return the number of bytes of the original buffer
740+
* 0 if the original size is not known
741+
* negative if there is an error decoding the frame header
742+
*/
743+
public static native long getDirectByteBuferFrameContentSize(ByteBuffer src, int srcPosition, int srcSize, boolean magicless);
744+
653745
/**
654746
* Return the original size of a compressed buffer (if known)
655747
*
@@ -658,11 +750,28 @@ public static long decompressedSize(byte[] src) {
658750
* @param srcSize length of the compressed data inside the src buffe
659751
* @return the number of bytes of the original buffer
660752
* 0 if the original size is not known
753+
* @deprecated
754+
* Use `getDirectByteBuferFrameContentSize` that return also the errors
661755
*/
756+
@Deprecated
662757
public static long decompressedDirectByteBufferSize(ByteBuffer src, int srcPosition, int srcSize) {
663758
return decompressedDirectByteBufferSize(src, srcPosition, srcSize, false);
664759
}
665760

761+
/**
762+
* Return the original size of a compressed buffer (if known)
763+
*
764+
* @param src the compressed buffer
765+
* @param srcPosition offset of the compressed data inside the src buffer
766+
* @param srcSize length of the compressed data inside the src buffe
767+
* @return the number of bytes of the original buffer
768+
* 0 if the original size is not known
769+
* negative if there is an error decoding the frame header
770+
*/
771+
public static long getDirectByteBuferFrameContentSize(ByteBuffer src, int srcPosition, int srcSize) {
772+
return getDirectByteBuferFrameContentSize(src, srcPosition, srcSize, false);
773+
}
774+
666775
/**
667776
* Maximum size of the compressed data
668777
*

src/main/native/jni_zstd.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,26 @@ JNIEXPORT jlong JNICALL Java_com_github_luben_zstd_Zstd_decompressedSize0
7474
if (src_buff == NULL) goto E1;
7575
size = JNI_ZSTD_decompressedSize(((char *) src_buff) + offset, (size_t) limit, magicless);
7676
(*env)->ReleasePrimitiveArrayCritical(env, src, src_buff, JNI_ABORT);
77+
if (size <= 0) return 0;
7778
E1: return size;
7879
}
7980

81+
/*
82+
* Class: com_github_luben_zstd_Zstd
83+
* Method: getFrameContentSize0
84+
* Signature: ([B)JII
85+
*/
86+
JNIEXPORT jlong JNICALL Java_com_github_luben_zstd_Zstd_getFrameContentSize0
87+
(JNIEnv *env, jclass obj, jbyteArray src, jint offset, jint limit, jboolean magicless) {
88+
size_t size = -ZSTD_error_memory_allocation;
89+
void *src_buff = (*env)->GetPrimitiveArrayCritical(env, src, NULL);
90+
if (src_buff == NULL) goto E1;
91+
size = JNI_ZSTD_decompressedSize(((char *) src_buff) + offset, (size_t) limit, magicless);
92+
(*env)->ReleasePrimitiveArrayCritical(env, src, src_buff, JNI_ABORT);
93+
E1: return size;
94+
}
95+
96+
8097
/*
8198
* Class: com_github_luben_zstd_Zstd
8299
* Method: getDictIdFromFrame
@@ -155,6 +172,23 @@ JNIEXPORT jlong JNICALL Java_com_github_luben_zstd_Zstd_decompressedDirectByteBu
155172
E1: return size;
156173
}
157174

175+
/*
176+
* Class: com_github_luben_zstd_Zstd
177+
* Method: getDirectByteBufferFrameContentSize
178+
* Signature: (Ljava/nio/ByteBuffer;II)J
179+
*/
180+
JNIEXPORT jlong JNICALL Java_com_github_luben_zstd_Zstd_getDirectByteBufferFrameContentSize
181+
(JNIEnv *env, jclass obj, jobject src_buf, jint src_offset, jint src_size, jboolean magicless) {
182+
size_t size = -ZSTD_error_memory_allocation;
183+
jsize src_cap = (*env)->GetDirectBufferCapacity(env, src_buf);
184+
if (src_offset + src_size > src_cap) return -ZSTD_error_GENERIC;
185+
char *src_buf_ptr = (char*)(*env)->GetDirectBufferAddress(env, src_buf);
186+
if (src_buf_ptr == NULL) goto E1;
187+
size = JNI_ZSTD_decompressedSize(src_buf_ptr + src_offset, (size_t) src_size, magicless);
188+
if (size <= 0) return 0;
189+
E1: return size;
190+
}
191+
158192
/*
159193
* Class: com_github_luben_zstd_Zstd
160194
* Method: compressBound

0 commit comments

Comments
 (0)