package k4;

import a.AbstractC0872a;
import com.google.firebase.sessions.settings.RemoteSettings;
import com.google.protobuf.AbstractC1329s;
import com.google.protobuf.C1297b0;
import com.google.protobuf.InterfaceC1332t0;
import com.google.protobuf.S0;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.util.concurrent.atomic.AtomicReferenceArray;
import r4.AbstractC2890c;
import r4.C2888a;
import r4.C2889b;

/* JADX INFO: loaded from: classes3.dex */
public final class i0 {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final h0 f19878a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final String f19879b;

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

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final C2889b f19882e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final boolean f19883f;

    public i0(h0 h0Var, String str, C2889b c2889b, C2889b c2889b2, boolean z6) {
        new AtomicReferenceArray(2);
        A4.g.o(h0Var, "type");
        this.f19878a = h0Var;
        A4.g.o(str, "fullMethodName");
        this.f19879b = str;
        int iLastIndexOf = str.lastIndexOf(47);
        this.f19880c = iLastIndexOf == -1 ? null : str.substring(0, iLastIndexOf);
        A4.g.o(c2889b, "requestMarshaller");
        this.f19881d = c2889b;
        A4.g.o(c2889b2, "responseMarshaller");
        this.f19882e = c2889b2;
        this.f19883f = z6;
    }

    public static String a(String str, String str2) {
        StringBuilder sb = new StringBuilder();
        A4.g.o(str, "fullServiceName");
        sb.append(str);
        sb.append(RemoteSettings.FORWARD_SLASH_STRING);
        A4.g.o(str2, "methodName");
        sb.append(str2);
        return sb.toString();
    }

    public static C0.D b() {
        C0.D d6 = new C0.D();
        d6.f519b = null;
        d6.f520c = null;
        return d6;
    }

    public final InterfaceC1332t0 c(InputStream inputStream) {
        AbstractC1329s abstractC1329sG;
        byte[] bArr;
        C2889b c2889b = this.f19882e;
        c2889b.getClass();
        if ((inputStream instanceof C2888a) && ((C2888a) inputStream).f23999b == c2889b.f24002a) {
            try {
                InterfaceC1332t0 interfaceC1332t0 = ((C2888a) inputStream).f23998a;
                if (interfaceC1332t0 != null) {
                    return interfaceC1332t0;
                }
                throw new IllegalStateException("message not available");
            } catch (IllegalStateException unused) {
            }
        }
        try {
            if (inputStream instanceof M) {
                int iAvailable = inputStream.available();
                if (iAvailable <= 0 || iAvailable > 4194304) {
                    if (iAvailable == 0) {
                        return c2889b.f24003b;
                    }
                    abstractC1329sG = null;
                } else {
                    ThreadLocal threadLocal = C2889b.f24001d;
                    Reference reference = (Reference) threadLocal.get();
                    if (reference == null || (bArr = (byte[]) reference.get()) == null || bArr.length < iAvailable) {
                        bArr = new byte[iAvailable];
                        threadLocal.set(new WeakReference(bArr));
                    }
                    int i6 = iAvailable;
                    while (i6 > 0) {
                        int i7 = inputStream.read(bArr, iAvailable - i6, i6);
                        if (i7 == -1) {
                            break;
                        }
                        i6 -= i7;
                    }
                    if (i6 != 0) {
                        throw new RuntimeException("size inaccurate: " + iAvailable + " != " + (iAvailable - i6));
                    }
                    abstractC1329sG = AbstractC1329s.f(bArr, 0, iAvailable, false);
                }
            } else {
                abstractC1329sG = null;
            }
            if (abstractC1329sG == null) {
                abstractC1329sG = AbstractC1329s.g(inputStream);
            }
            int i8 = c2889b.f24004c;
            if (i8 >= 0) {
                if (i8 < 0) {
                    throw new IllegalArgumentException(Z0.o.l("Recursion limit cannot be negative: ", i8));
                }
                abstractC1329sG.f14352b = i8;
            }
            try {
                com.google.protobuf.L partialFrom = com.google.protobuf.L.parsePartialFrom(((com.google.protobuf.H) c2889b.f24002a).f14186a, abstractC1329sG, AbstractC2890c.f24005a);
                if (partialFrom != null && !partialFrom.isInitialized()) {
                    S0 s0NewUninitializedMessageException = partialFrom.newUninitializedMessageException();
                    s0NewUninitializedMessageException.getClass();
                    throw new C1297b0(s0NewUninitializedMessageException.getMessage());
                }
                abstractC1329sG.a(0);
                return partialFrom;
            } catch (C1297b0 e6) {
                throw new t0(r0.f19945m.g("Invalid protobuf byte sequence").f(e6));
            }
        } catch (IOException e7) {
            throw new RuntimeException(e7);
        }
    }

    public final C2888a d(Object obj) {
        C2889b c2889b = this.f19881d;
        c2889b.getClass();
        return new C2888a((InterfaceC1332t0) obj, c2889b.f24002a);
    }

    public final String toString() {
        E3.j jVarI0 = AbstractC0872a.i0(this);
        jVarI0.a(this.f19879b, "fullMethodName");
        jVarI0.a(this.f19878a, "type");
        jVarI0.c("idempotent", false);
        jVarI0.c("safe", false);
        jVarI0.c("sampledToLocalTracing", this.f19883f);
        jVarI0.a(this.f19881d, "requestMarshaller");
        jVarI0.a(this.f19882e, "responseMarshaller");
        jVarI0.a(null, "schemaDescriptor");
        jVarI0.f1151b = true;
        return jVarI0.toString();
    }
}
