package m4;

import java.io.Closeable;
import java.io.IOException;
import java.util.Locale;
import k4.AbstractC2400m;
import k4.C2401n;
import k4.InterfaceC2402o;
import m1.AbstractC2464a;

/* JADX INFO: renamed from: m4.c1, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes3.dex */
public final class C2496c1 implements Closeable {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public AbstractC2488a f20957a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public int f20958b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final g2 f20959c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final k2 f20960d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public InterfaceC2402o f20961e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public byte[] f20962f;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public int f20963j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public int f20964k;
    public int l;

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

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

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public C2487A f20967o;

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

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

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

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public int f20971s;

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

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

    public C2496c1(AbstractC2488a abstractC2488a, int i6, g2 g2Var, k2 k2Var) {
        C2401n c2401n = C2401n.f19897b;
        this.f20964k = 1;
        this.l = 5;
        this.f20967o = new C2487A();
        this.f20969q = false;
        this.f20970r = -1;
        this.f20972t = false;
        this.f20973u = false;
        this.f20957a = abstractC2488a;
        this.f20961e = c2401n;
        this.f20958b = i6;
        this.f20959c = g2Var;
        A4.g.o(k2Var, "transportTracer");
        this.f20960d = k2Var;
    }

    public final void a() {
        if (this.f20969q) {
            return;
        }
        boolean z6 = true;
        this.f20969q = true;
        while (!this.f20973u && this.f20968p > 0 && d()) {
            try {
                int iC = AbstractC2464a.c(this.f20964k);
                if (iC == 0) {
                    c();
                } else {
                    if (iC != 1) {
                        StringBuilder sb = new StringBuilder();
                        sb.append("Invalid state: ");
                        int i6 = this.f20964k;
                        sb.append(i6 != 1 ? i6 != 2 ? "null" : "BODY" : "HEADER");
                        throw new AssertionError(sb.toString());
                    }
                    b();
                    this.f20968p--;
                }
            } catch (Throwable th) {
                this.f20969q = false;
                throw th;
            }
        }
        if (this.f20973u) {
            close();
            this.f20969q = false;
            return;
        }
        if (this.f20972t) {
            if (this.f20967o.f20553c != 0) {
                z6 = false;
            }
            if (z6) {
                close();
            }
        }
        this.f20969q = false;
    }

    public final void b() {
        Object c2493b1;
        int i6 = this.f20970r;
        long j4 = this.f20971s;
        g2 g2Var = this.f20959c;
        for (AbstractC2400m abstractC2400m : g2Var.f21042a) {
            abstractC2400m.d(i6, j4);
        }
        this.f20971s = 0;
        if (this.f20965m) {
            InterfaceC2402o interfaceC2402o = this.f20961e;
            if (interfaceC2402o == C2401n.f19897b) {
                throw new k4.t0(k4.r0.f19945m.g("Can't decode compressed gRPC message as compression not configured"));
            }
            try {
                C2487A c2487a = this.f20966n;
                C2555w1 c2555w1 = AbstractC2558x1.f21213a;
                C2552v1 c2552v1 = new C2552v1();
                A4.g.o(c2487a, "buffer");
                c2552v1.f21206a = c2487a;
                c2493b1 = new C2493b1(interfaceC2402o.g(c2552v1), this.f20958b, g2Var);
            } catch (IOException e6) {
                throw new RuntimeException(e6);
            }
        } else {
            long j6 = this.f20966n.f20553c;
            for (AbstractC2400m abstractC2400m2 : g2Var.f21042a) {
                abstractC2400m2.f(j6);
            }
            C2487A c2487a2 = this.f20966n;
            C2555w1 c2555w12 = AbstractC2558x1.f21213a;
            C2552v1 c2552v12 = new C2552v1();
            A4.g.o(c2487a2, "buffer");
            c2552v12.f21206a = c2487a2;
            c2493b1 = c2552v12;
        }
        this.f20966n.getClass();
        this.f20966n = null;
        AbstractC2488a abstractC2488a = this.f20957a;
        l2.c cVar = new l2.c(5);
        cVar.f20267b = c2493b1;
        abstractC2488a.f20934j.a(cVar);
        this.f20964k = 1;
        this.l = 5;
    }

    public final void c() {
        int iT = this.f20966n.t();
        if ((iT & 254) != 0) {
            throw new k4.t0(k4.r0.f19945m.g("gRPC frame header malformed: reserved bits not zero"));
        }
        this.f20965m = (iT & 1) != 0;
        C2487A c2487a = this.f20966n;
        c2487a.a(4);
        int iT2 = c2487a.t() | (c2487a.t() << 24) | (c2487a.t() << 16) | (c2487a.t() << 8);
        this.l = iT2;
        if (iT2 < 0 || iT2 > this.f20958b) {
            k4.r0 r0Var = k4.r0.f19944k;
            Locale locale = Locale.US;
            throw new k4.t0(r0Var.g("gRPC message exceeds maximum size " + this.f20958b + ": " + iT2));
        }
        int i6 = this.f20970r + 1;
        this.f20970r = i6;
        for (AbstractC2400m abstractC2400m : this.f20959c.f21042a) {
            abstractC2400m.c(i6);
        }
        k2 k2Var = this.f20960d;
        ((C0) k2Var.f21088c).a();
        ((i2) k2Var.f21087b).e();
        this.f20964k = 2;
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public final void close() {
        if (isClosed()) {
            return;
        }
        C2487A c2487a = this.f20966n;
        boolean z6 = c2487a != null && c2487a.f20553c > 0;
        try {
            C2487A c2487a2 = this.f20967o;
            if (c2487a2 != null) {
                c2487a2.close();
            }
            C2487A c2487a3 = this.f20966n;
            if (c2487a3 != null) {
                c2487a3.close();
            }
            this.f20967o = null;
            this.f20966n = null;
            this.f20957a.c(z6);
        } catch (Throwable th) {
            this.f20967o = null;
            this.f20966n = null;
            throw th;
        }
    }

    /* JADX WARN: Code restructure failed: missing block: B:17:0x002d, code lost:
    
        r0.a(r3);
        r8.f20971s += r3;
     */
    /* JADX WARN: Code restructure failed: missing block: B:26:0x0057, code lost:
    
        if (r8.f20964k == 2) goto L17;
     */
    /* JADX WARN: Type inference fix 'apply assigned field type' failed
    java.lang.UnsupportedOperationException: ArgType.getObject(), call class: class jadx.core.dex.instructions.args.ArgType$UnknownArg
    	at jadx.core.dex.instructions.args.ArgType.getObject(ArgType.java:593)
    	at jadx.core.dex.attributes.nodes.ClassTypeVarsAttr.getTypeVarsMapFor(ClassTypeVarsAttr.java:35)
    	at jadx.core.dex.nodes.utils.TypeUtils.replaceClassGenerics(TypeUtils.java:177)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.insertExplicitUseCast(FixTypesVisitor.java:397)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.tryFieldTypeWithNewCasts(FixTypesVisitor.java:359)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.applyFieldType(FixTypesVisitor.java:309)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.visit(FixTypesVisitor.java:94)
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final boolean d() throws Throwable {
        g2 g2Var = this.f20959c;
        int i6 = 0;
        z = false;
        z = false;
        boolean z6 = false;
        try {
            if (this.f20966n == null) {
                this.f20966n = new C2487A();
            }
            int i7 = 0;
            while (true) {
                try {
                    int i8 = this.l - this.f20966n.f20553c;
                    if (i8 <= 0) {
                        z6 = true;
                        if (i7 > 0) {
                            this.f20957a.a(i7);
                        }
                        return true;
                    }
                    int i9 = this.f20967o.f20553c;
                    if (i9 != 0) {
                        int iMin = Math.min(i8, i9);
                        i7 += iMin;
                        this.f20966n.B(this.f20967o.d(iMin));
                    } else if (i7 > 0) {
                        this.f20957a.a(i7);
                        if (this.f20964k == 2) {
                        }
                    }
                } catch (Throwable th) {
                    int i10 = i7;
                    th = th;
                    i6 = i10;
                    if (i6 > 0) {
                        this.f20957a.a(i6);
                        if (this.f20964k == 2) {
                            g2Var.a(i6);
                            this.f20971s += i6;
                        }
                    }
                    throw th;
                }
            }
            return z6;
        } catch (Throwable th2) {
            th = th2;
        }
    }

    public final boolean isClosed() {
        return this.f20967o == null;
    }
}
