package d;

import java.io.EOFException;
import java.io.IOException;
import java.util.zip.DataFormatException;
import java.util.zip.Inflater;

/* JADX INFO: compiled from: InflaterSource.kt */
/* JADX INFO: loaded from: classes.dex */
public final class n implements a0 {

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private boolean f1584c;

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

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

    public n(h hVar, Inflater inflater) {
        c.q.d.j.c(hVar, "source");
        c.q.d.j.c(inflater, "inflater");
        this.f1585d = hVar;
        this.f1586e = inflater;
    }

    private final void F() throws IOException {
        int i = this.f1583b;
        if (i == 0) {
            return;
        }
        int remaining = i - this.f1586e.getRemaining();
        this.f1583b -= remaining;
        this.f1585d.l(remaining);
    }

    @Override // d.a0, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        if (this.f1584c) {
            return;
        }
        this.f1586e.end();
        this.f1584c = true;
        this.f1585d.close();
    }

    @Override // d.a0
    public long read(f fVar, long j) throws IOException {
        boolean zV;
        c.q.d.j.c(fVar, "sink");
        if (!(j >= 0)) {
            throw new IllegalArgumentException(("byteCount < 0: " + j).toString());
        }
        if (!(!this.f1584c)) {
            throw new IllegalStateException("closed".toString());
        }
        if (j == 0) {
            return 0L;
        }
        do {
            zV = v();
            try {
                v vVarE0 = fVar.e0(1);
                int iInflate = this.f1586e.inflate(vVarE0.f1606a, vVarE0.f1608c, (int) Math.min(j, 8192 - vVarE0.f1608c));
                if (iInflate > 0) {
                    vVarE0.f1608c += iInflate;
                    long j2 = iInflate;
                    fVar.a0(fVar.b0() + j2);
                    return j2;
                }
                if (!this.f1586e.finished() && !this.f1586e.needsDictionary()) {
                }
                F();
                if (vVarE0.f1607b != vVarE0.f1608c) {
                    return -1L;
                }
                fVar.f1563b = vVarE0.b();
                w.a(vVarE0);
                return -1L;
            } catch (DataFormatException e2) {
                throw new IOException(e2);
            }
        } while (!zV);
        throw new EOFException("source exhausted prematurely");
    }

    @Override // d.a0
    public b0 timeout() {
        return this.f1585d.timeout();
    }

    public final boolean v() throws IOException {
        if (!this.f1586e.needsInput()) {
            return false;
        }
        F();
        if (!(this.f1586e.getRemaining() == 0)) {
            throw new IllegalStateException("?".toString());
        }
        if (this.f1585d.y()) {
            return true;
        }
        v vVar = this.f1585d.a().f1563b;
        if (vVar == null) {
            c.q.d.j.g();
            throw null;
        }
        int i = vVar.f1608c;
        int i2 = vVar.f1607b;
        int i3 = i - i2;
        this.f1583b = i3;
        this.f1586e.setInput(vVar.f1606a, i2, i3);
        return false;
    }
}
