package i;

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 d0 {
    private int b;

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

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

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

    public o(h hVar, Inflater inflater) {
        h.x.d.k.d(hVar, "source");
        h.x.d.k.d(inflater, "inflater");
        this.f1037f = hVar;
        this.f1038g = inflater;
    }

    private final void d() throws IOException {
        int i2 = this.b;
        if (i2 == 0) {
            return;
        }
        int remaining = i2 - this.f1038g.getRemaining();
        this.b -= remaining;
        this.f1037f.skip(remaining);
    }

    public final long a(f fVar, long j2) throws IOException {
        h.x.d.k.d(fVar, "sink");
        if (!(j2 >= 0)) {
            throw new IllegalArgumentException(("byteCount < 0: " + j2).toString());
        }
        if (!(!this.f1036e)) {
            throw new IllegalStateException("closed".toString());
        }
        if (j2 == 0) {
            return 0L;
        }
        try {
            y yVarP = fVar.P(1);
            int iMin = (int) Math.min(j2, 8192 - yVarP.f1053c);
            b();
            int iInflate = this.f1038g.inflate(yVarP.a, yVarP.f1053c, iMin);
            d();
            if (iInflate > 0) {
                yVarP.f1053c += iInflate;
                long j3 = iInflate;
                fVar.H(fVar.J() + j3);
                return j3;
            }
            if (yVarP.b == yVarP.f1053c) {
                fVar.b = yVarP.b();
                z.b(yVarP);
            }
            return 0L;
        } catch (DataFormatException e2) {
            throw new IOException(e2);
        }
    }

    public final boolean b() throws IOException {
        if (!this.f1038g.needsInput()) {
            return false;
        }
        if (this.f1037f.B()) {
            return true;
        }
        y yVar = this.f1037f.c().b;
        h.x.d.k.b(yVar);
        int i2 = yVar.f1053c;
        int i3 = yVar.b;
        int i4 = i2 - i3;
        this.b = i4;
        this.f1038g.setInput(yVar.a, i3, i4);
        return false;
    }

    @Override // i.d0, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        if (this.f1036e) {
            return;
        }
        this.f1038g.end();
        this.f1036e = true;
        this.f1037f.close();
    }

    @Override // i.d0
    public long read(f fVar, long j2) throws IOException {
        h.x.d.k.d(fVar, "sink");
        do {
            long jA = a(fVar, j2);
            if (jA > 0) {
                return jA;
            }
            if (this.f1038g.finished() || this.f1038g.needsDictionary()) {
                return -1L;
            }
        } while (!this.f1037f.B());
        throw new EOFException("source exhausted prematurely");
    }

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

    /* JADX WARN: 'this' call moved to the top of the method (can break code semantics) */
    public o(d0 d0Var, Inflater inflater) {
        this(q.d(d0Var), inflater);
        h.x.d.k.d(d0Var, "source");
        h.x.d.k.d(inflater, "inflater");
    }
}
