package j;

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 o implements b0 {

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public boolean f4626e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final g f4627f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final Inflater f4628g;

    public o(g gVar, Inflater inflater) {
        g.h0.c.l.f(gVar, "source");
        g.h0.c.l.f(inflater, "inflater");
        this.f4627f = gVar;
        this.f4628g = inflater;
    }

    public final long a(d dVar, long j2) throws IOException {
        g.h0.c.l.f(dVar, "sink");
        if (!(j2 >= 0)) {
            throw new IllegalArgumentException(d.b.a.a.a.l("byteCount < 0: ", j2).toString());
        }
        if (!(!this.f4626e)) {
            throw new IllegalStateException("closed".toString());
        }
        if (j2 == 0) {
            return 0L;
        }
        try {
            w wVarX = dVar.x(1);
            int iMin = (int) Math.min(j2, 8192 - wVarX.f4650c);
            if (this.f4628g.needsInput() && !this.f4627f.n()) {
                w wVar = this.f4627f.getBuffer().f4600b;
                g.h0.c.l.c(wVar);
                int i2 = wVar.f4650c;
                int i3 = wVar.f4649b;
                int i4 = i2 - i3;
                this.f4625b = i4;
                this.f4628g.setInput(wVar.a, i3, i4);
            }
            int iInflate = this.f4628g.inflate(wVarX.a, wVarX.f4650c, iMin);
            int i5 = this.f4625b;
            if (i5 != 0) {
                int remaining = i5 - this.f4628g.getRemaining();
                this.f4625b -= remaining;
                this.f4627f.K(remaining);
            }
            if (iInflate > 0) {
                wVarX.f4650c += iInflate;
                long j3 = iInflate;
                dVar.f4601e += j3;
                return j3;
            }
            if (wVarX.f4649b == wVarX.f4650c) {
                dVar.f4600b = wVarX.a();
                x.a(wVarX);
            }
            return 0L;
        } catch (DataFormatException e2) {
            throw new IOException(e2);
        }
    }

    @Override // j.b0, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        if (this.f4626e) {
            return;
        }
        this.f4628g.end();
        this.f4626e = true;
        this.f4627f.close();
    }

    @Override // j.b0
    public long read(d dVar, long j2) throws IOException {
        g.h0.c.l.f(dVar, "sink");
        do {
            long jA = a(dVar, j2);
            if (jA > 0) {
                return jA;
            }
            if (this.f4628g.finished() || this.f4628g.needsDictionary()) {
                return -1L;
            }
        } while (!this.f4627f.n());
        throw new EOFException("source exhausted prematurely");
    }

    @Override // j.b0
    public c0 timeout() {
        return this.f4627f.timeout();
    }

    public o(b0 b0Var, Inflater inflater) {
        g.h0.c.l.f(b0Var, "source");
        g.h0.c.l.f(inflater, "inflater");
        g gVarT = f.a.y.a.t(b0Var);
        g.h0.c.l.f(gVarT, "source");
        g.h0.c.l.f(inflater, "inflater");
        this.f4627f = gVarT;
        this.f4628g = inflater;
    }
}
