@@ -16,53 +16,55 @@ public class ZstdCompressCtx extends AutoCloseBase {
16
16
17
17
private ZstdDictCompress compression_dict = null ;
18
18
19
- private native void init ();
19
+ private static native long init ();
20
20
21
- private native void free ();
21
+ private static native void free (long ptr );
22
22
23
23
/**
24
24
* Create a context for faster compress operations
25
25
* One such context is required for each thread - put this in a ThreadLocal.
26
26
*/
27
27
public ZstdCompressCtx () {
28
- init ();
28
+ nativePtr = init ();
29
29
if (0 == nativePtr ) {
30
30
throw new IllegalStateException ("ZSTD_createCompressCtx failed" );
31
31
}
32
32
storeFence ();
33
33
}
34
34
35
- void doClose () {
35
+ void doClose () {
36
36
if (nativePtr != 0 ) {
37
- free ();
37
+ free (nativePtr );
38
38
nativePtr = 0 ;
39
39
}
40
40
}
41
41
42
+ private void ensureOpen () {
43
+ if (nativePtr == 0 ) {
44
+ throw new IllegalStateException ("Compression context is closed" );
45
+ }
46
+ }
47
+
42
48
/**
43
49
* Set compression level
44
50
* @param level compression level, default: {@link Zstd#defaultCompressionLevel()}
45
51
*/
46
52
public ZstdCompressCtx setLevel (int level ) {
47
- if (nativePtr == 0 ) {
48
- throw new IllegalStateException ("Compression context is closed" );
49
- }
53
+ ensureOpen ();
50
54
acquireSharedLock ();
51
- setLevel0 (level );
55
+ setLevel0 (nativePtr , level );
52
56
releaseSharedLock ();
53
57
return this ;
54
58
}
55
59
56
- private native void setLevel0 (int level );
60
+ private static native void setLevel0 (long ptr , int level );
57
61
58
62
/**
59
63
* Enable or disable magicless frames
60
64
* @param magiclessFlag A 32-bits magic number is written at start of frame, default: false
61
65
*/
62
66
public ZstdCompressCtx setMagicless (boolean magiclessFlag ) {
63
- if (nativePtr == 0 ) {
64
- throw new IllegalStateException ("Compression context is closed" );
65
- }
67
+ ensureOpen ();
66
68
acquireSharedLock ();
67
69
Zstd .setCompressionMagicless (nativePtr , magiclessFlag );
68
70
releaseSharedLock ();
@@ -74,18 +76,17 @@ public ZstdCompressCtx setMagicless(boolean magiclessFlag) {
74
76
* @param checksumFlag A 32-bits checksum of content is written at end of frame, default: false
75
77
*/
76
78
public ZstdCompressCtx setChecksum (boolean checksumFlag ) {
77
- if (nativePtr == 0 ) {
78
- throw new IllegalStateException ("Compression context is closed" );
79
- }
79
+ ensureOpen ();
80
80
acquireSharedLock ();
81
- setChecksum0 (checksumFlag );
81
+ setChecksum0 (nativePtr , checksumFlag );
82
82
releaseSharedLock ();
83
83
return this ;
84
84
}
85
- private native void setChecksum0 (boolean checksumFlag );
85
+ private static native void setChecksum0 (long ptr , boolean checksumFlag );
86
86
87
87
88
88
public ZstdCompressCtx setWorkers (int workers ) {
89
+ ensureOpen ();
89
90
acquireSharedLock ();
90
91
Zstd .setCompressionWorkers (nativePtr , workers );
91
92
releaseSharedLock ();
@@ -97,30 +98,26 @@ public ZstdCompressCtx setWorkers(int workers) {
97
98
* @param contentSizeFlag Content size will be written into frame header _whenever known_, default: true
98
99
*/
99
100
public ZstdCompressCtx setContentSize (boolean contentSizeFlag ) {
100
- if (nativePtr == 0 ) {
101
- throw new IllegalStateException ("Compression context is closed" );
102
- }
101
+ ensureOpen ();
103
102
acquireSharedLock ();
104
- setContentSize0 (contentSizeFlag );
103
+ setContentSize0 (nativePtr , contentSizeFlag );
105
104
releaseSharedLock ();
106
105
return this ;
107
106
}
108
- private native void setContentSize0 (boolean contentSizeFlag );
107
+ private static native void setContentSize0 (long ptr , boolean contentSizeFlag );
109
108
110
109
/**
111
110
* Enable or disable dictID
112
111
* @param dictIDFlag When applicable, dictionary's ID is written into frame header, default: true
113
112
*/
114
113
public ZstdCompressCtx setDictID (boolean dictIDFlag ) {
115
- if (nativePtr == 0 ) {
116
- throw new IllegalStateException ("Compression context is closed" );
117
- }
114
+ ensureOpen ();
118
115
acquireSharedLock ();
119
- setDictID0 (dictIDFlag );
116
+ setDictID0 (nativePtr , dictIDFlag );
120
117
releaseSharedLock ();
121
118
return this ;
122
119
}
123
- private native void setDictID0 (boolean dictIDFlag );
120
+ private static native void setDictID0 (long ptr , boolean dictIDFlag );
124
121
125
122
/**
126
123
* Enable or disable LongDistanceMatching and set the window size
@@ -132,9 +129,7 @@ public ZstdCompressCtx setDictID(boolean dictIDFlag) {
132
129
* 0 disables LDM.
133
130
*/
134
131
public ZstdCompressCtx setLong (int windowLog ) {
135
- if (nativePtr == 0 ) {
136
- throw new IllegalStateException ("Compression context is closed" );
137
- }
132
+ ensureOpen ();
138
133
acquireSharedLock ();
139
134
Zstd .setCompressionLong (nativePtr , windowLog );
140
135
releaseSharedLock ();
@@ -147,14 +142,11 @@ public ZstdCompressCtx setLong(int windowLog) {
147
142
* @param dict the dictionary or `null` to remove loaded dictionary
148
143
*/
149
144
public ZstdCompressCtx loadDict (ZstdDictCompress dict ) {
150
- if (nativePtr == 0 ) {
151
- throw new IllegalStateException ("Compression context is closed" );
152
- }
153
-
145
+ ensureOpen ();
154
146
acquireSharedLock ();
155
147
dict .acquireSharedLock ();
156
148
try {
157
- long result = loadCDictFast0 (dict );
149
+ long result = loadCDictFast0 (nativePtr , dict );
158
150
if (Zstd .isError (result )) {
159
151
throw new ZstdException (result );
160
152
}
@@ -166,20 +158,18 @@ public ZstdCompressCtx loadDict(ZstdDictCompress dict) {
166
158
}
167
159
return this ;
168
160
}
169
- private native long loadCDictFast0 (ZstdDictCompress dict );
161
+ private native long loadCDictFast0 (long ptr , ZstdDictCompress dict );
170
162
171
163
/**
172
164
* Load compression dictionary to be used for subsequently compressed frames.
173
165
*
174
166
* @param dict the dictionary or `null` to remove loaded dictionary
175
167
*/
176
168
public ZstdCompressCtx loadDict (byte [] dict ) {
177
- if (nativePtr == 0 ) {
178
- throw new IllegalStateException ("Compression context is closed" );
179
- }
169
+ ensureOpen ();
180
170
acquireSharedLock ();
181
171
try {
182
- long result = loadCDict0 (dict );
172
+ long result = loadCDict0 (nativePtr , dict );
183
173
if (Zstd .isError (result )) {
184
174
throw new ZstdException (result );
185
175
}
@@ -189,36 +179,30 @@ public ZstdCompressCtx loadDict(byte[] dict) {
189
179
}
190
180
return this ;
191
181
}
192
- private native long loadCDict0 (byte [] dict );
193
-
194
- private void ensureOpen () {
195
- if (nativePtr == 0 ) {
196
- throw new IllegalStateException ("Compression context is closed" );
197
- }
198
- }
182
+ private native long loadCDict0 (long ptr , byte [] dict );
199
183
200
184
/**
201
185
* Tells how much data has been ingested (read from input),
202
186
* consumed (input actually compressed) and produced (output) for current frame.
203
187
*/
204
188
public ZstdFrameProgression getFrameProgression () {
205
189
ensureOpen ();
206
- return getFrameProgression0 ();
190
+ return getFrameProgression0 (nativePtr );
207
191
}
208
- private native ZstdFrameProgression getFrameProgression0 ();
192
+ private static native ZstdFrameProgression getFrameProgression0 (long ptr );
209
193
210
194
/**
211
195
* Clear all state and parameters from the compression context. This leaves the object in a
212
196
* state identical to a newly created compression context.
213
197
*/
214
198
public void reset () {
215
199
ensureOpen ();
216
- long result = reset0 ();
200
+ long result = reset0 (nativePtr );
217
201
if (Zstd .isError (result )) {
218
202
throw new ZstdException (result );
219
203
}
220
204
}
221
- private native long reset0 ();
205
+ private static native long reset0 (long ptr );
222
206
223
207
/**
224
208
* Promise to compress a certain number of source bytes. Knowing the number of bytes to compress
@@ -230,12 +214,12 @@ public void reset() {
230
214
*/
231
215
public void setPledgedSrcSize (long srcSize ) {
232
216
ensureOpen ();
233
- long result = setPledgedSrcSize0 (srcSize );
217
+ long result = setPledgedSrcSize0 (nativePtr , srcSize );
234
218
if (Zstd .isError (result )) {
235
219
throw new ZstdException (result );
236
220
}
237
221
}
238
- private native long setPledgedSrcSize0 (long srcSize );
222
+ private static native long setPledgedSrcSize0 (long ptr , long srcSize );
239
223
240
224
/**
241
225
* Compress as much of the <code>src</code> {@link ByteBuffer} into the <code>dst</code> {@link
@@ -248,7 +232,7 @@ public void setPledgedSrcSize(long srcSize) {
248
232
*/
249
233
public boolean compressDirectByteBufferStream (ByteBuffer dst , ByteBuffer src , EndDirective endOp ) {
250
234
ensureOpen ();
251
- long result = compressDirectByteBufferStream0 (dst , dst .position (), dst .limit (), src , src .position (), src .limit (), endOp .value ());
235
+ long result = compressDirectByteBufferStream0 (nativePtr , dst , dst .position (), dst .limit (), src , src .position (), src .limit (), endOp .value ());
252
236
if ((result & 0x80000000L ) != 0 ) {
253
237
long code = result & 0xFF ;
254
238
throw new ZstdException (code , Zstd .getErrorName (code ));
@@ -265,7 +249,7 @@ public boolean compressDirectByteBufferStream(ByteBuffer dst, ByteBuffer src, En
265
249
* bit is set if an error occurred. If an error occurred, the lowest 31 bits encode a zstd error
266
250
* code. Otherwise, the lowest 31 bits are the new position of the source buffer.
267
251
*/
268
- private native long compressDirectByteBufferStream0 (ByteBuffer dst , int dstOffset , int dstSize , ByteBuffer src , int srcSize , int srcOffset , int endOp );
252
+ private static native long compressDirectByteBufferStream0 (long ptr , ByteBuffer dst , int dstOffset , int dstSize , ByteBuffer src , int srcSize , int srcOffset , int endOp );
269
253
270
254
/**
271
255
* Compresses buffer 'srcBuff' into buffer 'dstBuff' reusing this ZstdCompressCtx.
@@ -284,9 +268,7 @@ public boolean compressDirectByteBufferStream(ByteBuffer dst, ByteBuffer src, En
284
268
* @return the number of bytes written into buffer 'dstBuff'.
285
269
*/
286
270
public int compressDirectByteBuffer (ByteBuffer dstBuff , int dstOffset , int dstSize , ByteBuffer srcBuff , int srcOffset , int srcSize ) {
287
- if (nativePtr == 0 ) {
288
- throw new IllegalStateException ("Compression context is closed" );
289
- }
271
+ ensureOpen ();
290
272
if (!srcBuff .isDirect ()) {
291
273
throw new IllegalArgumentException ("srcBuff must be a direct buffer" );
292
274
}
@@ -297,7 +279,7 @@ public int compressDirectByteBuffer(ByteBuffer dstBuff, int dstOffset, int dstSi
297
279
acquireSharedLock ();
298
280
299
281
try {
300
- long size = compressDirectByteBuffer0 (dstBuff , dstOffset , dstSize , srcBuff , srcOffset , srcSize );
282
+ long size = compressDirectByteBuffer0 (nativePtr , dstBuff , dstOffset , dstSize , srcBuff , srcOffset , srcSize );
301
283
if (Zstd .isError (size )) {
302
284
throw new ZstdException (size );
303
285
}
@@ -310,7 +292,7 @@ public int compressDirectByteBuffer(ByteBuffer dstBuff, int dstOffset, int dstSi
310
292
}
311
293
}
312
294
313
- private native long compressDirectByteBuffer0 (ByteBuffer dst , int dstOffset , int dstSize , ByteBuffer src , int srcOffset , int srcSize );
295
+ private static native long compressDirectByteBuffer0 (long ptr , ByteBuffer dst , int dstOffset , int dstSize , ByteBuffer src , int srcOffset , int srcSize );
314
296
315
297
/**
316
298
* Compresses byte array 'srcBuff' into byte array 'dstBuff' reusing this ZstdCompressCtx.
@@ -328,14 +310,11 @@ public int compressDirectByteBuffer(ByteBuffer dstBuff, int dstOffset, int dstSi
328
310
* @return the number of bytes written into buffer 'dstBuff'.
329
311
*/
330
312
public int compressByteArray (byte [] dstBuff , int dstOffset , int dstSize , byte [] srcBuff , int srcOffset , int srcSize ) {
331
- if (nativePtr == 0 ) {
332
- throw new IllegalStateException ("Compression context is closed" );
333
- }
334
-
313
+ ensureOpen ();
335
314
acquireSharedLock ();
336
315
337
316
try {
338
- long size = compressByteArray0 (dstBuff , dstOffset , dstSize , srcBuff , srcOffset , srcSize );
317
+ long size = compressByteArray0 (nativePtr , dstBuff , dstOffset , dstSize , srcBuff , srcOffset , srcSize );
339
318
if (Zstd .isError (size )) {
340
319
throw new ZstdException (size );
341
320
}
@@ -348,7 +327,7 @@ public int compressByteArray(byte[] dstBuff, int dstOffset, int dstSize, byte[]
348
327
}
349
328
}
350
329
351
- private native long compressByteArray0 (byte [] dst , int dstOffset , int dstSize , byte [] src , int srcOffset , int srcSize );
330
+ private static native long compressByteArray0 (long ptr , byte [] dst , int dstOffset , int dstSize , byte [] src , int srcOffset , int srcSize );
352
331
353
332
/* Convenience methods */
354
333
0 commit comments