package l;

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 {
    private int b;
    private boolean d;
    private final h e;
    private final Inflater f;

    public n(h hVar, Inflater inflater) {
        j.p0.d.r.g(hVar, "source");
        j.p0.d.r.g(inflater, "inflater");
        this.e = hVar;
        this.f = inflater;
    }

    private final void f() throws IOException {
        int i2 = this.b;
        if (i2 == 0) {
            return;
        }
        int remaining = i2 - this.f.getRemaining();
        this.b -= remaining;
        this.e.k(remaining);
    }

    public final boolean c() throws IOException {
        if (!this.f.needsInput()) {
            return false;
        }
        f();
        if (!(this.f.getRemaining() == 0)) {
            throw new IllegalStateException("?".toString());
        }
        if (this.e.E()) {
            return true;
        }
        v vVar = this.e.a().b;
        if (vVar == null) {
            j.p0.d.r.p();
            throw null;
        }
        int i2 = vVar.c;
        int i3 = vVar.b;
        int i4 = i2 - i3;
        this.b = i4;
        this.f.setInput(vVar.a, i3, i4);
        return false;
    }

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

    @Override // l.a0
    public long read(f fVar, long j2) throws IOException {
        boolean zC;
        j.p0.d.r.g(fVar, "sink");
        if (!(j2 >= 0)) {
            throw new IllegalArgumentException(("byteCount < 0: " + j2).toString());
        }
        if (!(!this.d)) {
            throw new IllegalStateException("closed".toString());
        }
        if (j2 == 0) {
            return 0L;
        }
        do {
            zC = c();
            try {
                v vVarP = fVar.P(1);
                int iInflate = this.f.inflate(vVarP.a, vVarP.c, (int) Math.min(j2, 8192 - vVarP.c));
                if (iInflate > 0) {
                    vVarP.c += iInflate;
                    long j3 = iInflate;
                    fVar.I(fVar.K() + j3);
                    return j3;
                }
                if (!this.f.finished() && !this.f.needsDictionary()) {
                }
                f();
                if (vVarP.b != vVarP.c) {
                    return -1L;
                }
                fVar.b = vVarP.b();
                w.c.a(vVarP);
                return -1L;
            } catch (DataFormatException e) {
                throw new IOException(e);
            }
        } while (!zC);
        throw new EOFException("source exhausted prematurely");
    }

    @Override // l.a0
    public b0 timeout() {
        return this.e.timeout();
    }
}
