package x7;

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

/* JADX INFO: loaded from: classes.dex */
public final class r implements H, AutoCloseable {

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public final B f18785q;

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

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

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

    public r(B b8, Inflater inflater) {
        this.f18785q = b8;
        this.f18786r = inflater;
    }

    @Override // x7.H
    public final long M(C1731g c1731g, long j8) throws IOException {
        long j9;
        P6.g.e(c1731g, "sink");
        while (j8 >= 0) {
            if (this.f18788t) {
                throw new IllegalStateException("closed");
            }
            B b8 = this.f18785q;
            Inflater inflater = this.f18786r;
            if (j8 == 0) {
                j9 = 0;
            } else {
                try {
                    C cQ0 = c1731g.q0(1);
                    int iMin = (int) Math.min(j8, 8192 - cQ0.f18729c);
                    if (inflater.needsInput() && !b8.a()) {
                        C c3 = b8.f18725r.f18764q;
                        P6.g.b(c3);
                        int i = c3.f18729c;
                        int i5 = c3.f18728b;
                        int i8 = i - i5;
                        this.f18787s = i8;
                        inflater.setInput(c3.f18727a, i5, i8);
                    }
                    int iInflate = inflater.inflate(cQ0.f18727a, cQ0.f18729c, iMin);
                    int i9 = this.f18787s;
                    if (i9 != 0) {
                        int remaining = i9 - inflater.getRemaining();
                        this.f18787s -= remaining;
                        b8.y(remaining);
                    }
                    if (iInflate > 0) {
                        cQ0.f18729c += iInflate;
                        j9 = iInflate;
                        c1731g.f18765r += j9;
                    } else {
                        if (cQ0.f18728b == cQ0.f18729c) {
                            c1731g.f18764q = cQ0.a();
                            D.a(cQ0);
                        }
                        j9 = 0;
                    }
                } catch (DataFormatException e8) {
                    throw new IOException(e8);
                }
            }
            if (j9 > 0) {
                return j9;
            }
            if (inflater.finished() || inflater.needsDictionary()) {
                return -1L;
            }
            if (b8.a()) {
                throw new EOFException("source exhausted prematurely");
            }
        }
        throw new IllegalArgumentException(AbstractC0844a.i(j8, "byteCount < 0: ").toString());
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public final void close() {
        if (this.f18788t) {
            return;
        }
        this.f18786r.end();
        this.f18788t = true;
        this.f18785q.close();
    }

    @Override // x7.H
    public final J f() {
        return this.f18785q.f18724q.f();
    }
}
