package q7;

import N1.C0207h;
import P6.g;
import X6.n;
import com.google.android.gms.internal.measurement.X1;
import d5.AbstractC0844a;
import java.io.IOException;
import java.net.ProtocolException;
import java.util.concurrent.TimeUnit;
import k7.o;
import k7.q;
import k7.w;
import o7.l;
import x7.B;
import x7.C1731g;

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

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

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

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

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

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public c(F4.a aVar, q qVar) {
        super(aVar);
        g.e(qVar, "url");
        this.f16338w = aVar;
        this.f16335t = qVar;
        this.f16336u = -1L;
        this.f16337v = true;
    }

    /* JADX WARN: Code restructure failed: missing block: B:51:0x00f2, code lost:
    
        if (r18.f16337v == false) goto L52;
     */
    @Override // q7.a, x7.H
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final long M(C1731g c1731g, long j8) throws IOException {
        C1731g c1731g2;
        long j9;
        byte bA;
        g.e(c1731g, "sink");
        long j10 = 0;
        if (j8 < 0) {
            throw new IllegalArgumentException(AbstractC0844a.i(j8, "byteCount < 0: ").toString());
        }
        if (this.f16330r) {
            throw new IllegalStateException("closed");
        }
        if (this.f16337v) {
            long j11 = this.f16336u;
            F4.a aVar = this.f16338w;
            if (j11 == 0 || j11 == -1) {
                if (j11 != -1) {
                    ((B) aVar.f2497d).K(Long.MAX_VALUE);
                }
                try {
                    B b8 = (B) aVar.f2497d;
                    b8.O(1L);
                    int i = 0;
                    while (true) {
                        int i5 = i + 1;
                        boolean zC = b8.C(i5);
                        c1731g2 = b8.f18725r;
                        if (!zC) {
                            j9 = j10;
                            break;
                        }
                        j9 = j10;
                        bA = c1731g2.A(i);
                        if ((bA < 48 || bA > 57) && ((bA < 97 || bA > 102) && (bA < 65 || bA > 70))) {
                            break;
                        }
                        i = i5;
                        j10 = j9;
                    }
                    if (i == 0) {
                        X1.f(16);
                        String string = Integer.toString(bA, 16);
                        g.d(string, "toString(...)");
                        throw new NumberFormatException("Expected leading [0-9a-fA-F] character but was 0x".concat(string));
                    }
                    this.f16336u = c1731g2.j0();
                    String string2 = X6.e.j0(((B) aVar.f2497d).K(Long.MAX_VALUE)).toString();
                    if (this.f16336u < j9 || (string2.length() > 0 && !n.M(string2, ";", false))) {
                        throw new ProtocolException("expected chunk size and optional extensions but was \"" + this.f16336u + string2 + '\"');
                    }
                    if (this.f16336u == j9) {
                        this.f16337v = false;
                        aVar.f2500g = ((C0207h) aVar.f2499f).x();
                        w wVar = (w) aVar.f2495b;
                        g.b(wVar);
                        o oVar = (o) aVar.f2500g;
                        g.b(oVar);
                        p7.c.b(wVar.f14193z, this.f16335t, oVar);
                        a();
                    }
                } catch (NumberFormatException e8) {
                    throw new ProtocolException(e8.getMessage());
                }
            }
            long jM = super.M(c1731g, Math.min(j8, this.f16336u));
            if (jM != -1) {
                this.f16336u -= jM;
                return jM;
            }
            ((l) aVar.f2496c).l();
            ProtocolException protocolException = new ProtocolException("unexpected end of stream");
            a();
            throw protocolException;
        }
        return -1L;
    }

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