package Q4;

import O4.j;
import O4.l;
import X4.e;
import java.io.IOException;
import java.net.ProtocolException;
import java.util.concurrent.TimeUnit;
import r4.AbstractC0983h;

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

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

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public final /* synthetic */ l f2613o;

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public c(l lVar, long j5) {
        super(lVar);
        AbstractC0983h.e("this$0", lVar);
        this.f2613o = lVar;
        this.f2612n = j5;
        if (j5 == 0) {
            a();
        }
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public final void close() {
        if (this.f2608l) {
            return;
        }
        if (this.f2612n != 0) {
            TimeUnit timeUnit = TimeUnit.MILLISECONDS;
            if (!L4.b.f(this)) {
                ((j) this.f2613o.f2368c).k();
                a();
            }
        }
        this.f2608l = true;
    }

    @Override // Q4.a, X4.u
    public final long p(e eVar, long j5) throws IOException {
        AbstractC0983h.e("sink", eVar);
        if (this.f2608l) {
            throw new IllegalStateException("closed");
        }
        long j6 = this.f2612n;
        if (j6 == 0) {
            return -1L;
        }
        long jP = super.p(eVar, Math.min(j6, 8192L));
        if (jP == -1) {
            ((j) this.f2613o.f2368c).k();
            ProtocolException protocolException = new ProtocolException("unexpected end of stream");
            a();
            throw protocolException;
        }
        long j7 = this.f2612n - jP;
        this.f2612n = j7;
        if (j7 == 0) {
            a();
        }
        return jP;
    }
}
