package i;

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

/* JADX INFO: compiled from: GzipSource.kt */
/* JADX INFO: loaded from: classes.dex */
public final class n implements d0 {
    private byte b;

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

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

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

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

    public n(d0 d0Var) {
        h.x.d.k.d(d0Var, "source");
        this.f1032e = new x(d0Var);
        Inflater inflater = new Inflater(true);
        this.f1033f = inflater;
        this.f1034g = new o((h) this.f1032e, inflater);
        this.f1035h = 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));
        h.x.d.k.c(str2, "java.lang.String.format(this, *args)");
        throw new IOException(str2);
    }

    private final void b() throws IOException {
        this.f1032e.Y(10L);
        byte bN = this.f1032e.b.n(3L);
        boolean z = ((bN >> 1) & 1) == 1;
        if (z) {
            i(this.f1032e.b, 0L, 10L);
        }
        a("ID1ID2", 8075, this.f1032e.readShort());
        this.f1032e.skip(8L);
        if (((bN >> 2) & 1) == 1) {
            this.f1032e.Y(2L);
            if (z) {
                i(this.f1032e.b, 0L, 2L);
            }
            long jA = this.f1032e.b.A();
            this.f1032e.Y(jA);
            if (z) {
                i(this.f1032e.b, 0L, jA);
            }
            this.f1032e.skip(jA);
        }
        if (((bN >> 3) & 1) == 1) {
            long jA2 = this.f1032e.a((byte) 0);
            if (jA2 == -1) {
                throw new EOFException();
            }
            if (z) {
                i(this.f1032e.b, 0L, jA2 + 1);
            }
            this.f1032e.skip(jA2 + 1);
        }
        if (((bN >> 4) & 1) == 1) {
            long jA3 = this.f1032e.a((byte) 0);
            if (jA3 == -1) {
                throw new EOFException();
            }
            if (z) {
                i(this.f1032e.b, 0L, jA3 + 1);
            }
            this.f1032e.skip(jA3 + 1);
        }
        if (z) {
            a("FHCRC", this.f1032e.j(), (short) this.f1035h.getValue());
            this.f1035h.reset();
        }
    }

    private final void d() throws IOException {
        a("CRC", this.f1032e.i(), (int) this.f1035h.getValue());
        a("ISIZE", this.f1032e.i(), (int) this.f1033f.getBytesWritten());
    }

    private final void i(f fVar, long j2, long j3) {
        y yVar = fVar.b;
        h.x.d.k.b(yVar);
        while (true) {
            int i2 = yVar.f1053c;
            int i3 = yVar.b;
            if (j2 < i2 - i3) {
                break;
            }
            j2 -= (long) (i2 - i3);
            yVar = yVar.f1056f;
            h.x.d.k.b(yVar);
        }
        while (j3 > 0) {
            int i4 = (int) (((long) yVar.b) + j2);
            int iMin = (int) Math.min(yVar.f1053c - i4, j3);
            this.f1035h.update(yVar.a, i4, iMin);
            j3 -= (long) iMin;
            yVar = yVar.f1056f;
            h.x.d.k.b(yVar);
            j2 = 0;
        }
    }

    @Override // i.d0, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        this.f1034g.close();
    }

    @Override // i.d0
    public long read(f fVar, long j2) throws IOException {
        h.x.d.k.d(fVar, "sink");
        if (!(j2 >= 0)) {
            throw new IllegalArgumentException(("byteCount < 0: " + j2).toString());
        }
        if (j2 == 0) {
            return 0L;
        }
        if (this.b == 0) {
            b();
            this.b = (byte) 1;
        }
        if (this.b == 1) {
            long J = fVar.J();
            long j3 = this.f1034g.read(fVar, j2);
            if (j3 != -1) {
                i(fVar, J, j3);
                return j3;
            }
            this.b = (byte) 2;
        }
        if (this.b == 2) {
            d();
            this.b = (byte) 3;
            if (!this.f1032e.B()) {
                throw new IOException("gzip finished without exhausting source");
            }
        }
        return -1L;
    }

    @Override // i.d0
    public e0 timeout() {
        return this.f1032e.timeout();
    }
}
