Skip to content

Commit 54d2204

Browse files
committed
Also pass the pointer explicitly in ZstCompressCtx
... and make all JNI functions static
1 parent 1317e44 commit 54d2204

File tree

3 files changed

+93
-123
lines changed

3 files changed

+93
-123
lines changed

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

+46-67
Original file line numberDiff line numberDiff line change
@@ -16,53 +16,55 @@ public class ZstdCompressCtx extends AutoCloseBase {
1616

1717
private ZstdDictCompress compression_dict = null;
1818

19-
private native void init();
19+
private static native long init();
2020

21-
private native void free();
21+
private static native void free(long ptr);
2222

2323
/**
2424
* Create a context for faster compress operations
2525
* One such context is required for each thread - put this in a ThreadLocal.
2626
*/
2727
public ZstdCompressCtx() {
28-
init();
28+
nativePtr = init();
2929
if (0 == nativePtr) {
3030
throw new IllegalStateException("ZSTD_createCompressCtx failed");
3131
}
3232
storeFence();
3333
}
3434

35-
void doClose() {
35+
void doClose() {
3636
if (nativePtr != 0) {
37-
free();
37+
free(nativePtr);
3838
nativePtr = 0;
3939
}
4040
}
4141

42+
private void ensureOpen() {
43+
if (nativePtr == 0) {
44+
throw new IllegalStateException("Compression context is closed");
45+
}
46+
}
47+
4248
/**
4349
* Set compression level
4450
* @param level compression level, default: {@link Zstd#defaultCompressionLevel()}
4551
*/
4652
public ZstdCompressCtx setLevel(int level) {
47-
if (nativePtr == 0) {
48-
throw new IllegalStateException("Compression context is closed");
49-
}
53+
ensureOpen();
5054
acquireSharedLock();
51-
setLevel0(level);
55+
setLevel0(nativePtr, level);
5256
releaseSharedLock();
5357
return this;
5458
}
5559

56-
private native void setLevel0(int level);
60+
private static native void setLevel0(long ptr, int level);
5761

5862
/**
5963
* Enable or disable magicless frames
6064
* @param magiclessFlag A 32-bits magic number is written at start of frame, default: false
6165
*/
6266
public ZstdCompressCtx setMagicless(boolean magiclessFlag) {
63-
if (nativePtr == 0) {
64-
throw new IllegalStateException("Compression context is closed");
65-
}
67+
ensureOpen();
6668
acquireSharedLock();
6769
Zstd.setCompressionMagicless(nativePtr, magiclessFlag);
6870
releaseSharedLock();
@@ -74,18 +76,17 @@ public ZstdCompressCtx setMagicless(boolean magiclessFlag) {
7476
* @param checksumFlag A 32-bits checksum of content is written at end of frame, default: false
7577
*/
7678
public ZstdCompressCtx setChecksum(boolean checksumFlag) {
77-
if (nativePtr == 0) {
78-
throw new IllegalStateException("Compression context is closed");
79-
}
79+
ensureOpen();
8080
acquireSharedLock();
81-
setChecksum0(checksumFlag);
81+
setChecksum0(nativePtr, checksumFlag);
8282
releaseSharedLock();
8383
return this;
8484
}
85-
private native void setChecksum0(boolean checksumFlag);
85+
private static native void setChecksum0(long ptr, boolean checksumFlag);
8686

8787

8888
public ZstdCompressCtx setWorkers(int workers) {
89+
ensureOpen();
8990
acquireSharedLock();
9091
Zstd.setCompressionWorkers(nativePtr, workers);
9192
releaseSharedLock();
@@ -97,30 +98,26 @@ public ZstdCompressCtx setWorkers(int workers) {
9798
* @param contentSizeFlag Content size will be written into frame header _whenever known_, default: true
9899
*/
99100
public ZstdCompressCtx setContentSize(boolean contentSizeFlag) {
100-
if (nativePtr == 0) {
101-
throw new IllegalStateException("Compression context is closed");
102-
}
101+
ensureOpen();
103102
acquireSharedLock();
104-
setContentSize0(contentSizeFlag);
103+
setContentSize0(nativePtr, contentSizeFlag);
105104
releaseSharedLock();
106105
return this;
107106
}
108-
private native void setContentSize0(boolean contentSizeFlag);
107+
private static native void setContentSize0(long ptr, boolean contentSizeFlag);
109108

110109
/**
111110
* Enable or disable dictID
112111
* @param dictIDFlag When applicable, dictionary's ID is written into frame header, default: true
113112
*/
114113
public ZstdCompressCtx setDictID(boolean dictIDFlag) {
115-
if (nativePtr == 0) {
116-
throw new IllegalStateException("Compression context is closed");
117-
}
114+
ensureOpen();
118115
acquireSharedLock();
119-
setDictID0(dictIDFlag);
116+
setDictID0(nativePtr, dictIDFlag);
120117
releaseSharedLock();
121118
return this;
122119
}
123-
private native void setDictID0(boolean dictIDFlag);
120+
private static native void setDictID0(long ptr, boolean dictIDFlag);
124121

125122
/**
126123
* Enable or disable LongDistanceMatching and set the window size
@@ -132,9 +129,7 @@ public ZstdCompressCtx setDictID(boolean dictIDFlag) {
132129
* 0 disables LDM.
133130
*/
134131
public ZstdCompressCtx setLong(int windowLog) {
135-
if (nativePtr == 0) {
136-
throw new IllegalStateException("Compression context is closed");
137-
}
132+
ensureOpen();
138133
acquireSharedLock();
139134
Zstd.setCompressionLong(nativePtr, windowLog);
140135
releaseSharedLock();
@@ -147,14 +142,11 @@ public ZstdCompressCtx setLong(int windowLog) {
147142
* @param dict the dictionary or `null` to remove loaded dictionary
148143
*/
149144
public ZstdCompressCtx loadDict(ZstdDictCompress dict) {
150-
if (nativePtr == 0) {
151-
throw new IllegalStateException("Compression context is closed");
152-
}
153-
145+
ensureOpen();
154146
acquireSharedLock();
155147
dict.acquireSharedLock();
156148
try {
157-
long result = loadCDictFast0(dict);
149+
long result = loadCDictFast0(nativePtr, dict);
158150
if (Zstd.isError(result)) {
159151
throw new ZstdException(result);
160152
}
@@ -166,20 +158,18 @@ public ZstdCompressCtx loadDict(ZstdDictCompress dict) {
166158
}
167159
return this;
168160
}
169-
private native long loadCDictFast0(ZstdDictCompress dict);
161+
private native long loadCDictFast0(long ptr, ZstdDictCompress dict);
170162

171163
/**
172164
* Load compression dictionary to be used for subsequently compressed frames.
173165
*
174166
* @param dict the dictionary or `null` to remove loaded dictionary
175167
*/
176168
public ZstdCompressCtx loadDict(byte[] dict) {
177-
if (nativePtr == 0) {
178-
throw new IllegalStateException("Compression context is closed");
179-
}
169+
ensureOpen();
180170
acquireSharedLock();
181171
try {
182-
long result = loadCDict0(dict);
172+
long result = loadCDict0(nativePtr, dict);
183173
if (Zstd.isError(result)) {
184174
throw new ZstdException(result);
185175
}
@@ -189,36 +179,30 @@ public ZstdCompressCtx loadDict(byte[] dict) {
189179
}
190180
return this;
191181
}
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);
199183

200184
/**
201185
* Tells how much data has been ingested (read from input),
202186
* consumed (input actually compressed) and produced (output) for current frame.
203187
*/
204188
public ZstdFrameProgression getFrameProgression() {
205189
ensureOpen();
206-
return getFrameProgression0();
190+
return getFrameProgression0(nativePtr);
207191
}
208-
private native ZstdFrameProgression getFrameProgression0();
192+
private static native ZstdFrameProgression getFrameProgression0(long ptr);
209193

210194
/**
211195
* Clear all state and parameters from the compression context. This leaves the object in a
212196
* state identical to a newly created compression context.
213197
*/
214198
public void reset() {
215199
ensureOpen();
216-
long result = reset0();
200+
long result = reset0(nativePtr);
217201
if (Zstd.isError(result)) {
218202
throw new ZstdException(result);
219203
}
220204
}
221-
private native long reset0();
205+
private static native long reset0(long ptr);
222206

223207
/**
224208
* Promise to compress a certain number of source bytes. Knowing the number of bytes to compress
@@ -230,12 +214,12 @@ public void reset() {
230214
*/
231215
public void setPledgedSrcSize(long srcSize) {
232216
ensureOpen();
233-
long result = setPledgedSrcSize0(srcSize);
217+
long result = setPledgedSrcSize0(nativePtr, srcSize);
234218
if (Zstd.isError(result)) {
235219
throw new ZstdException(result);
236220
}
237221
}
238-
private native long setPledgedSrcSize0(long srcSize);
222+
private static native long setPledgedSrcSize0(long ptr, long srcSize);
239223

240224
/**
241225
* 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) {
248232
*/
249233
public boolean compressDirectByteBufferStream(ByteBuffer dst, ByteBuffer src, EndDirective endOp) {
250234
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());
252236
if ((result & 0x80000000L) != 0) {
253237
long code = result & 0xFF;
254238
throw new ZstdException(code, Zstd.getErrorName(code));
@@ -265,7 +249,7 @@ public boolean compressDirectByteBufferStream(ByteBuffer dst, ByteBuffer src, En
265249
* bit is set if an error occurred. If an error occurred, the lowest 31 bits encode a zstd error
266250
* code. Otherwise, the lowest 31 bits are the new position of the source buffer.
267251
*/
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);
269253

270254
/**
271255
* Compresses buffer 'srcBuff' into buffer 'dstBuff' reusing this ZstdCompressCtx.
@@ -284,9 +268,7 @@ public boolean compressDirectByteBufferStream(ByteBuffer dst, ByteBuffer src, En
284268
* @return the number of bytes written into buffer 'dstBuff'.
285269
*/
286270
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();
290272
if (!srcBuff.isDirect()) {
291273
throw new IllegalArgumentException("srcBuff must be a direct buffer");
292274
}
@@ -297,7 +279,7 @@ public int compressDirectByteBuffer(ByteBuffer dstBuff, int dstOffset, int dstSi
297279
acquireSharedLock();
298280

299281
try {
300-
long size = compressDirectByteBuffer0(dstBuff, dstOffset, dstSize, srcBuff, srcOffset, srcSize);
282+
long size = compressDirectByteBuffer0(nativePtr, dstBuff, dstOffset, dstSize, srcBuff, srcOffset, srcSize);
301283
if (Zstd.isError(size)) {
302284
throw new ZstdException(size);
303285
}
@@ -310,7 +292,7 @@ public int compressDirectByteBuffer(ByteBuffer dstBuff, int dstOffset, int dstSi
310292
}
311293
}
312294

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);
314296

315297
/**
316298
* Compresses byte array 'srcBuff' into byte array 'dstBuff' reusing this ZstdCompressCtx.
@@ -328,14 +310,11 @@ public int compressDirectByteBuffer(ByteBuffer dstBuff, int dstOffset, int dstSi
328310
* @return the number of bytes written into buffer 'dstBuff'.
329311
*/
330312
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();
335314
acquireSharedLock();
336315

337316
try {
338-
long size = compressByteArray0(dstBuff, dstOffset, dstSize, srcBuff, srcOffset, srcSize);
317+
long size = compressByteArray0(nativePtr, dstBuff, dstOffset, dstSize, srcBuff, srcOffset, srcSize);
339318
if (Zstd.isError(size)) {
340319
throw new ZstdException(size);
341320
}
@@ -348,7 +327,7 @@ public int compressByteArray(byte[] dstBuff, int dstOffset, int dstSize, byte[]
348327
}
349328
}
350329

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);
352331

353332
/* Convenience methods */
354333

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

+3
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,9 @@ public int decompress(byte[] dst, byte[] src) {
272272
* @return byte array with the decompressed data
273273
*/
274274
public byte[] decompress(byte[] src, int originalSize) throws ZstdException {
275+
if (originalSize < 0) {
276+
throw new ZstdException(Zstd.errGeneric(), "Original size should not be negative");
277+
}
275278
byte[] dst = new byte[originalSize];
276279
int size = decompress(dst, src);
277280
if (size != originalSize) {

0 commit comments

Comments
 (0)