package okio;

import java.io.EOFException;
import java.io.IOException;
import java.util.Arrays;
import java.util.zip.CRC32;
import java.util.zip.Inflater;

/* JADX INFO: loaded from: classes2.dex */
public final class n implements E {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private byte f6040b;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private final x f6041g;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    private final Inflater f6042r;

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    private final o f6043x;

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    private final CRC32 f6044y;

    public n(E source) {
        kotlin.jvm.internal.l.e(source, "source");
        x xVar = new x(source);
        this.f6041g = xVar;
        Inflater inflater = new Inflater(true);
        this.f6042r = inflater;
        this.f6043x = new o((h) xVar, inflater);
        this.f6044y = new CRC32();
    }

    private final void a(String str, int i2, int i3) throws IOException {
        if (i3 == i2) {
            return;
        }
        String str2 = String.format("%s: actual 0x%08x != expected 0x%08x", Arrays.copyOf(new Object[]{str, Integer.valueOf(i3), Integer.valueOf(i2)}, 3));
        kotlin.jvm.internal.l.d(str2, "java.lang.String.format(this, *args)");
        throw new IOException(str2);
    }

    private final void b() throws IOException {
        this.f6041g.W(10L);
        byte bJ = this.f6041g.f6066b.j(3L);
        boolean z2 = ((bJ >> 1) & 1) == 1;
        if (z2) {
            e(this.f6041g.f6066b, 0L, 10L);
        }
        a("ID1ID2", 8075, this.f6041g.readShort());
        this.f6041g.skip(8L);
        if (((bJ >> 2) & 1) == 1) {
            this.f6041g.W(2L);
            if (z2) {
                e(this.f6041g.f6066b, 0L, 2L);
            }
            long jZ = this.f6041g.f6066b.z();
            this.f6041g.W(jZ);
            if (z2) {
                e(this.f6041g.f6066b, 0L, jZ);
            }
            this.f6041g.skip(jZ);
        }
        if (((bJ >> 3) & 1) == 1) {
            long jA = this.f6041g.a((byte) 0);
            if (jA == -1) {
                throw new EOFException();
            }
            if (z2) {
                e(this.f6041g.f6066b, 0L, jA + 1);
            }
            this.f6041g.skip(jA + 1);
        }
        if (((bJ >> 4) & 1) == 1) {
            long jA2 = this.f6041g.a((byte) 0);
            if (jA2 == -1) {
                throw new EOFException();
            }
            if (z2) {
                e(this.f6041g.f6066b, 0L, jA2 + 1);
            }
            this.f6041g.skip(jA2 + 1);
        }
        if (z2) {
            a("FHCRC", this.f6041g.f(), (short) this.f6044y.getValue());
            this.f6044y.reset();
        }
    }

    private final void d() throws IOException {
        a("CRC", this.f6041g.e(), (int) this.f6044y.getValue());
        a("ISIZE", this.f6041g.e(), (int) this.f6042r.getBytesWritten());
    }

    private final void e(C0688f c0688f, long j2, long j3) {
        y yVar = c0688f.f6021b;
        kotlin.jvm.internal.l.b(yVar);
        while (true) {
            int i2 = yVar.f6073c;
            int i3 = yVar.f6072b;
            if (j2 < i2 - i3) {
                break;
            }
            j2 -= (long) (i2 - i3);
            yVar = yVar.f6076f;
            kotlin.jvm.internal.l.b(yVar);
        }
        while (j3 > 0) {
            int i4 = (int) (((long) yVar.f6072b) + j2);
            int iMin = (int) Math.min(yVar.f6073c - i4, j3);
            this.f6044y.update(yVar.f6071a, i4, iMin);
            j3 -= (long) iMin;
            yVar = yVar.f6076f;
            kotlin.jvm.internal.l.b(yVar);
            j2 = 0;
        }
    }

    @Override // okio.E, java.io.Closeable, java.lang.AutoCloseable
    public void close() {
        this.f6043x.close();
    }

    @Override // okio.E
    public long read(C0688f sink, long j2) throws IOException {
        kotlin.jvm.internal.l.e(sink, "sink");
        if (!(j2 >= 0)) {
            throw new IllegalArgumentException(("byteCount < 0: " + j2).toString());
        }
        if (j2 == 0) {
            return 0L;
        }
        if (this.f6040b == 0) {
            b();
            this.f6040b = (byte) 1;
        }
        if (this.f6040b == 1) {
            long jG = sink.G();
            long j3 = this.f6043x.read(sink, j2);
            if (j3 != -1) {
                e(sink, jG, j3);
                return j3;
            }
            this.f6040b = (byte) 2;
        }
        if (this.f6040b == 2) {
            d();
            this.f6040b = (byte) 3;
            if (!this.f6041g.k()) {
                throw new IOException("gzip finished without exhausting source");
            }
        }
        return -1L;
    }

    @Override // okio.E
    public F timeout() {
        return this.f6041g.timeout();
    }
}
