Repository navigation
Expand file tree
/
Copy pathZstd.java
More file actions
1855 lines (1722 loc) · 78.9 KB
/
Copy pathZstd.java
File metadata and controls
1855 lines (1722 loc) · 78.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.github.luben.zstd;
import com.github.luben.zstd.util.Native;
import org.jetbrains.annotations.NotNull;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class Zstd {
private static final @NotNull String maxDecompressSizeOverride = "ZstdMaxDecompressSize";
/**
* Max memory size automatically allocated on decompression when no explicit content size is supplied.
*
* It can be controlled by the ZstdMaxDecompressSize property and defaults to 512MiB
*/
public static final long MAX_DECOMPRESS_SIZE;
static {
Native.load();
long configuredMax;
try {
String prop = System.getProperty(maxDecompressSizeOverride);
configuredMax = (prop != null) ? Long.parseLong(prop) : 512L * 1024L * 1024L;
} catch (NumberFormatException e) {
configuredMax = 512L * 1024L * 1024L;
}
MAX_DECOMPRESS_SIZE = configuredMax;
}
/**
* Note: This enum controls features which are conditionally beneficial.
* Zstd typically will make a final decision on whether to enable the
* feature ({@link AUTO}), but setting the switch to {@link ENABLE} or
* {@link DISABLE} allows for force enabling/disabling the feature.
*/
public static enum ParamSwitch {
/**
* Let the library automatically determine whether the feature shall be enabled
*/
AUTO(0),
/**
* Force-enable the feature
*/
ENABLE(1),
/**
* Do not use the feature
*/
DISABLE(2);
private int val;
ParamSwitch(int val) {
this.val = val;
}
public int getValue() {
return val;
}
}
/**
* Compresses buffer 'src' into buffer 'dst'.
*
* Destination buffer should be sized to handle worst cases situations (input
* data not compressible). Worst case size evaluation is provided by function
* ZSTD_compressBound().
*
* @param dst the destination buffer
* @param src the source buffer
* @param level compression level
* @param checksumFlag flag to enable or disable checksum
* @return the number of bytes written into buffer 'dst' or an error code if
* it fails (which can be tested using ZSTD_isError())
*/
public static long compress(byte @NotNull [] dst, byte @NotNull [] src, int level, boolean checksumFlag) {
ZstdCompressCtx ctx = new ZstdCompressCtx();
try {
ctx.setLevel(level);
ctx.setChecksum(checksumFlag);
return (long) ctx.compress(dst, src);
} finally {
ctx.close();
}
}
/**
* Compresses buffer 'src' into buffer 'dst'.
*
* Destination buffer should be sized to handle worst cases situations (input
* data not compressible). Worst case size evaluation is provided by function
* ZSTD_compressBound().
*
* @param dst the destination buffer
* @param src the source buffer
* @param level compression level
* @return the number of bytes written into buffer 'dst' or an error code if
* it fails (which can be tested using ZSTD_isError())
*/
public static long compress(byte @NotNull [] dst, byte @NotNull [] src, int level) {
return compress(dst, src, level, false);
}
/**
* Compresses buffer 'src' into buffer 'dst'.
*
* Destination buffer should be sized to handle worst cases situations (input
* data not compressible). Worst case size evaluation is provided by function
* ZSTD_compressBound().
*
* @param dst the destination buffer
* @param dstOffset offset from the start of the destination buffer
* @param dstSize available space in the destination buffer after the offset
* @param src the source buffer
* @param srcOffset offset from the start of the source buffer
* @param srcSize available data in the source buffer after the offset
* @param level compression level
* @param checksumFlag flag to enable or disable checksum
* @return the number of bytes written into buffer 'dst' or an error code if
* it fails (which can be tested using ZSTD_isError())
*/
public static long compressByteArray(byte @NotNull [] dst, int dstOffset, int dstSize, byte @NotNull [] src, int srcOffset, int srcSize, int level, boolean checksumFlag) {
ZstdCompressCtx ctx = new ZstdCompressCtx();
try {
ctx.setLevel(level);
ctx.setChecksum(checksumFlag);
return (long) ctx.compressByteArray(dst, dstOffset, dstSize, src, srcOffset, srcSize);
} finally {
ctx.close();
}
}
/**
* Compresses buffer 'src' into buffer 'dst'.
*
* Destination buffer should be sized to handle worst cases situations (input
* data not compressible). Worst case size evaluation is provided by function
* ZSTD_compressBound().
*
* @param dst the destination buffer
* @param dstOffset offset from the start of the destination buffer
* @param dstSize available space in the destination buffer after the offset
* @param src the source buffer
* @param srcOffset offset from the start of the source buffer
* @param srcSize available data in the source buffer after the offset
* @param level compression level
* @return the number of bytes written into buffer 'dst' or an error code if
* it fails (which can be tested using ZSTD_isError())
*/
public static long compressByteArray(byte @NotNull [] dst, int dstOffset, int dstSize, byte @NotNull [] src, int srcOffset, int srcSize, int level) {
return compressByteArray(dst, dstOffset, dstSize, src, srcOffset, srcSize, level, false);
}
/**
* Compresses direct buffer 'src' into direct buffer 'dst'.
*
* Destination buffer should be sized to handle worst cases situations (input
* data not compressible). Worst case size evaluation is provided by function
* ZSTD_compressBound().
*
* @param dst the destination buffer
* @param dstOffset offset from the start of the destination buffer
* @param dstSize available space in the destination buffer after the offset
* @param src the source buffer
* @param srcOffset offset from the start of the source buffer
* @param srcSize available data in the source buffer after the offset
* @param level compression level
* @param checksumFlag flag to enable or disable checksum
* @return the number of bytes written into buffer 'dst' or an error code if
* it fails (which can be tested using ZSTD_isError())
*/
public static long compressDirectByteBuffer(@NotNull ByteBuffer dst, int dstOffset, int dstSize, @NotNull ByteBuffer src, int srcOffset, int srcSize, int level, boolean checksumFlag) {
ZstdCompressCtx ctx = new ZstdCompressCtx();
try {
ctx.setLevel(level);
ctx.setChecksum(checksumFlag);
return (long) ctx.compressDirectByteBuffer(dst, dstOffset, dstSize, src, srcOffset, srcSize);
} finally {
ctx.close();
}
}
/**
* Compresses direct buffer 'src' into direct buffer 'dst'.
*
* Destination buffer should be sized to handle worst cases situations (input
* data not compressible). Worst case size evaluation is provided by function
* ZSTD_compressBound().
*
* @param dst the destination buffer
* @param dstOffset offset from the start of the destination buffer
* @param dstSize available space in the destination buffer after the offset
* @param src the source buffer
* @param srcOffset offset from the start of the source buffer
* @param srcSize available data in the source buffer after the offset
* @param level compression level
* @return the number of bytes written into buffer 'dst' or an error code if
* it fails (which can be tested using ZSTD_isError())
*/
public static long compressDirectByteBuffer(@NotNull ByteBuffer dst, int dstOffset, int dstSize, @NotNull ByteBuffer src, int srcOffset, int srcSize, int level) {
return compressDirectByteBuffer(dst, dstOffset, dstSize, src, srcOffset, srcSize, level, false);
}
/**
* Compresses buffer 'src' into direct buffer 'dst'.
*
* Destination buffer should be sized to handle worst cases situations (input
* data not compressible). Worst case size evaluation is provided by function
* ZSTD_compressBound().
*
* @param dst pointer to the destination buffer
* @param dstSize available space in the destination buffer
* @param src pointer to the source buffer
* @param srcSize available data in the source buffer
* @param level compression level
* @param checksumFlag flag to enable or disable checksum
* @return the number of bytes written into buffer 'dst' or an error code if
* it fails (which can be tested using ZSTD_isError())
*/
public static native long compressUnsafe(long dst, long dstSize, long src, long srcSize, int level, boolean checksumFlag);
/**
* Compresses buffer 'src' into direct buffer 'dst'.
*
* Destination buffer should be sized to handle worst cases situations (input
* data not compressible). Worst case size evaluation is provided by function
* ZSTD_compressBound().
*
* @param dst pointer to the destination buffer
* @param dstSize available space in the destination buffer
* @param src pointer to the source buffer
* @param srcSize available data in the source buffer
* @param level compression level
* @return the number of bytes written into buffer 'dst' or an error code if
* it fails (which can be tested using ZSTD_isError())
*/
public static long compressUnsafe(long dst, long dstSize, long src, long srcSize, int level) {
return compressUnsafe(dst, dstSize, src, srcSize, level, false);
}
/**
* Compresses buffer 'src' into buffer 'dst' with dictionary.
*
* Destination buffer should be sized to handle worst cases situations (input
* data not compressible). Worst case size evaluation is provided by function
* ZSTD_compressBound().
*
* @param dst the destination buffer
* @param dstOffset the start offset of 'dst'
* @param src the source buffer
* @param srcOffset the start offset of 'src'
* @param length the length of available data in 'src' after `srcOffset'
* @param dict the dictionary buffer
* @param level compression level
* @return the number of bytes written into buffer 'dst' or an error code if
* it fails (which can be tested using ZSTD_isError())
*/
public static long compressUsingDict (byte @NotNull [] dst, int dstOffset, byte @NotNull [] src, int srcOffset, int length, byte @NotNull [] dict, int level) {
ZstdCompressCtx ctx = new ZstdCompressCtx();
try {
ctx.setLevel(level);
ctx.loadDict(dict);
return (long) ctx.compressByteArray(dst, dstOffset, dst.length - dstOffset, src, srcOffset, length);
} finally {
ctx.close();
}
}
/**
* Compresses buffer 'src' into buffer 'dst' with dictionary.
*
* Destination buffer should be sized to handle worst cases situations (input
* data not compressible). Worst case size evaluation is provided by function
* ZSTD_compressBound().
*
* @param dst the destination buffer
* @param dstOffset the start offset of 'dst'
* @param src the source buffer
* @param srcOffset the start offset of 'src'
* @param dict the dictionary buffer
* @param level compression level
* @return the number of bytes written into buffer 'dst' or an error code if
* it fails (which can be tested using ZSTD_isError())
*/
public static long compressUsingDict (byte @NotNull [] dst, int dstOffset, byte @NotNull [] src, int srcOffset, byte @NotNull [] dict, int level) {
ZstdCompressCtx ctx = new ZstdCompressCtx();
try {
ctx.setLevel(level);
ctx.loadDict(dict);
return (long) ctx.compressByteArray(dst, dstOffset, dst.length - dstOffset, src, srcOffset, src.length - srcOffset);
} finally {
ctx.close();
}
}
/**
* Compresses direct byte buffer 'src' into direct byte buffer 'dst' with dictionary.
*
* Destination buffer should be sized to handle worst cases situations (input
* data not compressible). Worst case size evaluation is provided by function
* ZSTD_compressBound().
*
* @param dst the destination buffer
* @param dstOffset the start offset of 'dst'
* @param dstSize size of 'dst'
* @param src the source buffer
* @param srcOffset the start offset of 'src'
* @param srcSize the length of 'src'
* @param dict the dictionary buffer
* @param level compression level
* @return the number of bytes written into buffer 'dst' or an error code if
* it fails (which can be tested using ZSTD_isError())
*/
public static long compressDirectByteBufferUsingDict(@NotNull ByteBuffer dst, int dstOffset, int dstSize, @NotNull ByteBuffer src, int srcOffset, int srcSize, byte @NotNull [] dict, int level) {
ZstdCompressCtx ctx = new ZstdCompressCtx();
try {
ctx.setLevel(level);
ctx.loadDict(dict);
return (long) ctx.compressDirectByteBuffer(dst, dstOffset, dstSize, src, srcOffset, srcSize);
} finally {
ctx.close();
}
}
/**
* Compresses buffer 'src' into buffer 'dst' with dictionary.
*
* Destination buffer should be sized to handle worst cases situations (input
* data not compressible). Worst case size evaluation is provided by function
* ZSTD_compressBound().
*
* @param dst the destination buffer
* @param dstOffset the start offset of 'dst'
* @param src the source buffer
* @param srcOffset the start offset of 'src'
* @param length the length of available data in 'src' after `srcOffset'
* @param dict the dictionary
* @return the number of bytes written into buffer 'dst' or an error code if
* it fails (which can be tested using ZSTD_isError())
*/
public static long compressFastDict(byte @NotNull [] dst, int dstOffset, byte @NotNull [] src, int srcOffset, int length, @NotNull ZstdDictCompress dict) {
ZstdCompressCtx ctx = new ZstdCompressCtx();
try {
ctx.loadDict(dict);
ctx.setLevel(dict.level());
return (long) ctx.compressByteArray(dst, dstOffset, dst.length - dstOffset, src, srcOffset, length);
} finally {
ctx.close();
}
}
/**
* Compresses buffer 'src' into buffer 'dst' with dictionary.
*
* Destination buffer should be sized to handle worst cases situations (input
* data not compressible). Worst case size evaluation is provided by function
* ZSTD_compressBound().
*
* @param dst the destination buffer
* @param dstOffset the start offset of 'dst'
* @param src the source buffer
* @param srcOffset the start offset of 'src'
* @param dict the dictionary
* @return the number of bytes written into buffer 'dst' or an error code if
* it fails (which can be tested using ZSTD_isError())
*/
public static long compressFastDict(byte @NotNull [] dst, int dstOffset, byte @NotNull [] src, int srcOffset, @NotNull ZstdDictCompress dict) {
ZstdCompressCtx ctx = new ZstdCompressCtx();
try {
ctx.loadDict(dict);
ctx.setLevel(dict.level());
return (long) ctx.compressByteArray(dst, dstOffset, dst.length - dstOffset, src, srcOffset, src.length - srcOffset);
} finally {
ctx.close();
}
}
public static long compress(byte @NotNull [] dst, byte @NotNull [] src, @NotNull ZstdDictCompress dict) {
ZstdCompressCtx ctx = new ZstdCompressCtx();
try {
ctx.loadDict(dict);
ctx.setLevel(dict.level());
return (long) ctx.compress(dst, src);
} finally {
ctx.close();
}
}
/**
* Compresses buffer 'src' into buffer 'dst' with dictionary.
*
* Destination buffer should be sized to handle worst cases situations (input
* data not compressible). Worst case size evaluation is provided by function
* ZSTD_compressBound().
*
* @param dst the destination buffer
* @param dstOffset the start offset of 'dst'
* @param dstSize the size of 'dst'
* @param src the source buffer
* @param srcOffset the start offset of 'src'
* @param srcSize the length of 'src'
* @param dict the dictionary
* @return the number of bytes written into buffer 'dst' or an error code if
* it fails (which can be tested using ZSTD_isError())
*/
public static long compressDirectByteBufferFastDict(@NotNull ByteBuffer dst, int dstOffset, int dstSize, @NotNull ByteBuffer src, int srcOffset, int srcSize, @NotNull ZstdDictCompress dict) {
ZstdCompressCtx ctx = new ZstdCompressCtx();
try {
ctx.loadDict(dict);
ctx.setLevel(dict.level());
return (long) ctx.compressDirectByteBuffer(dst, dstOffset, dstSize, src, srcOffset, srcSize);
} finally {
ctx.close();
}
}
/**
* Decompresses buffer 'src' into buffer 'dst'.
*
* Destination buffer should be sized to be larger of equal to the originalSize
*
* @param dst the destination buffer
* @param src the source buffer
* @return the number of bytes decompressed into destination buffer (originalSize)
* or an errorCode if it fails (which can be tested using ZSTD_isError())
*
*/
public static long decompress(byte @NotNull [] dst, byte @NotNull [] src) {
ZstdDecompressCtx ctx = new ZstdDecompressCtx();
try {
return (long) ctx.decompress(dst, src);
} finally {
ctx.close();
}
}
public static int decompress(byte @NotNull [] dst, @NotNull ByteBuffer srcBuf) {
ZstdDecompressCtx ctx = new ZstdDecompressCtx();
try {
return ctx.decompress(dst, srcBuf);
} finally {
ctx.close();
}
}
/**
* Decompresses buffer 'src' into buffer 'dst'.
*
* Destination buffer should be sized to be larger of equal to the originalSize
*
* @param dst the destination buffer
* @param dstOffset offset from the start of the destination buffer
* @param dstSize available space in the destination buffer after the offset
* @param src the source buffer
* @param srcOffset offset from the start of the source buffer
* @param srcSize available data in the source buffer after the offset
* @return the number of bytes decompressed into destination buffer (originalSize)
* or an errorCode if it fails (which can be tested using ZSTD_isError())
*
*/
public static long decompressByteArray(byte @NotNull [] dst, int dstOffset, int dstSize, byte @NotNull [] src, int srcOffset, int srcSize) {
ZstdDecompressCtx ctx = new ZstdDecompressCtx();
try {
return (long) ctx.decompressByteArray(dst, dstOffset, dstSize, src, srcOffset, srcSize);
} finally {
ctx.close();
}
}
/**
* Decompresses direct buffer 'src' into direct buffer 'dst'.
*
* Destination buffer should be sized to be larger of equal to the originalSize
*
* @param dst the destination buffer
* @param dstOffset offset from the start of the destination buffer
* @param dstSize available space in the destination buffer after the offset
* @param src the source buffer
* @param srcOffset offset from the start of the source buffer
* @param srcSize available data in the source buffer after the offset
*
* @return the number of bytes decompressed into destination buffer (originalSize)
* or an errorCode if it fails (which can be tested using ZSTD_isError())
*
*/
public static long decompressDirectByteBuffer(@NotNull ByteBuffer dst, int dstOffset, int dstSize, @NotNull ByteBuffer src, int srcOffset, int srcSize) {
ZstdDecompressCtx ctx = new ZstdDecompressCtx();
try {
return (long) ctx.decompressDirectByteBuffer(dst, dstOffset, dstSize, src, srcOffset, srcSize);
} finally {
ctx.close();
}
}
/**
* Decompresses buffer 'src' into direct buffer 'dst'.
*
* Destination buffer should be sized to be larger of equal to the originalSize
*
* @param dst pointer to the destination buffer
* @param dstSize available space in the destination buffer after the offset
* @param src pointer the source buffer
* @param srcSize available data in the source buffer after the offset
*
* @return the number of bytes decompressed into destination buffer (originalSize)
* or an errorCode if it fails (which can be tested using ZSTD_isError())
*
*/
public static native long decompressUnsafe(long dst, long dstSize, long src, long srcSize);
/**
* Decompresses buffer 'src' into buffer 'dst' with dictionary.
*
* Destination buffer should be sized to be larger of equal to the originalSize
*
* @param dst the destination buffer
* @param dstOffset the start offset of 'dst'
* @param src the source buffer
* @param srcOffset the start offset of 'src'
* @param length the length of 'src'
* @param dict the dictionary buffer
* @return the number of bytes decompressed into destination buffer (originalSize)
* or an errorCode if it fails (which can be tested using ZSTD_isError())
*
*/
public static long decompressUsingDict(byte @NotNull [] dst, int dstOffset, byte @NotNull [] src, int srcOffset, int length, byte @NotNull [] dict) {
ZstdDecompressCtx ctx = new ZstdDecompressCtx();
try {
ctx.loadDict(dict);
return (long) ctx.decompressByteArray(dst, dstOffset, dst.length - dstOffset, src, srcOffset, length);
} finally {
ctx.close();
}
}
/**
* Decompresses buffer 'src' into buffer 'dst' with dictionary.
*
* Destination buffer should be sized to be larger of equal to the originalSize
*
* @param dst the destination buffer
* @param dstOffset the start offset of 'dst'
* @param dstSize size of 'dst'
* @param src the source buffer
* @param srcOffset the start offset of 'src'
* @param srcSize the size of 'src'
* @param dict the dictionary buffer
* @return the number of bytes decompressed into destination buffer (originalSize)
* or an errorCode if it fails (which can be tested using ZSTD_isError())
*
*/
public static long decompressDirectByteBufferUsingDict(@NotNull ByteBuffer dst, int dstOffset, int dstSize, @NotNull ByteBuffer src, int srcOffset, int srcSize, byte @NotNull [] dict) {
ZstdDecompressCtx ctx = new ZstdDecompressCtx();
try {
ctx.loadDict(dict);
return (long) ctx.decompressDirectByteBuffer(dst, dstOffset, dstSize, src, srcOffset, srcSize);
} finally {
ctx.close();
}
}
/**
* Decompresses buffer 'src' into buffer 'dst' with dictionary.
*
* Destination buffer should be sized to be larger of equal to the originalSize
*
* @param dst the destination buffer
* @param dstOffset the start offset of 'dst'
* @param src the source buffer
* @param srcOffset the start offset of 'src'
* @param length the length of 'src'
* @param dict the dictionary
* @return the number of bytes decompressed into destination buffer (originalSize)
* or an errorCode if it fails (which can be tested using ZSTD_isError())
*
*/
public static long decompressFastDict(byte @NotNull [] dst, int dstOffset, byte @NotNull [] src, int srcOffset, int length, @NotNull ZstdDictDecompress dict) {
ZstdDecompressCtx ctx = new ZstdDecompressCtx();
try {
ctx.loadDict(dict);
return (long) ctx.decompressByteArray(dst, dstOffset, dst.length - dstOffset, src, srcOffset, length);
} finally {
ctx.close();
}
}
/**
* Decompresses buffer 'src' into buffer 'dst' with dictionary.
*
* Destination buffer should be sized to be larger of equal to the originalSize
*
* @param dst the destination buffer
* @param dstOffset the start offset of 'dst'
* @param dstSize the size of 'dst'
* @param src the source buffer
* @param srcOffset the start offset of 'src'
* @param srcSize the size of 'src'
* @param dict the dictionary
* @return the number of bytes decompressed into destination buffer (originalSize)
* or an errorCode if it fails (which can be tested using ZSTD_isError())
*
*/
public static long decompressDirectByteBufferFastDict(@NotNull ByteBuffer dst, int dstOffset, int dstSize, @NotNull ByteBuffer src, int srcOffset, int srcSize, @NotNull ZstdDictDecompress dict) {
ZstdDecompressCtx ctx = new ZstdDecompressCtx();
try {
ctx.loadDict(dict);
return (long) ctx.decompressDirectByteBuffer(dst, dstOffset, dstSize, src, srcOffset, srcSize);
} finally {
ctx.close();
}
}
/* Advance API */
public static native int loadDictDecompress(long stream, byte @NotNull [] dict, int dict_size);
public static native int loadFastDictDecompress(long stream, @NotNull ZstdDictDecompress dict);
public static native int loadDictCompress(long stream, byte @NotNull [] dict, int dict_size);
public static native int loadFastDictCompress(long stream, @NotNull ZstdDictCompress dict);
public static native void registerSequenceProducer(long stream, long seqProdState, long seqProdFunction);
public static native int setCompressionChecksums(long stream, boolean useChecksums);
public static native int setCompressionMagicless(long stream, boolean useMagicless);
public static native int setCompressionLevel(long stream, int level);
public static native int setCompressionLong(long stream, int windowLog);
public static native int setCompressionWorkers(long stream, int workers);
public static native int setCompressionOverlapLog(long stream, int overlapLog);
public static native int setCompressionJobSize(long stream, int jobSize);
public static native int setCompressionTargetLength(long stream, int targetLength);
public static native int setCompressionMinMatch(long stream, int minMatch);
public static native int setCompressionSearchLog(long stream, int searchLog);
public static native int setCompressionChainLog(long stream, int chainLog);
public static native int setCompressionHashLog(long stream, int hashLog);
public static native int setCompressionWindowLog(long stream, int windowLog);
public static native int setCompressionStrategy(long stream, int strategy);
public static native int setDecompressionLongMax(long stream, int windowLogMax);
public static native int setDecompressionMagicless(long stream, boolean useMagicless);
public static native int setRefMultipleDDicts(long stream, boolean useMultiple);
public static native int setValidateSequences(long stream, int validateSequences);
public static native int setSequenceProducerFallback(long stream, boolean fallbackFlag);
public static native int setSearchForExternalRepcodes(long stream, int searchRepcodes);
public static native int setEnableLongDistanceMatching(long stream, int enableLDM);
/* Utility methods */
/**
* Return the compressed size of a frame within a buffer.
*
* @param src the compressed buffer
* @param srcPosition offset of the compressed frame inside the src buffer
* @param srcSize length of the compressed data inside the src buffer
* @return the number of bytes of the compressed frame
* @throws ZstdException if there is an error decoding the frame
*/
public static long findFrameCompressedSize(byte @NotNull [] src, int srcPosition, int srcSize) {
if (srcPosition < 0 || srcPosition >= src.length) {
throw new ArrayIndexOutOfBoundsException(srcPosition);
}
if (srcSize < 0 || srcSize > src.length - srcPosition) {
throw new ArrayIndexOutOfBoundsException(srcPosition + srcSize);
}
long size = findFrameCompressedSize0(src, srcPosition, srcSize);
if (Zstd.isError(size)) {
throw new ZstdException(size);
}
return size;
}
private static native long findFrameCompressedSize0(byte @NotNull [] src, int srcPosition, int srcSize);
/**
* Return the compressed size of a frame within a buffer.
*
* @param src the compressed buffer
* @param srcPosition offset of the compressed frame inside the src buffer
* @return the number of bytes of the compressed frame
* negative if there is an error decoding the frame header
*/
public static long findFrameCompressedSize(byte @NotNull [] src, int srcPosition) {
return findFrameCompressedSize(src, srcPosition, src.length - srcPosition);
}
/**
* Return the compressed size of a frame within a buffer.
*
* @param src the compressed buffer
* @return the number of bytes of the compressed frame
* negative if there is an error decoding the frame header
*/
public static long findFrameCompressedSize(byte @NotNull [] src) {
return findFrameCompressedSize(src, 0);
}
/**
* Return the compressed size of a frame within a buffer.
*
* @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 compressed frame
* negative if there is an error decoding the frame header
*/
public static long findFrameCompressedSize(@NotNull ByteBuffer srcBuf) {
return findDirectByteBufferFrameCompressedSize(srcBuf, srcBuf.position(), srcBuf.limit() - srcBuf.position());
}
/**
* Return the compressed size of a frame within a buffer.
*
* @param src the compressed buffer
* @param srcPosition offset of the compressed data inside the src buffer
* @param srcSize length of the compressed data inside the src buffe
* @return the number of bytes of the compressed frame
* negative if there is an error decoding the frame header
*/
public static native long findDirectByteBufferFrameCompressedSize(@NotNull ByteBuffer src, int srcPosition, int srcSize);
/**
* Return the original size of a compressed buffer (if known)
*
* @param src the compressed buffer
* @param srcPosition offset of the compressed data inside the src buffer
* @param srcSize length of the compressed data inside the src buffer
* @param magicless whether the buffer contains a magicless frame
* @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(byte @NotNull [] src, int srcPosition, int srcSize, boolean magicless) {
if (srcPosition < 0 || srcPosition >= src.length) {
throw new ArrayIndexOutOfBoundsException(srcPosition);
}
if (srcSize < 0 || srcSize > src.length - srcPosition) {
throw new ArrayIndexOutOfBoundsException(srcPosition + srcSize);
}
return getFrameContentSize0(src, srcPosition, srcSize, magicless);
}
private static native long getFrameContentSize0(byte @NotNull [] src, int srcPosition, int srcSize, boolean magicless);
/**
* Return the original size of a compressed buffer (if known)
*
* @param src the compressed buffer
* @param srcPosition offset of the compressed data inside the src buffer
* @param srcSize length of the compressed data inside the src buffer
* @param magicless whether the buffer contains a magicless frame
* @return the number of bytes of the original buffer
* 0 if the original size is not known
* @deprecated
* Use `getFrameContentSize` to also return error codes from zstd
*/
@Deprecated
public static long decompressedSize(byte @NotNull [] src, int srcPosition, int srcSize, boolean magicless) {
if (srcPosition < 0 || srcPosition >= src.length) {
throw new ArrayIndexOutOfBoundsException(srcPosition);
}
if (srcSize < 0 || srcSize > src.length - srcPosition) {
throw new ArrayIndexOutOfBoundsException(srcPosition + srcSize);
}
return decompressedSize0(src, srcPosition, srcSize, magicless);
}
private static native long decompressedSize0(byte @NotNull [] src, int srcPosition, int srcSize, boolean magicless);
/**
* Return the original size of a compressed buffer (if known)
*
* @param src the compressed buffer
* @param srcPosition offset of the compressed data inside the src buffer
* @param srcSize length of the compressed data inside the src buffer
* @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(byte @NotNull [] src, int srcPosition, int srcSize) {
return getFrameContentSize(src, srcPosition, srcSize, false);
}
/**
* Return the original size of a compressed buffer (if known)
*
* @param src the compressed buffer
* @param srcPosition offset of the compressed data inside the src buffer
* @param srcSize length of the compressed data inside the src buffer
* @return the number of bytes of the original buffer
* 0 if the original size is not known
* @deprecated
* Use `getFrameContentSize` to also return error codes from zstd
*/
@Deprecated
public static long decompressedSize(byte @NotNull [] src, int srcPosition, int srcSize) {
return decompressedSize(src, srcPosition, srcSize, false);
}
/**
* Return the original size of a compressed buffer (if known)
*
* @param src the compressed buffer
* @param srcPosition offset of the compressed data inside the src buffer
* @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(byte @NotNull [] src, int srcPosition) {
return getFrameContentSize(src, srcPosition, src.length - srcPosition);
}
/**
* Return the original size of a compressed buffer (if known)
*
* @param src the compressed buffer
* @param srcPosition offset of the compressed data inside the src buffer
* @return the number of bytes of the original buffer
* 0 if the original size is not known
* @deprecated
* Use `getFrameContentSize` to also return error codes from zstd
*/
@Deprecated
public static long decompressedSize(byte @NotNull [] src, int srcPosition) {
return decompressedSize(src, srcPosition, src.length - srcPosition);
}
/**
* Return the original size of a compressed buffer (if known)
*
* @param src the compressed buffer
* @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(byte @NotNull [] src) {
return getFrameContentSize(src, 0);
}
/**
* Return the original size of a compressed buffer (if known)
*
* @param src the compressed buffer
* @return the number of bytes of the original buffer
* 0 if the original size is not known
* @deprecated
* Use `getFrameContentSize` to also return error codes from zstd
*/
@Deprecated
public static long decompressedSize(byte @NotNull [] src) {
return decompressedSize(src, 0);
}
/**
* Return the original size of a compressed buffer (if known)
*
* @param src the compressed buffer
* @param srcPosition offset of the compressed data inside the src buffer
* @param srcSize length of the compressed data inside the src buffe
* @param magicless whether the buffer contains a magicless frame
* @return the number of bytes of the original buffer
* 0 if the original size is not known
* @deprecated
* Use `getDirectByteBufferFrameContentSize` to also return error codes from zstd
*/
@Deprecated
public static native long decompressedDirectByteBufferSize(@NotNull ByteBuffer src, int srcPosition, int srcSize, boolean magicless);
/**
* Return the original size of a compressed buffer (if known)
*
* @param src the compressed buffer
* @param srcPosition offset of the compressed data inside the src buffer
* @param srcSize length of the compressed data inside the src buffe
* @param magicless whether the buffer contains a magicless frame
* @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 native long getDirectByteBufferFrameContentSize(@NotNull ByteBuffer src, int srcPosition, int srcSize, boolean magicless);
/**
* Return the original size of a compressed buffer (if known)
*
* @param src the compressed buffer
* @param srcPosition offset of the compressed data inside the src buffer
* @param srcSize length of the compressed data inside the src buffe
* @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 decompressedDirectByteBufferSize(@NotNull ByteBuffer src, int srcPosition, int srcSize) {
return decompressedDirectByteBufferSize(src, srcPosition, srcSize, false);
}
/**
* Return the original size of a compressed buffer (if known)
*
* @param src the compressed buffer
* @param srcPosition offset of the compressed data inside the src buffer
* @param srcSize length of the compressed data inside the src buffe
* @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 getDirectByteBufferFrameContentSize(@NotNull ByteBuffer src, int srcPosition, int srcSize) {
return getDirectByteBufferFrameContentSize(src, srcPosition, srcSize, false);
}
/**
* Maximum size of the compressed data
*
* @param srcSize the size of the data to be compressed
* @return the maximum size of the compressed data
*/
public static native long compressBound(long srcSize);
/**
* Error handling
*
* @param code return code/size
* @return if the return code signals an error
*/
public static native boolean isError(long code);
public static native @NotNull String getErrorName(long code);
public static native long getErrorCode(long code);
/* Stable constants from the zstd_errors header */
public static native long errNoError();
public static native long errGeneric();
public static native long errPrefixUnknown();
public static native long errVersionUnsupported();
public static native long errFrameParameterUnsupported();
public static native long errFrameParameterWindowTooLarge();
public static native long errCorruptionDetected();
public static native long errChecksumWrong();
public static native long errDictionaryCorrupted();
public static native long errDictionaryWrong();
public static native long errDictionaryCreationFailed();
public static native long errParameterUnsupported();
public static native long errParameterOutOfBound();
public static native long errTableLogTooLarge();
public static native long errMaxSymbolValueTooLarge();
public static native long errMaxSymbolValueTooSmall();
public static native long errStageWrong();
public static native long errInitMissing();
public static native long errMemoryAllocation();
public static native long errWorkSpaceTooSmall();
public static native long errDstSizeTooSmall();
public static native long errSrcSizeWrong();
public static native long errDstBufferNull();
/**
* Creates a new dictionary to tune a kind of samples
*
* @param samples the samples buffer array
* @param dictBuffer the new dictionary buffer
* @param legacy use the legacy training algorithm; otherwise cover
* @return the number of bytes into buffer 'dictBuffer' or an error code if
* it fails (which can be tested using ZSTD_isError())
*/
public static long trainFromBuffer(byte @NotNull [][] samples, byte @NotNull [] dictBuffer, boolean legacy) {
return trainFromBuffer(samples, dictBuffer, legacy, defaultCompressionLevel());
}
/**
* Creates a new dictionary to tune a kind of samples
*
* @param samples the samples buffer array
* @param dictBuffer the new dictionary buffer
* @param legacy use the legacy training algorithm; otherwise cover
* @param compressionLevel optimal if using the same level as when compressing.
* @return the number of bytes into buffer 'dictBuffer' or an error code if
* it fails (which can be tested using ZSTD_isError())
*/
public static long trainFromBuffer(byte @NotNull [][] samples, byte @NotNull [] dictBuffer, boolean legacy, int compressionLevel) {
Objects.requireNonNull(samples, "samples");
Objects.requireNonNull(dictBuffer, "dictBuffer");
for (byte[] sample : samples) {
Objects.requireNonNull(sample, "sample");
}
if (samples.length <= 10) {
throw new ZstdException(Zstd.errGeneric(), "nb of samples too low");
}
// FASTCOVER trains on the first ~75% of samples (d defaults to 8); if those sum to
// fewer than 8 bytes, nbDmers underflows in the vendored fastcover.c (meta/zstd#4499).
if (!legacy) {
int nbTrain = (int) (samples.length * 0.75);
long trainSize = 0;
for (int i = 0; i < nbTrain; i++) trainSize += samples[i].length;
if (trainSize < 8) {
throw new ZstdException(Zstd.errGeneric(), "training samples too small");
}
}
return trainFromBuffer0(samples, dictBuffer, legacy, compressionLevel);
}
private static native long trainFromBuffer0(byte @NotNull [][] samples, byte @NotNull [] dictBuffer, boolean legacy, int compressionLevel);
/**
* Creates a new dictionary to tune a kind of samples