// 
// Decompiled by Procyon v0.6.0
// 

package com.github.luben.zstd;

import com.github.luben.zstd.util.Native;
import java.util.Arrays;
import java.nio.ByteBuffer;

public class ZstdCompressCtx extends AutoCloseBase
{
    private long nativePtr;
    private ZstdDictCompress compression_dict;
    private SequenceProducer seqprod;
    private long seqprod_state;
    
    private static native long init();
    
    private static native void free(final long p0);
    
    public ZstdCompressCtx() {
        this.nativePtr = 0L;
        this.compression_dict = null;
        this.seqprod = null;
        this.seqprod_state = 0L;
        this.nativePtr = init();
        if (0L == this.nativePtr) {
            throw new IllegalStateException("ZSTD_createCompressCtx failed");
        }
        this.storeFence();
    }
    
    @Override
    void doClose() {
        if (this.nativePtr != 0L) {
            free(this.nativePtr);
            this.nativePtr = 0L;
            if (this.seqprod != null) {
                this.seqprod.freeState(this.seqprod_state);
                this.seqprod = null;
            }
        }
    }
    
    private void ensureOpen() {
        if (this.nativePtr == 0L) {
            throw new IllegalStateException("Compression context is closed");
        }
    }
    
    public ZstdCompressCtx setLevel(final int n) {
        this.ensureOpen();
        this.acquireSharedLock();
        setLevel0(this.nativePtr, n);
        this.releaseSharedLock();
        return this;
    }
    
    private static native void setLevel0(final long p0, final int p1);
    
    public ZstdCompressCtx setMagicless(final boolean b) {
        this.ensureOpen();
        this.acquireSharedLock();
        Zstd.setCompressionMagicless(this.nativePtr, b);
        this.releaseSharedLock();
        return this;
    }
    
    public ZstdCompressCtx setChecksum(final boolean b) {
        this.ensureOpen();
        this.acquireSharedLock();
        setChecksum0(this.nativePtr, b);
        this.releaseSharedLock();
        return this;
    }
    
    private static native void setChecksum0(final long p0, final boolean p1);
    
    public ZstdCompressCtx setWorkers(final int n) {
        this.ensureOpen();
        this.acquireSharedLock();
        try {
            final long n2 = Zstd.setCompressionWorkers(this.nativePtr, n);
            if (Zstd.isError(n2)) {
                throw new ZstdException(n2);
            }
        }
        finally {
            this.releaseSharedLock();
        }
        return this;
    }
    
    public ZstdCompressCtx setOverlapLog(final int n) {
        this.ensureOpen();
        this.acquireSharedLock();
        try {
            final long n2 = Zstd.setCompressionOverlapLog(this.nativePtr, n);
            if (Zstd.isError(n2)) {
                throw new ZstdException(n2);
            }
        }
        finally {
            this.releaseSharedLock();
        }
        return this;
    }
    
    public ZstdCompressCtx setJobSize(final int n) {
        this.ensureOpen();
        this.acquireSharedLock();
        try {
            final long n2 = Zstd.setCompressionJobSize(this.nativePtr, n);
            if (Zstd.isError(n2)) {
                throw new ZstdException(n2);
            }
        }
        finally {
            this.releaseSharedLock();
        }
        return this;
    }
    
    public ZstdCompressCtx setTargetLength(final int n) {
        this.ensureOpen();
        this.acquireSharedLock();
        try {
            final long n2 = Zstd.setCompressionTargetLength(this.nativePtr, n);
            if (Zstd.isError(n2)) {
                throw new ZstdException(n2);
            }
        }
        finally {
            this.releaseSharedLock();
        }
        return this;
    }
    
    public ZstdCompressCtx setMinMatch(final int n) {
        this.ensureOpen();
        this.acquireSharedLock();
        try {
            final long n2 = Zstd.setCompressionMinMatch(this.nativePtr, n);
            if (Zstd.isError(n2)) {
                throw new ZstdException(n2);
            }
        }
        finally {
            this.releaseSharedLock();
        }
        return this;
    }
    
    public ZstdCompressCtx setSearchLog(final int n) {
        this.ensureOpen();
        this.acquireSharedLock();
        try {
            final long n2 = Zstd.setCompressionSearchLog(this.nativePtr, n);
            if (Zstd.isError(n2)) {
                throw new ZstdException(n2);
            }
        }
        finally {
            this.releaseSharedLock();
        }
        return this;
    }
    
    public ZstdCompressCtx setChainLog(final int n) {
        this.ensureOpen();
        this.acquireSharedLock();
        try {
            final long n2 = Zstd.setCompressionChainLog(this.nativePtr, n);
            if (Zstd.isError(n2)) {
                throw new ZstdException(n2);
            }
        }
        finally {
            this.releaseSharedLock();
        }
        return this;
    }
    
    public ZstdCompressCtx setHashLog(final int n) {
        this.ensureOpen();
        this.acquireSharedLock();
        try {
            final long n2 = Zstd.setCompressionHashLog(this.nativePtr, n);
            if (Zstd.isError(n2)) {
                throw new ZstdException(n2);
            }
        }
        finally {
            this.releaseSharedLock();
        }
        return this;
    }
    
    public ZstdCompressCtx setWindowLog(final int n) {
        this.ensureOpen();
        this.acquireSharedLock();
        try {
            final long n2 = Zstd.setCompressionWindowLog(this.nativePtr, n);
            if (Zstd.isError(n2)) {
                throw new ZstdException(n2);
            }
        }
        finally {
            this.releaseSharedLock();
        }
        return this;
    }
    
    public ZstdCompressCtx setStrategy(final int n) {
        this.ensureOpen();
        this.acquireSharedLock();
        try {
            final long n2 = Zstd.setCompressionStrategy(this.nativePtr, n);
            if (Zstd.isError(n2)) {
                throw new ZstdException(n2);
            }
        }
        finally {
            this.releaseSharedLock();
        }
        return this;
    }
    
    public ZstdCompressCtx setContentSize(final boolean b) {
        this.ensureOpen();
        this.acquireSharedLock();
        setContentSize0(this.nativePtr, b);
        this.releaseSharedLock();
        return this;
    }
    
    private static native void setContentSize0(final long p0, final boolean p1);
    
    public ZstdCompressCtx setDictID(final boolean b) {
        this.ensureOpen();
        this.acquireSharedLock();
        setDictID0(this.nativePtr, b);
        this.releaseSharedLock();
        return this;
    }
    
    private static native void setDictID0(final long p0, final boolean p1);
    
    public ZstdCompressCtx setLong(final int n) {
        this.ensureOpen();
        this.acquireSharedLock();
        Zstd.setCompressionLong(this.nativePtr, n);
        this.releaseSharedLock();
        return this;
    }
    
    public ZstdCompressCtx registerSequenceProducer(final SequenceProducer seqprod) {
        this.ensureOpen();
        this.acquireSharedLock();
        try {
            if (this.seqprod != null) {
                this.seqprod.freeState(this.seqprod_state);
                this.seqprod = null;
            }
            if (seqprod == null) {
                Zstd.registerSequenceProducer(this.nativePtr, 0L, 0L);
            }
            else {
                this.seqprod_state = seqprod.createState();
                Zstd.registerSequenceProducer(this.nativePtr, this.seqprod_state, seqprod.getFunctionPointer());
                this.seqprod = seqprod;
            }
        }
        catch (final Exception ex) {
            this.seqprod = null;
            Zstd.registerSequenceProducer(this.nativePtr, 0L, 0L);
            throw ex;
        }
        finally {
            this.releaseSharedLock();
        }
        return this;
    }
    
    public ZstdCompressCtx setSequenceProducerFallback(final boolean b) {
        this.ensureOpen();
        this.acquireSharedLock();
        try {
            final long n = Zstd.setSequenceProducerFallback(this.nativePtr, b);
            if (Zstd.isError(n)) {
                throw new ZstdException(n);
            }
        }
        finally {
            this.releaseSharedLock();
        }
        return this;
    }
    
    public ZstdCompressCtx setSearchForExternalRepcodes(final Zstd.ParamSwitch paramSwitch) {
        this.ensureOpen();
        this.acquireSharedLock();
        try {
            final long n = Zstd.setSearchForExternalRepcodes(this.nativePtr, paramSwitch.getValue());
            if (Zstd.isError(n)) {
                throw new ZstdException(n);
            }
        }
        finally {
            this.releaseSharedLock();
        }
        return this;
    }
    
    public ZstdCompressCtx setValidateSequences(final Zstd.ParamSwitch paramSwitch) {
        this.ensureOpen();
        this.acquireSharedLock();
        try {
            final long n = Zstd.setValidateSequences(this.nativePtr, paramSwitch.getValue());
            if (Zstd.isError(n)) {
                throw new ZstdException(n);
            }
        }
        finally {
            this.releaseSharedLock();
        }
        return this;
    }
    
    public ZstdCompressCtx setEnableLongDistanceMatching(final Zstd.ParamSwitch paramSwitch) {
        this.ensureOpen();
        this.acquireSharedLock();
        try {
            final long n = Zstd.setEnableLongDistanceMatching(this.nativePtr, paramSwitch.getValue());
            if (Zstd.isError(n)) {
                throw new ZstdException(n);
            }
        }
        finally {
            this.releaseSharedLock();
        }
        return this;
    }
    
    long getNativePtr() {
        return this.nativePtr;
    }
    
    public ZstdCompressCtx loadDict(final ZstdDictCompress compression_dict) {
        this.ensureOpen();
        this.acquireSharedLock();
        compression_dict.acquireSharedLock();
        try {
            final long loadCDictFast0 = this.loadCDictFast0(this.nativePtr, compression_dict);
            if (Zstd.isError(loadCDictFast0)) {
                throw new ZstdException(loadCDictFast0);
            }
            this.compression_dict = compression_dict;
        }
        finally {
            compression_dict.releaseSharedLock();
            this.releaseSharedLock();
        }
        return this;
    }
    
    private native long loadCDictFast0(final long p0, final ZstdDictCompress p1);
    
    public ZstdCompressCtx loadDict(final byte[] array) {
        this.ensureOpen();
        this.acquireSharedLock();
        try {
            final long loadCDict0 = this.loadCDict0(this.nativePtr, array);
            if (Zstd.isError(loadCDict0)) {
                throw new ZstdException(loadCDict0);
            }
            this.compression_dict = null;
        }
        finally {
            this.releaseSharedLock();
        }
        return this;
    }
    
    private native long loadCDict0(final long p0, final byte[] p1);
    
    public ZstdFrameProgression getFrameProgression() {
        this.ensureOpen();
        this.acquireSharedLock();
        try {
            return getFrameProgression0(this.nativePtr);
        }
        finally {
            this.releaseSharedLock();
        }
    }
    
    private static native ZstdFrameProgression getFrameProgression0(final long p0);
    
    public void reset() {
        this.ensureOpen();
        this.acquireSharedLock();
        try {
            final long reset0 = reset0(this.nativePtr);
            if (Zstd.isError(reset0)) {
                throw new ZstdException(reset0);
            }
        }
        finally {
            this.releaseSharedLock();
        }
    }
    
    private static native long reset0(final long p0);
    
    public void setPledgedSrcSize(final long n) {
        this.ensureOpen();
        this.acquireSharedLock();
        try {
            final long setPledgedSrcSize0 = setPledgedSrcSize0(this.nativePtr, n);
            if (Zstd.isError(setPledgedSrcSize0)) {
                throw new ZstdException(setPledgedSrcSize0);
            }
        }
        finally {
            this.releaseSharedLock();
        }
    }
    
    private static native long setPledgedSrcSize0(final long p0, final long p1);
    
    public boolean compressDirectByteBufferStream(final ByteBuffer byteBuffer, final ByteBuffer byteBuffer2, final EndDirective endDirective) {
        this.ensureOpen();
        this.acquireSharedLock();
        try {
            final long compressDirectByteBufferStream0 = compressDirectByteBufferStream0(this.nativePtr, byteBuffer, byteBuffer.position(), byteBuffer.limit(), byteBuffer2, byteBuffer2.position(), byteBuffer2.limit(), endDirective.value());
            if ((compressDirectByteBufferStream0 & 0x80000000L) != 0x0L) {
                final long n = -(compressDirectByteBufferStream0 & 0xFFL);
                throw new ZstdException(n, Zstd.getErrorName(n));
            }
            byteBuffer2.position();
            byteBuffer.position();
            return compressDirectByteBufferStream0 >>> 63 == 1L;
        }
        finally {
            this.releaseSharedLock();
        }
    }
    
    private static native long compressDirectByteBufferStream0(final long p0, final ByteBuffer p1, final int p2, final int p3, final ByteBuffer p4, final int p5, final int p6, final int p7);
    
    public int compressDirectByteBuffer(final ByteBuffer byteBuffer, final int n, final int n2, final ByteBuffer byteBuffer2, final int n3, final int n4) {
        this.ensureOpen();
        if (!byteBuffer2.isDirect()) {
            throw new IllegalArgumentException("srcBuff must be a direct buffer");
        }
        if (!byteBuffer.isDirect()) {
            throw new IllegalArgumentException("dstBuff must be a direct buffer");
        }
        Objects.checkFromIndexSize(n3, n4, byteBuffer2.limit());
        Objects.checkFromIndexSize(n, n2, byteBuffer.limit());
        this.acquireSharedLock();
        try {
            final long compressDirectByteBuffer0 = compressDirectByteBuffer0(this.nativePtr, byteBuffer, n, n2, byteBuffer2, n3, n4);
            if (Zstd.isError(compressDirectByteBuffer0)) {
                throw new ZstdException(compressDirectByteBuffer0);
            }
            if (compressDirectByteBuffer0 > 2147483647L) {
                throw new ZstdException(Zstd.errGeneric(), "Output size is greater than MAX_INT");
            }
            return (int)compressDirectByteBuffer0;
        }
        finally {
            this.releaseSharedLock();
        }
    }
    
    private static native long compressDirectByteBuffer0(final long p0, final ByteBuffer p1, final int p2, final int p3, final ByteBuffer p4, final int p5, final int p6);
    
    public int compressByteArray(final byte[] array, final int n, final int n2, final byte[] array2, final int n3, final int n4) {
        Objects.checkFromIndexSize(n3, n4, array2.length);
        Objects.checkFromIndexSize(n, n2, array.length);
        this.ensureOpen();
        this.acquireSharedLock();
        try {
            final long compressByteArray0 = compressByteArray0(this.nativePtr, array, n, n2, array2, n3, n4);
            if (Zstd.isError(compressByteArray0)) {
                throw new ZstdException(compressByteArray0);
            }
            if (compressByteArray0 > 2147483647L) {
                throw new ZstdException(Zstd.errGeneric(), "Output size is greater than MAX_INT");
            }
            return (int)compressByteArray0;
        }
        finally {
            this.releaseSharedLock();
        }
    }
    
    private static native long compressByteArray0(final long p0, final byte[] p1, final int p2, final int p3, final byte[] p4, final int p5, final int p6);
    
    public int compress(final ByteBuffer byteBuffer, final ByteBuffer byteBuffer2) {
        final int compressDirectByteBuffer = this.compressDirectByteBuffer(byteBuffer, byteBuffer.position(), byteBuffer.limit() - byteBuffer.position(), byteBuffer2, byteBuffer2.position(), byteBuffer2.limit() - byteBuffer2.position());
        byteBuffer2.position();
        byteBuffer.position();
        return compressDirectByteBuffer;
    }
    
    public ByteBuffer compress(final ByteBuffer byteBuffer) throws ZstdException {
        final long compressBound = Zstd.compressBound(byteBuffer.limit() - byteBuffer.position());
        if (compressBound > 2147483647L) {
            throw new ZstdException(Zstd.errGeneric(), "Max output size is greater than MAX_INT");
        }
        final ByteBuffer allocateDirect = ByteBuffer.allocateDirect((int)compressBound);
        final int compressDirectByteBuffer = this.compressDirectByteBuffer(allocateDirect, 0, (int)compressBound, byteBuffer, byteBuffer.position(), byteBuffer.limit() - byteBuffer.position());
        byteBuffer.position();
        allocateDirect.limit();
        return allocateDirect;
    }
    
    public int compress(final byte[] array, final byte[] array2) {
        return this.compressByteArray(array, 0, array.length, array2, 0, array2.length);
    }
    
    public byte[] compress(final byte[] array) {
        final long compressBound = Zstd.compressBound(array.length);
        if (compressBound > 2147483647L) {
            throw new ZstdException(Zstd.errGeneric(), "Max output size is greater than MAX_INT");
        }
        final byte[] original = new byte[(int)compressBound];
        return Arrays.copyOfRange(original, 0, this.compressByteArray(original, 0, original.length, array, 0, array.length));
    }
    
    static {
        Native.load();
    }
}
