package q7;

import P6.g;
import d5.AbstractC0844a;
import java.io.IOException;
import java.net.ProtocolException;
import java.util.concurrent.TimeUnit;
import o7.l;
import x7.C1731g;

/* JADX INFO: loaded from: classes.dex */
public final class d extends a {

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

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public final /* synthetic */ F4.a f16340u;

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public d(F4.a aVar, long j8) {
        super(aVar);
        this.f16340u = aVar;
        this.f16339t = j8;
        if (j8 == 0) {
            a();
        }
    }

    @Override // q7.a, x7.H
    public final long M(C1731g c1731g, long j8) throws IOException {
        g.e(c1731g, "sink");
        if (j8 < 0) {
            throw new IllegalArgumentException(AbstractC0844a.i(j8, "byteCount < 0: ").toString());
        }
        if (this.f16330r) {
            throw new IllegalStateException("closed");
        }
        long j9 = this.f16339t;
        if (j9 == 0) {
            return -1L;
        }
        long jM = super.M(c1731g, Math.min(j9, j8));
        if (jM == -1) {
            ((l) this.f16340u.f2496c).l();
            ProtocolException protocolException = new ProtocolException("unexpected end of stream");
            a();
            throw protocolException;
        }
        long j10 = this.f16339t - jM;
        this.f16339t = j10;
        if (j10 == 0) {
            a();
        }
        return jM;
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public final void close() {
        if (this.f16330r) {
            return;
        }
        if (this.f16339t != 0) {
            TimeUnit timeUnit = TimeUnit.MILLISECONDS;
            if (!l7.b.h(this)) {
                ((l) this.f16340u.f2496c).l();
                a();
            }
        }
        this.f16330r = true;
    }
}
