package R4;

import java.io.IOException;
import r4.AbstractC0983h;

/* JADX INFO: loaded from: classes.dex */
public final class w implements X4.u {

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final long f2748k;

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

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public final X4.e f2750m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public final X4.e f2751n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public boolean f2752o;
    public final /* synthetic */ y p;

    public w(y yVar, long j5, boolean z5) {
        AbstractC0983h.e("this$0", yVar);
        this.p = yVar;
        this.f2748k = j5;
        this.f2749l = z5;
        this.f2750m = new X4.e();
        this.f2751n = new X4.e();
    }

    public final void a(long j5) {
        byte[] bArr = L4.b.f2100a;
        this.p.f2755b.l(j5);
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public final void close() {
        long j5;
        y yVar = this.p;
        synchronized (yVar) {
            this.f2752o = true;
            X4.e eVar = this.f2751n;
            j5 = eVar.f3321l;
            eVar.r(j5);
            yVar.notifyAll();
        }
        if (j5 > 0) {
            a(j5);
        }
        this.p.a();
    }

    @Override // X4.u
    public final X4.w d() {
        return this.p.f2763k;
    }

    @Override // X4.u
    public final long p(X4.e eVar, long j5) throws Throwable {
        Throwable e2;
        boolean z5;
        long jP;
        AbstractC0983h.e("sink", eVar);
        do {
            y yVar = this.p;
            synchronized (yVar) {
                yVar.f2763k.h();
                try {
                    if (yVar.f() == null || this.f2749l) {
                        e2 = null;
                    } else {
                        e2 = yVar.f2766n;
                        if (e2 == null) {
                            EnumC0183b enumC0183bF = yVar.f();
                            AbstractC0983h.b(enumC0183bF);
                            e2 = new E(enumC0183bF);
                        }
                    }
                    if (this.f2752o) {
                        throw new IOException("stream closed");
                    }
                    X4.e eVar2 = this.f2751n;
                    long j6 = eVar2.f3321l;
                    z5 = false;
                    if (j6 > 0) {
                        jP = eVar2.p(eVar, Math.min(8192L, j6));
                        long j7 = yVar.f2756c + jP;
                        yVar.f2756c = j7;
                        long j8 = j7 - yVar.f2757d;
                        if (e2 == null && j8 >= yVar.f2755b.f2726z.a() / 2) {
                            yVar.f2755b.o(j8, yVar.f2754a);
                            yVar.f2757d = yVar.f2756c;
                        }
                    } else {
                        if (!this.f2749l && e2 == null) {
                            yVar.k();
                            z5 = true;
                        }
                        jP = -1;
                    }
                    yVar.f2763k.k();
                } finally {
                }
            }
        } while (z5);
        if (jP != -1) {
            a(jP);
            return jP;
        }
        if (e2 == null) {
            return -1L;
        }
        throw e2;
    }
}
