Skip to content

Commit

Permalink
More robust error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Coder-256 authored and luben committed Nov 30, 2023
1 parent 96be04a commit 475ed5d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/github/luben/zstd/Zstd.java
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ public static long decompressDirectByteBufferFastDict(ByteBuffer dst, int dstOff
public static native int loadDictCompress(long stream, byte[] dict, int dict_size);
public static native int loadFastDictCompress(long stream, ZstdDictCompress dict);
public static native void registerSequenceProducer(long stream, long seqProdState, long seqProdFunction);
public static native void generateSequences(long stream, long outSeqs, long outSeqsSize, long src, long srcSize);
static native void generateSequences(long stream, long outSeqs, long outSeqsSize, long src, long srcSize);
static native long getBuiltinSequenceProducer(); // Used in tests
static native long getStubSequenceProducer(); // Used in tests
public static native int setCompressionChecksums(long stream, boolean useChecksums);
Expand Down
28 changes: 18 additions & 10 deletions src/main/java/com/github/luben/zstd/ZstdCompressCtx.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,19 +283,27 @@ public ZstdCompressCtx setLong(int windowLog) {
public ZstdCompressCtx registerSequenceProducer(SequenceProducer producer) {
ensureOpen();
acquireSharedLock();
if (this.seqprod != null) {
this.seqprod.freeState(seqprod_state);
}
try {
if (this.seqprod != null) {
this.seqprod.freeState(seqprod_state);
this.seqprod = null;
}

if (producer == null) {
seqprod_state = 0;
if (producer == null) {
Zstd.registerSequenceProducer(nativePtr, 0, 0);
} else {
seqprod_state = producer.createState();
Zstd.registerSequenceProducer(nativePtr, seqprod_state, producer.getFunctionPointer());
this.seqprod = producer;
}
} catch (Exception e) {
this.seqprod = null;
Zstd.registerSequenceProducer(nativePtr, 0, 0);
} else {
seqprod_state = producer.createState();
Zstd.registerSequenceProducer(nativePtr, seqprod_state, producer.getFunctionPointer());
releaseSharedLock();
throw e;
} finally {
releaseSharedLock();
}
this.seqprod = producer;
releaseSharedLock();
return this;
}

Expand Down

0 comments on commit 475ed5d

Please sign in to comment.