package com.bytedance.zoin.zstd;

import X.7p5;
import X.RVJ;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

public class ZstdInputStream extends FilterInputStream {
    public static final int srcBuffSize = ((int) recommendedDInSize());
    public long dstPos;
    public boolean finalize = true;
    public boolean frameFinished = true;
    public boolean isClosed;
    public boolean isContinuous;
    public boolean needRead = true;
    public byte[] src = new byte[srcBuffSize];
    public long srcPos;
    public long srcSize;
    public final long stream;

    public static native long createDStream();

    private native int decompressStream(long j, byte[] bArr, int i, byte[] bArr2, int i2);

    public static native int freeDStream(long j);

    private native int initDStream(long j);

    public static native long recommendedDInSize();

    public static native long recommendedDOutSize();

    public boolean markSupported() {
        return false;
    }

    public void finalize() {
        if (this.finalize) {
            close();
        }
    }

    public synchronized boolean getContinuous() {
        boolean z;
        MethodCollector.i(10752);
        z = this.isContinuous;
        MethodCollector.o(10752);
        return z;
    }

    static {
        MethodCollector.i(12108);
        Covode.recordClassIndex(40581);
        7p5.LIZ();
        MethodCollector.o(12108);
    }

    public synchronized int available() {
        MethodCollector.i(11660);
        if (this.isClosed) {
            IOException iOException = new IOException("Stream closed");
            MethodCollector.o(11660);
            throw iOException;
        } else if (!this.needRead) {
            MethodCollector.o(11660);
            return 1;
        } else {
            int available = this.in.available();
            MethodCollector.o(11660);
            return available;
        }
    }

    public synchronized void close() {
        MethodCollector.i(11896);
        if (this.isClosed) {
            MethodCollector.o(11896);
            return;
        }
        freeDStream(this.stream);
        this.in.close();
        this.isClosed = true;
        MethodCollector.o(11896);
    }

    public synchronized int read() {
        int readInternal;
        MethodCollector.i(11659);
        byte[] bArr = new byte[1];
        while (true) {
            readInternal = readInternal(bArr, 0, 1);
            if (readInternal != 0) {
                break;
            }
        }
        if (readInternal == 1) {
            byte b = bArr[0] & 255;
            MethodCollector.o(11659);
            return b;
        }
        MethodCollector.o(11659);
        return -1;
    }

    public void setFinalize(boolean z) {
        this.finalize = z;
    }

    public synchronized ZstdInputStream setContinuous(boolean z) {
        MethodCollector.i(10526);
        this.isContinuous = z;
        MethodCollector.o(10526);
        return this;
    }

    public ZstdInputStream(InputStream inputStream) {
        super(inputStream);
        MethodCollector.i(10524);
        synchronized (this) {
            try {
                long createDStream = createDStream();
                this.stream = createDStream;
                initDStream(createDStream);
            } finally {
                MethodCollector.o(10524);
            }
        }
    }

    public synchronized ZstdInputStream setDict(byte[] bArr) {
        MethodCollector.i(10956);
        long loadDictDecompress = (long) Zstd.loadDictDecompress(this.stream, bArr, bArr.length);
        if (!Zstd.isError(loadDictDecompress)) {
            MethodCollector.o(10956);
        } else {
            IOException iOException = new IOException("Decompression error: " + Zstd.getErrorName(loadDictDecompress));
            MethodCollector.o(10956);
            throw iOException;
        }
        return this;
    }

    public synchronized ZstdInputStream setDict(ZstdDictDecompress zstdDictDecompress) {
        int i;
        MethodCollector.i(11185);
        do {
            i = zstdDictDecompress.LIZIZ;
            if (i < 0) {
                IllegalStateException illegalStateException = new IllegalStateException("Closed");
                MethodCollector.o(11185);
                throw illegalStateException;
            } else if (i == Integer.MAX_VALUE) {
                IllegalStateException illegalStateException2 = new IllegalStateException("Shared lock overflow");
                MethodCollector.o(11185);
                throw illegalStateException2;
            }
        } while (!RVJ.LIZ.compareAndSet(zstdDictDecompress, i, i + 1));
        try {
            long loadFastDictDecompress = (long) Zstd.loadFastDictDecompress(this.stream, zstdDictDecompress);
            if (Zstd.isError(loadFastDictDecompress)) {
                IOException iOException = new IOException("Decompression error: " + Zstd.getErrorName(loadFastDictDecompress));
                MethodCollector.o(11185);
                throw iOException;
            }
        } finally {
            zstdDictDecompress.LIZ();
            MethodCollector.o(11185);
        }
        return this;
    }

    public synchronized long skip(long j) {
        int read;
        MethodCollector.i(11661);
        if (this.isClosed) {
            IOException iOException = new IOException("Stream closed");
            MethodCollector.o(11661);
            throw iOException;
        } else if (j <= 0) {
            MethodCollector.o(11661);
            return 0;
        } else {
            int min = (int) Math.min(recommendedDOutSize(), j);
            byte[] bArr = new byte[min];
            long j2 = j;
            while (j2 > 0 && (read = read(bArr, 0, (int) Math.min((long) min, j2))) >= 0) {
                j2 -= (long) read;
            }
            long j3 = j - j2;
            MethodCollector.o(11661);
            return j3;
        }
    }

    /* JADX WARNING: Unknown top exception splitter block from list: {B:19:0x0020=Splitter:B:19:0x0020, B:12:0x0014=Splitter:B:12:0x0014} */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public synchronized int read(byte[] r5, int r6, int r7) {
        /*
            r4 = this;
            monitor-enter(r4)
            r3 = 11426(0x2ca2, float:1.6011E-41)
            com.bytedance.frameworks.apm.trace.MethodCollector.i(r3)     // Catch:{ all -> 0x0048 }
            if (r6 < 0) goto L_0x0020
            int r0 = r5.length     // Catch:{ all -> 0x0048 }
            int r0 = r0 - r6
            if (r7 > r0) goto L_0x0020
            r0 = 0
            if (r7 != 0) goto L_0x0014
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r3)     // Catch:{ all -> 0x0048 }
            monitor-exit(r4)
            return r0
        L_0x0014:
            int r0 = r4.readInternal(r5, r6, r7)     // Catch:{ all -> 0x0048 }
            if (r0 != 0) goto L_0x001b
            goto L_0x0014
        L_0x001b:
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r3)     // Catch:{ all -> 0x0048 }
            monitor-exit(r4)
            return r0
        L_0x0020:
            java.lang.IndexOutOfBoundsException r2 = new java.lang.IndexOutOfBoundsException     // Catch:{ all -> 0x0048 }
            java.lang.StringBuilder r1 = new java.lang.StringBuilder     // Catch:{ all -> 0x0048 }
            java.lang.String r0 = "Requested lenght "
            r1.<init>(r0)     // Catch:{ all -> 0x0048 }
            r1.append(r7)     // Catch:{ all -> 0x0048 }
            java.lang.String r0 = " from offset "
            r1.append(r0)     // Catch:{ all -> 0x0048 }
            r1.append(r6)     // Catch:{ all -> 0x0048 }
            java.lang.String r0 = " in buffer of size "
            r1.append(r0)     // Catch:{ all -> 0x0048 }
            int r0 = r5.length     // Catch:{ all -> 0x0048 }
            r1.append(r0)     // Catch:{ all -> 0x0048 }
            java.lang.String r0 = r1.toString()     // Catch:{ all -> 0x0048 }
            r2.<init>(r0)     // Catch:{ all -> 0x0048 }
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r3)     // Catch:{ all -> 0x0048 }
            throw r2     // Catch:{ all -> 0x0048 }
        L_0x0048:
            r0 = move-exception
            monitor-exit(r4)
            throw r0
        */
        throw new UnsupportedOperationException("Method not decompiled: com.bytedance.zoin.zstd.ZstdInputStream.read(byte[], int, int):int");
    }

    public int readInternal(byte[] bArr, int i, int i2) {
        long j;
        int i3 = 11658;
        MethodCollector.i(11658);
        if (!this.isClosed) {
            int i4 = i;
            int i5 = i2;
            byte[] bArr2 = bArr;
            if (i4 < 0 || i5 > bArr2.length - i4) {
                IndexOutOfBoundsException indexOutOfBoundsException = new IndexOutOfBoundsException("Requested lenght " + i5 + " from offset " + i4 + " in buffer of size " + bArr2.length);
                MethodCollector.o(11658);
                throw indexOutOfBoundsException;
            }
            int i6 = i4 + i5;
            long j2 = (long) i4;
            this.dstPos = j2;
            long j3 = -1;
            while (true) {
                j = this.dstPos;
                long j4 = (long) i6;
                if (j >= j4 || j3 >= j) {
                    int i7 = (int) (j - j2);
                    MethodCollector.o(11658);
                } else {
                    boolean z = false;
                    if (this.needRead && (this.in.available() > 0 || this.dstPos == j2)) {
                        long read = (long) this.in.read(this.src, 0, srcBuffSize);
                        this.srcSize = read;
                        this.srcPos = 0;
                        if (read < 0) {
                            this.srcSize = 0;
                            if (this.frameFinished) {
                                MethodCollector.o(i3);
                                return -1;
                            } else if (this.isContinuous) {
                                int i8 = (int) (this.dstPos - j2);
                                MethodCollector.o(i3);
                                return i8;
                            } else {
                                IOException iOException = new IOException("Read error or truncated source");
                                MethodCollector.o(i3);
                                throw iOException;
                            }
                        } else {
                            this.frameFinished = false;
                        }
                    }
                    j3 = this.dstPos;
                    int i9 = i6;
                    byte[] bArr3 = bArr2;
                    int decompressStream = decompressStream(this.stream, bArr3, i9, this.src, (int) this.srcSize);
                    long j5 = (long) decompressStream;
                    if (Zstd.isError(j5)) {
                        IOException iOException2 = new IOException("Decompression error: " + Zstd.getErrorName(j5));
                        MethodCollector.o(11658);
                        throw iOException2;
                    } else if (decompressStream == 0) {
                        this.frameFinished = true;
                        if (this.srcPos == this.srcSize) {
                            z = true;
                        }
                        this.needRead = z;
                        int i10 = (int) (this.dstPos - j2);
                        MethodCollector.o(11658);
                        return i10;
                    } else {
                        if (this.dstPos < j4) {
                            z = true;
                        }
                        this.needRead = z;
                        i3 = 11658;
                    }
                }
            }
            int i72 = (int) (j - j2);
            MethodCollector.o(11658);
            return i72;
        }
        IOException iOException3 = new IOException("Stream closed");
        MethodCollector.o(11658);
        throw iOException3;
    }
}
