package o;

import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* loaded from: classes.dex */
public final class Zs0 extends F3 {
    public static final Logger p = Logger.getLogger(Zs0.class.getName());
    public static final boolean q = Fu0.e;
    public C1696kj0 l;
    public final byte[] m;
    public final int n;

    /* renamed from: o, reason: collision with root package name */
    public int f443o;

    public Zs0(int i, byte[] bArr) {
        int length = bArr.length;
        if (((length - i) | i) < 0) {
            Locale locale = Locale.US;
            throw new IllegalArgumentException(AbstractC0126Ew.i(length, i, "Array range is invalid. Buffer.length=", ", offset=0, length="));
        }
        this.m = bArr;
        this.f443o = 0;
        this.n = i;
    }

    public static int X(long j) {
        return (640 - (Long.numberOfLeadingZeros(j) * 9)) >>> 6;
    }

    public static int Y(String str) {
        int length;
        try {
            length = Ju0.b(str);
        } catch (Hu0 unused) {
            length = str.getBytes(Et0.a).length;
        }
        return o0(length) + length;
    }

    public static int o0(int i) {
        return (352 - (Integer.numberOfLeadingZeros(i) * 9)) >>> 6;
    }

    public final void Z(int i, int i2) throws C1723ky {
        i0((i << 3) | i2);
    }

    public final void a0(int i, int i2) throws C1723ky {
        i0(i << 3);
        h0(i2);
    }

    public final void b0(int i, int i2) throws C1723ky {
        i0(i << 3);
        i0(i2);
    }

    public final void c0(int i, int i2) throws C1723ky {
        i0((i << 3) | 5);
        j0(i2);
    }

    public final void d0(int i, long j) throws C1723ky {
        i0(i << 3);
        k0(j);
    }

    public final void e0(int i, long j) throws C1723ky {
        i0((i << 3) | 1);
        l0(j);
    }

    public final void f0(Ws0 ws0) throws C1723ky {
        i0(ws0.c());
        m0(ws0.c(), ws0.m);
    }

    public final void g0(byte b) throws C1723ky {
        int i = this.f443o;
        try {
            int i2 = i + 1;
            try {
                this.m[i] = b;
                this.f443o = i2;
            } catch (IndexOutOfBoundsException e) {
                e = e;
                i = i2;
                throw new C1723ky(i, this.n, 1, e);
            }
        } catch (IndexOutOfBoundsException e2) {
            e = e2;
        }
    }

    public final void h0(int i) throws C1723ky {
        if (i >= 0) {
            i0(i);
        } else {
            k0(i);
        }
    }

    public final void i0(int i) throws C1723ky {
        int i2;
        int i3 = this.f443o;
        while (true) {
            int i4 = i & (-128);
            byte[] bArr = this.m;
            if (i4 == 0) {
                i2 = i3 + 1;
                bArr[i3] = (byte) i;
                this.f443o = i2;
                return;
            } else {
                i2 = i3 + 1;
                try {
                    bArr[i3] = (byte) (i | 128);
                    i >>>= 7;
                    i3 = i2;
                } catch (IndexOutOfBoundsException e) {
                    throw new C1723ky(i2, this.n, 1, e);
                }
            }
            throw new C1723ky(i2, this.n, 1, e);
        }
    }

    public final void j0(int i) throws C1723ky {
        int i2 = this.f443o;
        try {
            byte[] bArr = this.m;
            bArr[i2] = (byte) i;
            bArr[i2 + 1] = (byte) (i >> 8);
            bArr[i2 + 2] = (byte) (i >> 16);
            bArr[i2 + 3] = (byte) (i >> 24);
            this.f443o = i2 + 4;
        } catch (IndexOutOfBoundsException e) {
            throw new C1723ky(i2, this.n, 4, e);
        }
    }

    public final void k0(long j) throws C1723ky {
        int i;
        int i2 = this.f443o;
        int i3 = this.n;
        byte[] bArr = this.m;
        if (!q || i3 - i2 < 10) {
            long j2 = j;
            while ((j2 & (-128)) != 0) {
                int i4 = i2 + 1;
                try {
                    bArr[i2] = (byte) (((int) j2) | 128);
                    j2 >>>= 7;
                    i2 = i4;
                } catch (IndexOutOfBoundsException e) {
                    e = e;
                    i = i4;
                    throw new C1723ky(i, i3, 1, e);
                }
            }
            i = i2 + 1;
            try {
                bArr[i2] = (byte) j2;
            } catch (IndexOutOfBoundsException e2) {
                e = e2;
                throw new C1723ky(i, i3, 1, e);
            }
        } else {
            long j3 = j;
            while ((j3 & (-128)) != 0) {
                Fu0.c.l(bArr, Fu0.f + i2, (byte) (((int) j3) | 128));
                j3 >>>= 7;
                i2++;
            }
            i = i2 + 1;
            Fu0.c.l(bArr, Fu0.f + i2, (byte) j3);
        }
        this.f443o = i;
    }

    public final void l0(long j) throws C1723ky {
        int i = this.f443o;
        try {
            byte[] bArr = this.m;
            bArr[i] = (byte) j;
            bArr[i + 1] = (byte) (j >> 8);
            bArr[i + 2] = (byte) (j >> 16);
            bArr[i + 3] = (byte) (j >> 24);
            bArr[i + 4] = (byte) (j >> 32);
            bArr[i + 5] = (byte) (j >> 40);
            bArr[i + 6] = (byte) (j >> 48);
            bArr[i + 7] = (byte) (j >> 56);
            this.f443o = i + 8;
        } catch (IndexOutOfBoundsException e) {
            throw new C1723ky(i, this.n, 8, e);
        }
    }

    public final void m0(int i, byte[] bArr) throws C1723ky {
        try {
            System.arraycopy(bArr, 0, this.m, this.f443o, i);
            this.f443o += i;
        } catch (IndexOutOfBoundsException e) {
            throw new C1723ky(this.f443o, this.n, i, e);
        }
    }

    public final void n0(String str) throws C1723ky {
        int i = this.f443o;
        try {
            int iO0 = o0(str.length() * 3);
            int iO02 = o0(str.length());
            int i2 = this.n;
            byte[] bArr = this.m;
            if (iO02 != iO0) {
                i0(Ju0.b(str));
                int i3 = this.f443o;
                this.f443o = Ju0.c(str, bArr, i3, i2 - i3);
            } else {
                int i4 = i + iO02;
                this.f443o = i4;
                int iC = Ju0.c(str, bArr, i4, i2 - i4);
                this.f443o = i;
                i0((iC - i) - iO02);
                this.f443o = iC;
            }
        } catch (IndexOutOfBoundsException e) {
            throw new C1723ky(e);
        } catch (Hu0 e2) {
            this.f443o = i;
            p.logp(Level.WARNING, "com.google.protobuf.CodedOutputStream", "inefficientWriteStringNoTag", "Converting ill-formed UTF-16. Your Protocol Buffer will not round trip correctly!", (Throwable) e2);
            byte[] bytes = str.getBytes(Et0.a);
            try {
                int length = bytes.length;
                i0(length);
                m0(length, bytes);
            } catch (IndexOutOfBoundsException e3) {
                throw new C1723ky(e3);
            }
        }
    }
}
