package r7;

import d5.AbstractC0844a;
import java.io.IOException;
import x7.C1731g;
import x7.H;
import x7.J;

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

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

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

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public final C1731g f16680s = new C1731g();

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public final C1731g f16681t = new C1731g();

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

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public final /* synthetic */ w f16683v;

    public u(w wVar, long j8, boolean z8) {
        this.f16683v = wVar;
        this.f16678q = j8;
        this.f16679r = z8;
    }

    @Override // x7.H
    public final long M(C1731g c1731g, long j8) throws Throwable {
        Throwable b8;
        boolean z8;
        long jM;
        P6.g.e(c1731g, "sink");
        long j9 = 0;
        if (j8 < 0) {
            throw new IllegalArgumentException(AbstractC0844a.i(j8, "byteCount < 0: ").toString());
        }
        while (true) {
            w wVar = this.f16683v;
            synchronized (wVar) {
                wVar.f16694k.h();
                try {
                    if (wVar.f() == 0 || this.f16679r) {
                        b8 = null;
                    } else {
                        b8 = wVar.f16697n;
                        if (b8 == null) {
                            int iF = wVar.f();
                            I1.a.z(iF);
                            b8 = new B(iF);
                        }
                    }
                    if (this.f16682u) {
                        throw new IOException("stream closed");
                    }
                    C1731g c1731g2 = this.f16681t;
                    long j10 = c1731g2.f18765r;
                    z8 = false;
                    if (j10 > j9) {
                        jM = c1731g2.M(c1731g, Math.min(j8, j10));
                        long j11 = wVar.f16687c + jM;
                        wVar.f16687c = j11;
                        long j12 = j11 - wVar.f16688d;
                        if (b8 == null && j12 >= wVar.f16686b.f16637F.a() / 2) {
                            wVar.f16686b.A(j12, wVar.f16685a);
                            wVar.f16688d = wVar.f16687c;
                        }
                    } else {
                        if (!this.f16679r && b8 == null) {
                            wVar.l();
                            z8 = true;
                        }
                        jM = -1;
                    }
                    wVar.f16694k.k();
                } finally {
                }
            }
            if (!z8) {
                if (jM != -1) {
                    return jM;
                }
                if (b8 == null) {
                    return -1L;
                }
                throw b8;
            }
            j9 = 0;
        }
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public final void close() {
        long j8;
        w wVar = this.f16683v;
        synchronized (wVar) {
            this.f16682u = true;
            C1731g c1731g = this.f16681t;
            j8 = c1731g.f18765r;
            c1731g.m();
            wVar.notifyAll();
        }
        if (j8 > 0) {
            byte[] bArr = l7.b.f14412a;
            this.f16683v.f16686b.n(j8);
        }
        this.f16683v.a();
    }

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