// 
// 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 ZstdDecompressCtx extends AutoCloseBase
{
    private long nativePtr;
    private ZstdDictDecompress decompression_dict;
    
    private static native long init();
    
    private static native void free(final long p0);
    
    public ZstdDecompressCtx() {
        this.nativePtr = 0L;
        this.decompression_dict = null;
        this.nativePtr = init();
        if (0L == this.nativePtr) {
            throw new IllegalStateException("ZSTD_createDeCompressCtx failed");
        }
        this.storeFence();
    }
    
    @Override
    void doClose() {
        if (this.nativePtr != 0L) {
            free(this.nativePtr);
            this.nativePtr = 0L;
        }
    }
    
    public ZstdDecompressCtx setMagicless(final boolean b) {
        this.ensureOpen();
        this.acquireSharedLock();
        Zstd.setDecompressionMagicless(this.nativePtr, b);
        this.releaseSharedLock();
        return this;
    }
    
    public ZstdDecompressCtx loadDict(final ZstdDictDecompress decompression_dict) {
        this.ensureOpen();
        this.acquireSharedLock();
        decompression_dict.acquireSharedLock();
        try {
            final long loadDDictFast0 = loadDDictFast0(this.nativePtr, decompression_dict);
            if (Zstd.isError(loadDDictFast0)) {
                throw new ZstdException(loadDDictFast0);
            }
            this.decompression_dict = decompression_dict;
        }
        finally {
            decompression_dict.releaseSharedLock();
            this.releaseSharedLock();
        }
        return this;
    }
    
    private static native long loadDDictFast0(final long p0, final ZstdDictDecompress p1);
    
    public ZstdDecompressCtx loadDict(final byte[] array) {
        this.ensureOpen();
        this.acquireSharedLock();
        try {
            final long loadDDict0 = loadDDict0(this.nativePtr, array);
            if (Zstd.isError(loadDDict0)) {
                throw new ZstdException(loadDDict0);
            }
            this.decompression_dict = null;
        }
        finally {
            this.releaseSharedLock();
        }
        return this;
    }
    
    private static native long loadDDict0(final long p0, final byte[] p1);
    
    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);
    
    private void ensureOpen() {
        if (this.nativePtr == 0L) {
            throw new IllegalStateException("Decompression context is closed");
        }
    }
    
    public boolean decompressDirectByteBufferStream(final ByteBuffer byteBuffer, final ByteBuffer byteBuffer2) {
        this.ensureOpen();
        this.acquireSharedLock();
        try {
            final long decompressDirectByteBufferStream0 = decompressDirectByteBufferStream0(this.nativePtr, byteBuffer, byteBuffer.position(), byteBuffer.limit(), byteBuffer2, byteBuffer2.position(), byteBuffer2.limit());
            if ((decompressDirectByteBufferStream0 & 0x80000000L) != 0x0L) {
                final long n = -(decompressDirectByteBufferStream0 & 0xFFL);
                throw new ZstdException(n, Zstd.getErrorName(n));
            }
            byteBuffer2.position();
            byteBuffer.position();
            return decompressDirectByteBufferStream0 >>> 63 == 1L;
        }
        finally {
            this.releaseSharedLock();
        }
    }
    
    private static native long decompressDirectByteBufferStream0(final long p0, final ByteBuffer p1, final int p2, final int p3, final ByteBuffer p4, final int p5, final int p6);
    
    public int decompressDirectByteBuffer(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 decompressDirectByteBuffer0 = decompressDirectByteBuffer0(this.nativePtr, byteBuffer, n, n2, byteBuffer2, n3, n4);
            if (Zstd.isError(decompressDirectByteBuffer0)) {
                throw new ZstdException(decompressDirectByteBuffer0);
            }
            if (decompressDirectByteBuffer0 > 2147483647L) {
                throw new ZstdException(Zstd.errGeneric(), "Output size is greater than MAX_INT");
            }
            return (int)decompressDirectByteBuffer0;
        }
        finally {
            this.releaseSharedLock();
        }
    }
    
    private static native long decompressDirectByteBuffer0(final long p0, final ByteBuffer p1, final int p2, final int p3, final ByteBuffer p4, final int p5, final int p6);
    
    public int decompressByteArray(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 decompressByteArray0 = decompressByteArray0(this.nativePtr, array, n, n2, array2, n3, n4);
            if (Zstd.isError(decompressByteArray0)) {
                throw new ZstdException(decompressByteArray0);
            }
            if (decompressByteArray0 > 2147483647L) {
                throw new ZstdException(Zstd.errGeneric(), "Output size is greater than MAX_INT");
            }
            return (int)decompressByteArray0;
        }
        finally {
            this.releaseSharedLock();
        }
    }
    
    private static native long decompressByteArray0(final long p0, final byte[] p1, final int p2, final int p3, final byte[] p4, final int p5, final int p6);
    
    public int decompressByteArrayToDirectByteBuffer(final ByteBuffer byteBuffer, final int n, final int n2, final byte[] array, final int n3, final int n4) {
        if (!byteBuffer.isDirect()) {
            throw new IllegalArgumentException("dstBuff must be a direct buffer");
        }
        Objects.checkFromIndexSize(n3, n4, array.length);
        Objects.checkFromIndexSize(n, n2, byteBuffer.limit());
        this.ensureOpen();
        this.acquireSharedLock();
        try {
            final long decompressByteArrayToDirectByteBuffer0 = decompressByteArrayToDirectByteBuffer0(this.nativePtr, byteBuffer, n, n2, array, n3, n4);
            if (Zstd.isError(decompressByteArrayToDirectByteBuffer0)) {
                throw new ZstdException(decompressByteArrayToDirectByteBuffer0);
            }
            if (decompressByteArrayToDirectByteBuffer0 > 2147483647L) {
                throw new ZstdException(Zstd.errGeneric(), "Output size is greater than MAX_INT");
            }
            return (int)decompressByteArrayToDirectByteBuffer0;
        }
        finally {
            this.releaseSharedLock();
        }
    }
    
    private static native long decompressByteArrayToDirectByteBuffer0(final long p0, final ByteBuffer p1, final int p2, final int p3, final byte[] p4, final int p5, final int p6);
    
    public int decompressDirectByteBufferToByteArray(final byte[] array, final int n, final int n2, final ByteBuffer byteBuffer, final int n3, final int n4) {
        if (!byteBuffer.isDirect()) {
            throw new IllegalArgumentException("srcBuff must be a direct buffer");
        }
        Objects.checkFromIndexSize(n3, n4, byteBuffer.limit());
        Objects.checkFromIndexSize(n, n2, array.length);
        this.ensureOpen();
        this.acquireSharedLock();
        try {
            final long decompressDirectByteBufferToByteArray0 = decompressDirectByteBufferToByteArray0(this.nativePtr, array, n, n2, byteBuffer, n3, n4);
            if (Zstd.isError(decompressDirectByteBufferToByteArray0)) {
                throw new ZstdException(decompressDirectByteBufferToByteArray0);
            }
            if (decompressDirectByteBufferToByteArray0 > 2147483647L) {
                throw new ZstdException(Zstd.errGeneric(), "Output size is greater than MAX_INT");
            }
            return (int)decompressDirectByteBufferToByteArray0;
        }
        finally {
            this.releaseSharedLock();
        }
    }
    
    private static native long decompressDirectByteBufferToByteArray0(final long p0, final byte[] p1, final int p2, final int p3, final ByteBuffer p4, final int p5, final int p6);
    
    public int decompress(final ByteBuffer byteBuffer, final ByteBuffer byteBuffer2) throws ZstdException {
        final int decompressDirectByteBuffer = this.decompressDirectByteBuffer(byteBuffer, byteBuffer.position(), byteBuffer.limit() - byteBuffer.position(), byteBuffer2, byteBuffer2.position(), byteBuffer2.limit() - byteBuffer2.position());
        byteBuffer2.position();
        byteBuffer.position();
        return decompressDirectByteBuffer;
    }
    
    public int decompress(final ByteBuffer byteBuffer, final byte[] array) throws ZstdException {
        final int decompressByteArrayToDirectByteBuffer = this.decompressByteArrayToDirectByteBuffer(byteBuffer, byteBuffer.position(), byteBuffer.limit() - byteBuffer.position(), array, 0, array.length);
        byteBuffer.position();
        return decompressByteArrayToDirectByteBuffer;
    }
    
    public int decompress(final byte[] array, final ByteBuffer byteBuffer) throws ZstdException {
        final int decompressDirectByteBufferToByteArray = this.decompressDirectByteBufferToByteArray(array, 0, array.length, byteBuffer, byteBuffer.position(), byteBuffer.limit() - byteBuffer.position());
        byteBuffer.position();
        return decompressDirectByteBufferToByteArray;
    }
    
    public ByteBuffer decompress(final ByteBuffer byteBuffer, final int capacity) throws ZstdException {
        final ByteBuffer allocateDirect = ByteBuffer.allocateDirect(capacity);
        this.decompressDirectByteBuffer(allocateDirect, 0, capacity, byteBuffer, byteBuffer.position(), byteBuffer.limit() - byteBuffer.position());
        byteBuffer.position();
        return allocateDirect;
    }
    
    public int decompress(final byte[] array, final byte[] array2) {
        return this.decompressByteArray(array, 0, array.length, array2, 0, array2.length);
    }
    
    public byte[] decompress(final byte[] array, final int n) throws ZstdException {
        return this.decompress(array, 0, array.length, n);
    }
    
    public byte[] decompress(final byte[] array, final int n, final int n2, final int n3) throws ZstdException {
        if (n3 < 0) {
            throw new ZstdException(Zstd.errGeneric(), "Original size should not be negative");
        }
        final byte[] original = new byte[n3];
        final int decompressByteArray = this.decompressByteArray(original, 0, original.length, array, n, n2);
        if (decompressByteArray != n3) {
            return Arrays.copyOfRange(original, 0, decompressByteArray);
        }
        return original;
    }
    
    static {
        Native.load();
    }
}
