package o;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* renamed from: o.wi, reason: case insensitive filesystem */
/* loaded from: classes.dex */
public final class C2832wi extends AbstractC1904ms0 {

    /* renamed from: o, reason: collision with root package name */
    public static final Logger f777o = Logger.getLogger(C2832wi.class.getName());
    public static final boolean p = AbstractC0835bi0.e;
    public OD j;
    public final byte[] k;
    public final int l;
    public int m;
    public final Hh0 n;

    public C2832wi(Hh0 hh0, int i) {
        if (i < 0) {
            throw new IllegalArgumentException("bufferSize must be >= 0");
        }
        int iMax = Math.max(i, 20);
        this.k = new byte[iMax];
        this.l = iMax;
        this.n = hh0;
    }

    public static int L(int i, AbstractC0161Gf abstractC0161Gf) {
        int iN = N(i);
        int size = abstractC0161Gf.size();
        return O(size) + size + iN;
    }

    public static int M(String str) {
        int length;
        try {
            length = AbstractC2074oi0.a(str);
        } catch (C1979ni0 unused) {
            length = str.getBytes(AbstractC2418sI.a).length;
        }
        return O(length) + length;
    }

    public static int N(int i) {
        return O(i << 3);
    }

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

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

    @Override // o.AbstractC1904ms0
    public final void E(byte[] bArr, int i, int i2) throws IOException {
        T(bArr, i, i2);
    }

    public final void G(int i) {
        int i2 = this.m;
        int i3 = i2 + 1;
        this.m = i3;
        byte[] bArr = this.k;
        bArr[i2] = (byte) (i & 255);
        int i4 = i2 + 2;
        this.m = i4;
        bArr[i3] = (byte) ((i >> 8) & 255);
        int i5 = i2 + 3;
        this.m = i5;
        bArr[i4] = (byte) ((i >> 16) & 255);
        this.m = i2 + 4;
        bArr[i5] = (byte) ((i >> 24) & 255);
    }

    public final void H(long j) {
        int i = this.m;
        int i2 = i + 1;
        this.m = i2;
        byte[] bArr = this.k;
        bArr[i] = (byte) (j & 255);
        int i3 = i + 2;
        this.m = i3;
        bArr[i2] = (byte) ((j >> 8) & 255);
        int i4 = i + 3;
        this.m = i4;
        bArr[i3] = (byte) ((j >> 16) & 255);
        int i5 = i + 4;
        this.m = i5;
        bArr[i4] = (byte) (255 & (j >> 24));
        int i6 = i + 5;
        this.m = i6;
        bArr[i5] = (byte) (((int) (j >> 32)) & 255);
        int i7 = i + 6;
        this.m = i7;
        bArr[i6] = (byte) (((int) (j >> 40)) & 255);
        int i8 = i + 7;
        this.m = i8;
        bArr[i7] = (byte) (((int) (j >> 48)) & 255);
        this.m = i + 8;
        bArr[i8] = (byte) (((int) (j >> 56)) & 255);
    }

    public final void I(int i, int i2) {
        J((i << 3) | i2);
    }

    public final void J(int i) {
        boolean z = p;
        byte[] bArr = this.k;
        if (z) {
            while ((i & (-128)) != 0) {
                int i2 = this.m;
                this.m = i2 + 1;
                AbstractC0835bi0.j(bArr, i2, (byte) ((i | 128) & 255));
                i >>>= 7;
            }
            int i3 = this.m;
            this.m = i3 + 1;
            AbstractC0835bi0.j(bArr, i3, (byte) i);
            return;
        }
        while ((i & (-128)) != 0) {
            int i4 = this.m;
            this.m = i4 + 1;
            bArr[i4] = (byte) ((i | 128) & 255);
            i >>>= 7;
        }
        int i5 = this.m;
        this.m = i5 + 1;
        bArr[i5] = (byte) i;
    }

    public final void K(long j) {
        boolean z = p;
        byte[] bArr = this.k;
        if (z) {
            while ((j & (-128)) != 0) {
                int i = this.m;
                this.m = i + 1;
                AbstractC0835bi0.j(bArr, i, (byte) ((((int) j) | 128) & 255));
                j >>>= 7;
            }
            int i2 = this.m;
            this.m = i2 + 1;
            AbstractC0835bi0.j(bArr, i2, (byte) j);
            return;
        }
        while ((j & (-128)) != 0) {
            int i3 = this.m;
            this.m = i3 + 1;
            bArr[i3] = (byte) ((((int) j) | 128) & 255);
            j >>>= 7;
        }
        int i4 = this.m;
        this.m = i4 + 1;
        bArr[i4] = (byte) j;
    }

    public final void Q() {
        this.n.write(this.k, 0, this.m);
        this.m = 0;
    }

    public final void R(int i) {
        if (this.l - this.m < i) {
            Q();
        }
    }

    public final void S(byte b) {
        if (this.m == this.l) {
            Q();
        }
        int i = this.m;
        this.m = i + 1;
        this.k[i] = b;
    }

    public final void T(byte[] bArr, int i, int i2) throws IOException {
        int i3 = this.m;
        int i4 = this.l;
        int i5 = i4 - i3;
        byte[] bArr2 = this.k;
        if (i5 >= i2) {
            System.arraycopy(bArr, i, bArr2, i3, i2);
            this.m += i2;
            return;
        }
        System.arraycopy(bArr, i, bArr2, i3, i5);
        int i6 = i + i5;
        int i7 = i2 - i5;
        this.m = i4;
        Q();
        if (i7 > i4) {
            this.n.write(bArr, i6, i7);
        } else {
            System.arraycopy(bArr, i6, bArr2, 0, i7);
            this.m = i7;
        }
    }

    public final void U(int i, boolean z) {
        R(11);
        I(i, 0);
        byte b = z ? (byte) 1 : (byte) 0;
        int i2 = this.m;
        this.m = i2 + 1;
        this.k[i2] = b;
    }

    public final void V(int i, AbstractC0161Gf abstractC0161Gf) {
        g0(i, 2);
        W(abstractC0161Gf);
    }

    public final void W(AbstractC0161Gf abstractC0161Gf) {
        i0(abstractC0161Gf.size());
        C0109Ef c0109Ef = (C0109Ef) abstractC0161Gf;
        E(c0109Ef.f106o, c0109Ef.f(), c0109Ef.size());
    }

    public final void X(int i, int i2) {
        R(14);
        I(i, 5);
        G(i2);
    }

    public final void Y(int i) {
        R(4);
        G(i);
    }

    public final void Z(int i, long j) {
        R(18);
        I(i, 1);
        H(j);
    }

    public final void a0(long j) {
        R(8);
        H(j);
    }

    public final void b0(int i, int i2) {
        R(20);
        I(i, 0);
        if (i2 >= 0) {
            J(i2);
        } else {
            K(i2);
        }
    }

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

    public final void d0(int i, U u, I40 i40) {
        g0(i, 2);
        i0(u.a(i40));
        i40.e(u, this.j);
    }

    public final void e0(int i, String str) throws IOException {
        g0(i, 2);
        f0(str);
    }

    public final void f0(String str) throws IOException {
        try {
            int length = str.length() * 3;
            int iO = O(length);
            int i = iO + length;
            int i2 = this.l;
            if (i > i2) {
                byte[] bArr = new byte[length];
                int iW = AbstractC2074oi0.a.w(str, bArr, 0, length);
                i0(iW);
                T(bArr, 0, iW);
                return;
            }
            if (i > i2 - this.m) {
                Q();
            }
            int iO2 = O(str.length());
            int i3 = this.m;
            byte[] bArr2 = this.k;
            try {
                if (iO2 == iO) {
                    int i4 = i3 + iO2;
                    this.m = i4;
                    int iW2 = AbstractC2074oi0.a.w(str, bArr2, i4, i2 - i4);
                    this.m = i3;
                    J((iW2 - i3) - iO2);
                    this.m = iW2;
                } else {
                    int iA = AbstractC2074oi0.a(str);
                    J(iA);
                    this.m = AbstractC2074oi0.a.w(str, bArr2, this.m, iA);
                }
            } catch (ArrayIndexOutOfBoundsException e) {
                throw new C2737vi(e);
            } catch (C1979ni0 e2) {
                this.m = i3;
                throw e2;
            }
        } catch (C1979ni0 e3) {
            f777o.log(Level.WARNING, "Converting ill-formed UTF-16. Your Protocol Buffer will not round trip correctly!", (Throwable) e3);
            byte[] bytes = str.getBytes(AbstractC2418sI.a);
            try {
                i0(bytes.length);
                E(bytes, 0, bytes.length);
            } catch (IndexOutOfBoundsException e4) {
                throw new C2737vi(e4);
            }
        }
    }

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

    public final void h0(int i, int i2) {
        R(20);
        I(i, 0);
        J(i2);
    }

    public final void i0(int i) {
        R(5);
        J(i);
    }

    public final void j0(int i, long j) {
        R(20);
        I(i, 0);
        K(j);
    }

    public final void k0(long j) {
        R(10);
        K(j);
    }
}
