package m4;

import androidx.navigation.C1134i;
import com.google.protobuf.C1343z;
import com.google.protobuf.InterfaceC1332t0;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Locale;
import k4.AbstractC2400m;
import k4.C2401n;
import k4.InterfaceC2402o;
import q.AbstractC2761L;
import r4.AbstractC2890c;
import r4.C2888a;

/* JADX INFO: renamed from: m4.f1, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes3.dex */
public final class C2505f1 implements InterfaceC2492b0 {

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public n4.u f21014c;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final C1134i f21019h;

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

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

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

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public int f21013b = -1;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public InterfaceC2402o f21015d = C2401n.f19897b;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public boolean f21016e = true;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final C2502e1 f21017f = new C2502e1(this, 0);

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final ByteBuffer f21018g = ByteBuffer.allocate(5);
    public int l = -1;

    public C2505f1(AbstractC2491b abstractC2491b, C1134i c1134i, g2 g2Var) {
        this.f21012a = abstractC2491b;
        this.f21019h = c1134i;
        this.f21020i = g2Var;
    }

    public static int i(C2888a c2888a, OutputStream outputStream) throws IOException {
        InterfaceC1332t0 interfaceC1332t0 = c2888a.f23998a;
        if (interfaceC1332t0 != null) {
            int serializedSize = interfaceC1332t0.getSerializedSize();
            c2888a.f23998a.writeTo(outputStream);
            c2888a.f23998a = null;
            return serializedSize;
        }
        ByteArrayInputStream byteArrayInputStream = c2888a.f24000c;
        if (byteArrayInputStream == null) {
            return 0;
        }
        C1343z c1343z = AbstractC2890c.f24005a;
        A4.g.o(outputStream, "outputStream cannot be null!");
        byte[] bArr = new byte[8192];
        long j4 = 0;
        while (true) {
            int i6 = byteArrayInputStream.read(bArr);
            if (i6 == -1) {
                int i7 = (int) j4;
                c2888a.f24000c = null;
                return i7;
            }
            outputStream.write(bArr, 0, i6);
            j4 += (long) i6;
        }
    }

    @Override // m4.InterfaceC2492b0
    public final InterfaceC2492b0 a(boolean z6) {
        this.f21016e = z6;
        return this;
    }

    public final void b(boolean z6, boolean z7) {
        n4.u uVar = this.f21014c;
        this.f21014c = null;
        this.f21012a.w(uVar, z6, z7, this.f21022k);
        this.f21022k = 0;
    }

    @Override // m4.InterfaceC2492b0
    public final void c(int i6) {
        A4.g.t(this.f21013b == -1, "max size already set");
        this.f21013b = i6;
    }

    @Override // m4.InterfaceC2492b0
    public final void close() {
        if (this.f21021j) {
            return;
        }
        this.f21021j = true;
        n4.u uVar = this.f21014c;
        if (uVar != null && uVar.f21769c == 0) {
            this.f21014c = null;
        }
        b(true, true);
    }

    @Override // m4.InterfaceC2492b0
    public final InterfaceC2492b0 d(InterfaceC2402o interfaceC2402o) {
        this.f21015d = interfaceC2402o;
        return this;
    }

    @Override // m4.InterfaceC2492b0
    public final void e(C2888a c2888a) {
        if (this.f21021j) {
            throw new IllegalStateException("Framer already closed");
        }
        this.f21022k++;
        int i6 = this.l + 1;
        this.l = i6;
        this.f21023m = 0L;
        g2 g2Var = this.f21020i;
        for (AbstractC2400m abstractC2400m : g2Var.f21042a) {
            abstractC2400m.i(i6);
        }
        boolean z6 = this.f21016e && this.f21015d != C2401n.f19897b;
        try {
            int iAvailable = c2888a.available();
            int iJ = (iAvailable == 0 || !z6) ? j(c2888a, iAvailable) : g(c2888a);
            if (iAvailable != -1 && iJ != iAvailable) {
                throw new k4.t0(k4.r0.f19945m.g(com.google.android.recaptcha.internal.a.i(iJ, iAvailable, "Message length inaccurate ", " != ")));
            }
            long j4 = iJ;
            AbstractC2400m[] abstractC2400mArr = g2Var.f21042a;
            for (AbstractC2400m abstractC2400m2 : abstractC2400mArr) {
                abstractC2400m2.k(j4);
            }
            long j6 = this.f21023m;
            for (AbstractC2400m abstractC2400m3 : abstractC2400mArr) {
                abstractC2400m3.l(j6);
            }
            int i7 = this.l;
            long j7 = this.f21023m;
            AbstractC2400m[] abstractC2400mArr2 = g2Var.f21042a;
            int length = abstractC2400mArr2.length;
            int i8 = 0;
            while (i8 < length) {
                long j8 = j4;
                abstractC2400mArr2[i8].j(i7, j7, j8);
                i8++;
                j4 = j8;
            }
        } catch (IOException e6) {
            throw new k4.t0(k4.r0.f19945m.g("Failed to frame message").f(e6));
        } catch (k4.t0 e7) {
            throw e7;
        } catch (RuntimeException e8) {
            throw new k4.t0(k4.r0.f19945m.g("Failed to frame message").f(e8));
        }
    }

    public final void f(C2499d1 c2499d1, boolean z6) {
        ArrayList arrayList = c2499d1.f20980a;
        Iterator it = arrayList.iterator();
        int i6 = 0;
        while (it.hasNext()) {
            i6 += ((n4.u) it.next()).f21769c;
        }
        int i7 = this.f21013b;
        if (i7 >= 0 && i6 > i7) {
            k4.r0 r0Var = k4.r0.f19944k;
            Locale locale = Locale.US;
            throw new k4.t0(r0Var.g("message too large " + i6 + " > " + i7));
        }
        ByteBuffer byteBuffer = this.f21018g;
        byteBuffer.clear();
        byteBuffer.put(z6 ? (byte) 1 : (byte) 0).putInt(i6);
        this.f21019h.getClass();
        n4.u uVarH = C1134i.h(5);
        uVarH.a(byteBuffer.array(), 0, byteBuffer.position());
        if (i6 == 0) {
            this.f21014c = uVarH;
            return;
        }
        int i8 = this.f21022k - 1;
        AbstractC2491b abstractC2491b = this.f21012a;
        abstractC2491b.w(uVarH, false, false, i8);
        this.f21022k = 1;
        for (int i9 = 0; i9 < arrayList.size() - 1; i9++) {
            abstractC2491b.w((n4.u) arrayList.get(i9), false, false, 0);
        }
        this.f21014c = (n4.u) AbstractC2761L.f(1, arrayList);
        this.f21023m = i6;
    }

    @Override // m4.InterfaceC2492b0
    public final void flush() {
        n4.u uVar = this.f21014c;
        if (uVar == null || uVar.f21769c <= 0) {
            return;
        }
        b(false, true);
    }

    public final int g(C2888a c2888a) throws IOException {
        C2499d1 c2499d1 = new C2499d1(this);
        OutputStream outputStreamC = this.f21015d.c(c2499d1);
        try {
            int i6 = i(c2888a, outputStreamC);
            outputStreamC.close();
            int i7 = this.f21013b;
            if (i7 < 0 || i6 <= i7) {
                f(c2499d1, true);
                return i6;
            }
            k4.r0 r0Var = k4.r0.f19944k;
            Locale locale = Locale.US;
            throw new k4.t0(r0Var.g("message too large " + i6 + " > " + i7));
        } catch (Throwable th) {
            outputStreamC.close();
            throw th;
        }
    }

    public final void h(byte[] bArr, int i6, int i7) {
        while (i7 > 0) {
            n4.u uVar = this.f21014c;
            if (uVar != null && uVar.f21768b == 0) {
                b(false, false);
            }
            if (this.f21014c == null) {
                this.f21019h.getClass();
                this.f21014c = C1134i.h(i7);
            }
            int iMin = Math.min(i7, this.f21014c.f21768b);
            this.f21014c.a(bArr, i6, iMin);
            i6 += iMin;
            i7 -= iMin;
        }
    }

    @Override // m4.InterfaceC2492b0
    public final boolean isClosed() {
        return this.f21021j;
    }

    public final int j(C2888a c2888a, int i6) throws IOException {
        if (i6 == -1) {
            C2499d1 c2499d1 = new C2499d1(this);
            int i7 = i(c2888a, c2499d1);
            f(c2499d1, false);
            return i7;
        }
        this.f21023m = i6;
        int i8 = this.f21013b;
        if (i8 >= 0 && i6 > i8) {
            k4.r0 r0Var = k4.r0.f19944k;
            Locale locale = Locale.US;
            throw new k4.t0(r0Var.g("message too large " + i6 + " > " + i8));
        }
        ByteBuffer byteBuffer = this.f21018g;
        byteBuffer.clear();
        byteBuffer.put((byte) 0).putInt(i6);
        if (this.f21014c == null) {
            int iPosition = byteBuffer.position() + i6;
            this.f21019h.getClass();
            this.f21014c = C1134i.h(iPosition);
        }
        h(byteBuffer.array(), 0, byteBuffer.position());
        return i(c2888a, this.f21017f);
    }
}
