package defpackage;

import androidx.media3.container.NalUnitUtil;
import java.io.EOFException;
import java.io.IOException;
import java.util.zip.CRC32;
import java.util.zip.Inflater;
import kotlin.Metadata;

/* JADX INFO: loaded from: classes3.dex */
@Metadata(d1 = {"\u0000\n\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\u0018\u00002\u00020\u0001¨\u0006\u0002"}, d2 = {"Lhz3;", "Lpx8;", "okio"}, k = 1, mv = {1, 9, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
@vx8
public final class hz3 implements px8 {
    public byte a;
    public final uu7 b;
    public final Inflater c;
    public final nl4 d;
    public final CRC32 e;

    public hz3(px8 px8Var) {
        cr4.g(px8Var, "source");
        uu7 uu7Var = new uu7(px8Var);
        this.b = uu7Var;
        Inflater inflater = new Inflater(true);
        this.c = inflater;
        this.d = new nl4(uu7Var, inflater);
        this.e = new CRC32();
    }

    public static void a(int i, int i2, String str) throws IOException {
        if (i2 == i) {
            return;
        }
        StringBuilder sbL = fi7.l(str, ": actual 0x");
        sbL.append(n89.ag(8, C0696t.d(i2)));
        sbL.append(" != expected 0x");
        sbL.append(n89.ag(8, C0696t.d(i)));
        throw new IOException(sbL.toString());
    }

    public final void b(long j, lg0 lg0Var, long j2) {
        me8 me8Var = lg0Var.a;
        cr4.d(me8Var);
        while (true) {
            int i = me8Var.c;
            int i2 = me8Var.b;
            if (j < i - i2) {
                break;
            }
            j -= (long) (i - i2);
            me8Var = me8Var.f;
            cr4.d(me8Var);
        }
        while (j2 > 0) {
            int i3 = (int) (((long) me8Var.b) + j);
            int iMin = (int) Math.min(me8Var.c - i3, j2);
            this.e.update(me8Var.a, i3, iMin);
            j2 -= (long) iMin;
            me8Var = me8Var.f;
            cr4.d(me8Var);
            j = 0;
        }
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public final void close() {
        this.d.close();
    }

    @Override // defpackage.px8
    public final long read(lg0 lg0Var, long j) throws IOException {
        hz3 hz3Var = this;
        cr4.g(lg0Var, "sink");
        if (j < 0) {
            throw new IllegalArgumentException(n41.j(j, "byteCount < 0: ").toString());
        }
        if (j == 0) {
            return 0L;
        }
        byte b = hz3Var.a;
        CRC32 crc32 = hz3Var.e;
        uu7 uu7Var = hz3Var.b;
        if (b == 0) {
            uu7Var.i(10L);
            lg0 lg0Var2 = uu7Var.b;
            byte bK = lg0Var2.k(3L);
            boolean z = ((bK >> 1) & 1) == 1;
            if (z) {
                hz3Var.b(0L, lg0Var2, 10L);
            }
            a(8075, uu7Var.readShort(), "ID1ID2");
            uu7Var.skip(8L);
            if (((bK >> 2) & 1) == 1) {
                uu7Var.i(2L);
                if (z) {
                    b(0L, lg0Var2, 2L);
                }
                long jW = lg0Var2.w() & 65535;
                uu7Var.i(jW);
                if (z) {
                    b(0L, lg0Var2, jW);
                }
                uu7Var.skip(jW);
            }
            if (((bK >> 3) & 1) == 1) {
                long jA = uu7Var.a((byte) 0, 0L, Long.MAX_VALUE);
                if (jA == -1) {
                    throw new EOFException();
                }
                if (z) {
                    b(0L, lg0Var2, jA + 1);
                }
                uu7Var.skip(jA + 1);
            }
            if (((bK >> 4) & 1) == 1) {
                long jA2 = uu7Var.a((byte) 0, 0L, Long.MAX_VALUE);
                if (jA2 == -1) {
                    throw new EOFException();
                }
                if (z) {
                    hz3Var = this;
                    hz3Var.b(0L, lg0Var2, jA2 + 1);
                } else {
                    hz3Var = this;
                }
                uu7Var.skip(jA2 + 1);
            } else {
                hz3Var = this;
            }
            if (z) {
                a(uu7Var.h(), (short) crc32.getValue(), "FHCRC");
                crc32.reset();
            }
            hz3Var.a = (byte) 1;
        }
        if (hz3Var.a == 1) {
            long j2 = lg0Var.b;
            long j3 = hz3Var.d.read(lg0Var, j);
            if (j3 != -1) {
                hz3Var.b(j2, lg0Var, j3);
                return j3;
            }
            hz3Var.a = (byte) 2;
        }
        if (hz3Var.a == 2) {
            a(uu7Var.b(), (int) crc32.getValue(), "CRC");
            a(uu7Var.b(), (int) hz3Var.c.getBytesWritten(), "ISIZE");
            hz3Var.a = (byte) 3;
            if (!uu7Var.j()) {
                throw new IOException("gzip finished without exhausting source");
            }
        }
        return -1L;
    }

    @Override // defpackage.px8
    /* JADX INFO: renamed from: timeout */
    public final ko9 getTimeout() {
        return this.b.a.getTimeout();
    }
}
