Skip to content

Commit 475ed5d

Browse files
Coder-256luben
authored andcommitted
More robust error handling
1 parent 96be04a commit 475ed5d

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ public static long decompressDirectByteBufferFastDict(ByteBuffer dst, int dstOff
591591
public static native int loadDictCompress(long stream, byte[] dict, int dict_size);
592592
public static native int loadFastDictCompress(long stream, ZstdDictCompress dict);
593593
public static native void registerSequenceProducer(long stream, long seqProdState, long seqProdFunction);
594-
public static native void generateSequences(long stream, long outSeqs, long outSeqsSize, long src, long srcSize);
594+
static native void generateSequences(long stream, long outSeqs, long outSeqsSize, long src, long srcSize);
595595
static native long getBuiltinSequenceProducer(); // Used in tests
596596
static native long getStubSequenceProducer(); // Used in tests
597597
public static native int setCompressionChecksums(long stream, boolean useChecksums);

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -283,19 +283,27 @@ public ZstdCompressCtx setLong(int windowLog) {
283283
public ZstdCompressCtx registerSequenceProducer(SequenceProducer producer) {
284284
ensureOpen();
285285
acquireSharedLock();
286-
if (this.seqprod != null) {
287-
this.seqprod.freeState(seqprod_state);
288-
}
286+
try {
287+
if (this.seqprod != null) {
288+
this.seqprod.freeState(seqprod_state);
289+
this.seqprod = null;
290+
}
289291

290-
if (producer == null) {
291-
seqprod_state = 0;
292+
if (producer == null) {
293+
Zstd.registerSequenceProducer(nativePtr, 0, 0);
294+
} else {
295+
seqprod_state = producer.createState();
296+
Zstd.registerSequenceProducer(nativePtr, seqprod_state, producer.getFunctionPointer());
297+
this.seqprod = producer;
298+
}
299+
} catch (Exception e) {
300+
this.seqprod = null;
292301
Zstd.registerSequenceProducer(nativePtr, 0, 0);
293-
} else {
294-
seqprod_state = producer.createState();
295-
Zstd.registerSequenceProducer(nativePtr, seqprod_state, producer.getFunctionPointer());
302+
releaseSharedLock();
303+
throw e;
304+
} finally {
305+
releaseSharedLock();
296306
}
297-
this.seqprod = producer;
298-
releaseSharedLock();
299307
return this;
300308
}
301309

0 commit comments

Comments
 (0)