Skip to content

Commit

Permalink
Use new ZstdIOException when replacing IOException
Browse files Browse the repository at this point in the history
So that we don't break backward compatibility - ZstdIOException is
subclass of the IOException so signatures don't have to be changed.
  • Loading branch information
luben committed Jul 20, 2022
1 parent bdbb00b commit 8e73994
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void compress(ByteBuffer source) throws IOException {
result = initCStream(stream, level);
}
if (Zstd.isError(result)) {
throw new ZstdException(result);
throw new ZstdIOException(result);
}
initialized = true;
}
Expand All @@ -113,7 +113,7 @@ public void compress(ByteBuffer source) throws IOException {
}
long result = compressDirectByteBuffer(stream, target, target.position(), target.remaining(), source, source.position(), source.remaining());
if (Zstd.isError(result)) {
throw new ZstdException(result);
throw new ZstdIOException(result);
}
target.position(target.position() + produced);
source.position(source.position() + consumed);
Expand All @@ -130,7 +130,7 @@ public void flush() throws IOException {
do {
needed = flushStream(stream, target, target.position(), target.remaining());
if (Zstd.isError(needed)) {
throw new ZstdException(needed);
throw new ZstdIOException(needed);
}
target.position(target.position() + produced);
target = flushBuffer(target);
Expand All @@ -155,7 +155,7 @@ public void close() throws IOException {
do {
needed = endStream(stream, target, target.position(), target.remaining());
if (Zstd.isError(needed)) {
throw new ZstdException(needed);
throw new ZstdIOException(needed);
}
target.position(target.position() + produced);
target = flushBuffer(target);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,20 @@ public static int recommendedTargetBufferSize() {
return (int) recommendedDOutSize();
}

public ZstdDirectBufferDecompressingStreamNoFinalizer setDict(byte[] dict) {
public ZstdDirectBufferDecompressingStreamNoFinalizer setDict(byte[] dict) throws IOException {
long size = Zstd.loadDictDecompress(stream, dict, dict.length);
if (Zstd.isError(size)) {
throw new ZstdException(size);
throw new ZstdIOException(size);
}
return this;
}

public ZstdDirectBufferDecompressingStreamNoFinalizer setDict(ZstdDictDecompress dict) {
public ZstdDirectBufferDecompressingStreamNoFinalizer setDict(ZstdDictDecompress dict) throws IOException {
dict.acquireSharedLock();
try {
long size = Zstd.loadFastDictDecompress(stream, dict);
if (Zstd.isError(size)) {
throw new ZstdException(size);
throw new ZstdIOException(size);
}
} finally {
dict.releaseSharedLock();
Expand All @@ -86,7 +86,7 @@ public int read(ByteBuffer target) throws IOException {

long remaining = decompressStream(stream, target, target.position(), target.remaining(), source, source.position(), source.remaining());
if (Zstd.isError(remaining)) {
throw new ZstdException(remaining);
throw new ZstdIOException(remaining);
}

source.position(source.position() + consumed);
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/com/github/luben/zstd/ZstdIOException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.github.luben.zstd;

import java.io.IOException;

public class ZstdIOException extends IOException {
private long code;

/**
* Construct a ZstdException from the result of a Zstd library call.
*
* The error code and message are automatically looked up using
* Zstd.getErrorCode and Zstd.getErrorName.
*
* @param result the return value of a Zstd library call
*/
public ZstdIOException(long result) {
this(Zstd.getErrorCode(result), Zstd.getErrorName(result));
}

/**
* Construct a ZstdException with a manually-specified error code and message.
*
* No transformation of either the code or message is done. It is advised
* that one of the Zstd.err*() is used to obtain a stable error code.
*
* @param code a Zstd error code
* @param message the exception's message
*/
public ZstdIOException(long code, String message) {
super(message);
this.code = code;
}

/**
* Get the Zstd error code that caused the exception.
*
* This will likely correspond to one of the Zstd.err*() methods, but the
* Zstd library may return error codes that are not yet stable. In such
* cases, this method will return the code reported by Zstd, but it will
* not correspond to any of the Zstd.err*() methods.
*
* @return a Zstd error code
*/
public long getErrorCode() {
return code;
}
}
8 changes: 4 additions & 4 deletions src/main/java/com/github/luben/zstd/ZstdInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ZstdInputStream extends FilterInputStream {
* create a new decompressing InputStream
* @param inStream the stream to wrap
*/
public ZstdInputStream(InputStream inStream) {
public ZstdInputStream(InputStream inStream) throws IOException {
super(inStream);
inner = new ZstdInputStreamNoFinalizer(inStream);
}
Expand All @@ -29,7 +29,7 @@ public ZstdInputStream(InputStream inStream) {
* @param inStream the stream to wrap
* @param bufferPool the pool to fetch and return buffers
*/
public ZstdInputStream(InputStream inStream, BufferPool bufferPool) {
public ZstdInputStream(InputStream inStream, BufferPool bufferPool) throws IOException {
super(inStream);
inner = new ZstdInputStreamNoFinalizer(inStream, bufferPool);
}
Expand Down Expand Up @@ -74,11 +74,11 @@ public boolean getContinuous() {
return inner.getContinuous();
}

public ZstdInputStream setDict(byte[] dict) {
public ZstdInputStream setDict(byte[] dict) throws IOException {
inner.setDict(dict);
return this;
}
public ZstdInputStream setDict(ZstdDictDecompress dict) {
public ZstdInputStream setDict(ZstdDictDecompress dict) throws IOException {
inner.setDict(dict);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class ZstdInputStreamNoFinalizer extends FilterInputStream {
* create a new decompressing InputStream
* @param inStream the stream to wrap
*/
public ZstdInputStreamNoFinalizer(InputStream inStream) {
public ZstdInputStreamNoFinalizer(InputStream inStream) throws IOException {
this(inStream, NoPool.INSTANCE);
}

Expand All @@ -59,12 +59,12 @@ public ZstdInputStreamNoFinalizer(InputStream inStream) {
* @param inStream the stream to wrap
* @param bufferPool the pool to fetch and return buffers
*/
public ZstdInputStreamNoFinalizer(InputStream inStream, BufferPool bufferPool) {
public ZstdInputStreamNoFinalizer(InputStream inStream, BufferPool bufferPool) throws IOException {
super(inStream);
this.bufferPool = bufferPool;
this.srcByteBuffer = bufferPool.get(srcBuffSize);
if (this.srcByteBuffer == null) {
throw new ZstdException(Zstd.errMemoryAllocation(), "Cannot get ByteBuffer of size " + srcBuffSize + " from the BufferPool");
throw new ZstdIOException(Zstd.errMemoryAllocation(), "Cannot get ByteBuffer of size " + srcBuffSize + " from the BufferPool");
}
this.src = Zstd.extractArray(srcByteBuffer);
// memory barrier
Expand All @@ -88,20 +88,20 @@ public synchronized boolean getContinuous() {
return this.isContinuous;
}

public synchronized ZstdInputStreamNoFinalizer setDict(byte[] dict) {
public synchronized ZstdInputStreamNoFinalizer setDict(byte[] dict) throws IOException {
int size = Zstd.loadDictDecompress(stream, dict, dict.length);
if (Zstd.isError(size)) {
throw new ZstdException(size);
throw new ZstdIOException(size);
}
return this;
}

public synchronized ZstdInputStreamNoFinalizer setDict(ZstdDictDecompress dict) {
public synchronized ZstdInputStreamNoFinalizer setDict(ZstdDictDecompress dict) throws IOException {
dict.acquireSharedLock();
try {
int size = Zstd.loadFastDictDecompress(stream, dict);
if (Zstd.isError(size)) {
throw new ZstdException(size);
throw new ZstdIOException(size);
}
} finally {
dict.releaseSharedLock();
Expand Down Expand Up @@ -158,7 +158,7 @@ int readInternal(byte[] dst, int offset, int len) throws IOException {
}
return -1;
} else {
throw new ZstdException(Zstd.errCorruptionDetected(), "Truncated source");
throw new ZstdIOException(Zstd.errCorruptionDetected(), "Truncated source");
}
}
frameFinished = false;
Expand All @@ -168,7 +168,7 @@ int readInternal(byte[] dst, int offset, int len) throws IOException {
int size = decompressStream(stream, dst, dstSize, src, (int) srcSize);

if (Zstd.isError(size)) {
throw new ZstdException(size);
throw new ZstdIOException(size);
}

// we have completed a frame
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/com/github/luben/zstd/ZstdOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ZstdOutputStream extends FilterOutputStream{
* Use ZstdOutputStream() or ZstdOutputStream(level) and set the other params with the setters
**/
@Deprecated
public ZstdOutputStream(OutputStream outStream, int level, boolean closeFrameOnFlush, boolean useChecksums) {
public ZstdOutputStream(OutputStream outStream, int level, boolean closeFrameOnFlush, boolean useChecksums) throws IOException {
super(outStream);
inner = new ZstdOutputStreamNoFinalizer(outStream, level);
inner.setCloseFrameOnFlush(closeFrameOnFlush);
Expand All @@ -31,7 +31,7 @@ public ZstdOutputStream(OutputStream outStream, int level, boolean closeFrameOnF
* Use ZstdOutputStream() or ZstdOutputStream(level) and set the other params with the setters
**/
@Deprecated
public ZstdOutputStream(OutputStream outStream, int level, boolean closeFrameOnFlush) {
public ZstdOutputStream(OutputStream outStream, int level, boolean closeFrameOnFlush) throws IOException {
super(outStream);
inner = new ZstdOutputStreamNoFinalizer(outStream, level);
inner.setCloseFrameOnFlush(closeFrameOnFlush);
Expand All @@ -42,7 +42,7 @@ public ZstdOutputStream(OutputStream outStream, int level, boolean closeFrameOnF
* @param outStream the stream to wrap
* @param level the compression level
*/
public ZstdOutputStream(OutputStream outStream, int level) {
public ZstdOutputStream(OutputStream outStream, int level) throws IOException {
this(outStream, NoPool.INSTANCE);
inner.setLevel(level);
}
Expand All @@ -51,7 +51,7 @@ public ZstdOutputStream(OutputStream outStream, int level) {
* create a new compressing OutputStream
* @param outStream the stream to wrap
*/
public ZstdOutputStream(OutputStream outStream) {
public ZstdOutputStream(OutputStream outStream) throws IOException {
this(outStream, NoPool.INSTANCE);
}

Expand All @@ -60,7 +60,7 @@ public ZstdOutputStream(OutputStream outStream) {
* @param outStream the stream to wrap
* @param bufferPool the pool to fetch and return buffers
*/
public ZstdOutputStream(OutputStream outStream, BufferPool bufferPool, int level) {
public ZstdOutputStream(OutputStream outStream, BufferPool bufferPool, int level) throws IOException {
this(outStream, bufferPool);
inner.setLevel(level);
}
Expand All @@ -70,7 +70,7 @@ public ZstdOutputStream(OutputStream outStream, BufferPool bufferPool, int level
* @param outStream the stream to wrap
* @param bufferPool the pool to fetch and return buffers
*/
public ZstdOutputStream(OutputStream outStream, BufferPool bufferPool) {
public ZstdOutputStream(OutputStream outStream, BufferPool bufferPool) throws IOException {
super(outStream);
inner = new ZstdOutputStreamNoFinalizer(outStream, bufferPool);
}
Expand Down Expand Up @@ -103,7 +103,7 @@ public static long recommendedCOutSize() {
*
* Default: false
*/
public ZstdOutputStream setChecksum(boolean useChecksums) {
public ZstdOutputStream setChecksum(boolean useChecksums) throws IOException {
inner.setChecksum(useChecksums);
return this;
}
Expand All @@ -113,7 +113,7 @@ public ZstdOutputStream setChecksum(boolean useChecksums) {
*
* Default: {@link Zstd#defaultCompressionLevel()}
*/
public ZstdOutputStream setLevel(int level) {
public ZstdOutputStream setLevel(int level) throws IOException {
inner.setLevel(level);
return this;
}
Expand All @@ -123,7 +123,7 @@ public ZstdOutputStream setLevel(int level) {
*
* Values for windowLog outside the range 10-27 will disable and reset LDM
*/
public ZstdOutputStream setLong(int windowLog) {
public ZstdOutputStream setLong(int windowLog) throws IOException {
inner.setLong(windowLog);
return this;
}
Expand All @@ -133,7 +133,7 @@ public ZstdOutputStream setLong(int windowLog) {
*
* Default: no worker threads.
*/
public ZstdOutputStream setWorkers(int n) {
public ZstdOutputStream setWorkers(int n) throws IOException {
inner.setWorkers(n);
return this;
}
Expand All @@ -152,12 +152,12 @@ public ZstdOutputStream setCloseFrameOnFlush(boolean closeOnFlush) {
return this;
}

public ZstdOutputStream setDict(byte[] dict) {
public ZstdOutputStream setDict(byte[] dict) throws IOException {
inner.setDict(dict);
return this;
}

public ZstdOutputStream setDict(ZstdDictCompress dict) {
public ZstdOutputStream setDict(ZstdDictCompress dict) throws IOException {
inner.setDict(dict);
return this;
}
Expand Down

0 comments on commit 8e73994

Please sign in to comment.