package p5;

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

/* JADX INFO: loaded from: classes3.dex */
public final class z implements P {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final J f23129a;

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

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

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

    public z(J j4, Inflater inflater) {
        this.f23129a = j4;
        this.f23130b = inflater;
    }

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

    @Override // p5.P
    public final long read(C2740j sink, long j4) throws IOException {
        J j6;
        long j7;
        kotlin.jvm.internal.o.h(sink, "sink");
        do {
            Inflater inflater = this.f23130b;
            if (j4 < 0) {
                throw new IllegalArgumentException(A0.a.m("byteCount < 0: ", j4).toString());
            }
            if (this.f23132d) {
                throw new IllegalStateException("closed");
            }
            j6 = this.f23129a;
            if (j4 == 0) {
                j7 = 0;
            } else {
                try {
                    K kM = sink.M(1);
                    int iMin = (int) Math.min(j4, 8192 - kM.f23063c);
                    if (inflater.needsInput() && !j6.a()) {
                        K k6 = j6.f23059b.f23099a;
                        kotlin.jvm.internal.o.e(k6);
                        int i6 = k6.f23063c;
                        int i7 = k6.f23062b;
                        int i8 = i6 - i7;
                        this.f23131c = i8;
                        inflater.setInput(k6.f23061a, i7, i8);
                    }
                    int iInflate = inflater.inflate(kM.f23061a, kM.f23063c, iMin);
                    int i9 = this.f23131c;
                    if (i9 != 0) {
                        int remaining = i9 - inflater.getRemaining();
                        this.f23131c -= remaining;
                        j6.B(remaining);
                    }
                    if (iInflate > 0) {
                        kM.f23063c += iInflate;
                        j7 = iInflate;
                        sink.f23100b += j7;
                    } else {
                        if (kM.f23062b == kM.f23063c) {
                            sink.f23099a = kM.a();
                            L.a(kM);
                        }
                        j7 = 0;
                    }
                } catch (DataFormatException e6) {
                    throw new IOException(e6);
                }
            }
            if (j7 > 0) {
                return j7;
            }
            if (inflater.finished() || inflater.needsDictionary()) {
                return -1L;
            }
        } while (!j6.a());
        throw new EOFException("source exhausted prematurely");
    }

    @Override // p5.P
    public final S timeout() {
        return this.f23129a.f23058a.timeout();
    }
}
