package defpackage;

import androidx.datastore.preferences.protobuf.AbstractC0007;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/* renamed from: ᵖˆ, reason: contains not printable characters */
/* loaded from: classes3.dex */
public final class C2347 {

    /* renamed from: ˇʼʤˎ, reason: contains not printable characters */
    public final C0040 f9183;

    /* renamed from: ˏᵙ, reason: contains not printable characters */
    public final int f9184;

    /* renamed from: ᵥᵯˎ, reason: contains not printable characters */
    public C3718 f9185;

    /* renamed from: ᵴᵳ, reason: contains not printable characters */
    public final byte[] f9186;

    /* renamed from: ᵻᵢﹳᵰ, reason: contains not printable characters */
    public int f9187;

    /* renamed from: ˏˋٴ, reason: contains not printable characters */
    public static final Logger f9182 = Logger.getLogger(C2347.class.getName());

    /* renamed from: ˎˏʜʾ, reason: contains not printable characters */
    public static final boolean f9181 = AbstractC3453.f12957;

    public C2347(C0040 c0040, int i) {
        if (i < 0) {
            ᵹˉᵞ.ˌᵜᵹᵩ("bufferSize must be >= 0");
            throw null;
        }
        int iMax = Math.max(i, 20);
        this.f9186 = new byte[iMax];
        this.f9184 = iMax;
        this.f9183 = c0040;
    }

    /* renamed from: ˎˏʜʾ, reason: contains not printable characters */
    public static int m6063(String str) {
        int length;
        try {
            length = AbstractC2475.m6319(str);
        } catch (C2515 unused) {
            length = str.getBytes(AbstractC2819.f10860).length;
        }
        return m6065(length) + length;
    }

    /* renamed from: ˏˋٴ, reason: contains not printable characters */
    public static int m6064(int i, C5050 c5050) {
        int iM6067 = m6067(i);
        int size = c5050.size();
        return m6065(size) + size + iM6067;
    }

    /* renamed from: ᵝᵵᵺ, reason: contains not printable characters */
    public static int m6065(int i) {
        return (352 - (Integer.numberOfLeadingZeros(i) * 9)) >>> 6;
    }

    /* renamed from: ᵶᵷᵕ, reason: contains not printable characters */
    public static int m6066(long j) {
        return (640 - (Long.numberOfLeadingZeros(j) * 9)) >>> 6;
    }

    /* renamed from: ᵹᵝᵶ, reason: contains not printable characters */
    public static int m6067(int i) {
        return m6065(i << 3);
    }

    /* renamed from: ˀᵗᵝᵭ, reason: contains not printable characters */
    public final void m6068(int i, int i2) {
        m6074((i << 3) | i2);
    }

    /* renamed from: ˇʼʤˎ, reason: contains not printable characters */
    public final void m6069(long j) {
        boolean z = f9181;
        byte[] bArr = this.f9186;
        if (z) {
            while (true) {
                long j2 = j & (-128);
                int i = this.f9187;
                if (j2 == 0) {
                    this.f9187 = i + 1;
                    AbstractC3453.m7843(bArr, i, (byte) j);
                    return;
                } else {
                    this.f9187 = i + 1;
                    AbstractC3453.m7843(bArr, i, (byte) ((((int) j) | 128) & 255));
                    j >>>= 7;
                }
            }
        } else {
            while (true) {
                long j3 = j & (-128);
                int i2 = this.f9187;
                if (j3 == 0) {
                    this.f9187 = i2 + 1;
                    bArr[i2] = (byte) j;
                    return;
                } else {
                    this.f9187 = i2 + 1;
                    bArr[i2] = (byte) ((((int) j) | 128) & 255);
                    j >>>= 7;
                }
            }
        }
    }

    /* renamed from: ˌʾˀ, reason: contains not printable characters */
    public final void m6070(int i, C5050 c5050) throws IOException {
        m6068(i, 2);
        m6077(c5050);
    }

    /* renamed from: ˌᵜᵹᵩ, reason: contains not printable characters */
    public final void m6071(int i) throws IOException {
        if (this.f9184 - this.f9187 < i) {
            m6091();
        }
    }

    /* renamed from: ˎˑː, reason: contains not printable characters */
    public final void m6072(long j, int i) throws IOException {
        m6071(20);
        m6073(i, 0);
        m6069(j);
    }

    /* renamed from: ˏᵙ, reason: contains not printable characters */
    public final void m6073(int i, int i2) {
        m6092((i << 3) | i2);
    }

    /* renamed from: ˑᵥˁ, reason: contains not printable characters */
    public final void m6074(int i) throws IOException {
        m6071(5);
        m6092(i);
    }

    /* renamed from: ˑᵱ, reason: contains not printable characters */
    public final void m6075(long j) throws IOException {
        m6071(8);
        m6089(j);
    }

    /* renamed from: ᵞʡ, reason: contains not printable characters */
    public final void m6076(int i) throws IOException {
        m6071(4);
        m6082(i);
    }

    /* renamed from: ᵡʠ, reason: contains not printable characters */
    public final void m6077(C5050 c5050) throws IOException {
        m6074(c5050.size());
        m6094(c5050.f18875, c5050.mo8448(), c5050.size());
    }

    /* renamed from: ᵡˎ, reason: contains not printable characters */
    public final void m6078(int i, int i2) throws IOException {
        m6071(20);
        m6073(i, 0);
        m6092(i2);
    }

    /* renamed from: ᵡⁱᵥ, reason: contains not printable characters */
    public final void m6079(String str) throws IOException {
        try {
            int length = str.length() * 3;
            int iM6065 = m6065(length);
            int i = iM6065 + length;
            int i2 = this.f9184;
            if (i > i2) {
                byte[] bArr = new byte[length];
                int i3 = AbstractC2475.f9678.ᵸˑ(str, bArr, 0, length);
                m6074(i3);
                m6085(bArr, 0, i3);
                return;
            }
            if (i > i2 - this.f9187) {
                m6091();
            }
            int iM60652 = m6065(str.length());
            int i4 = this.f9187;
            byte[] bArr2 = this.f9186;
            try {
                if (iM60652 == iM6065) {
                    int i5 = i4 + iM60652;
                    this.f9187 = i5;
                    int i6 = AbstractC2475.f9678.ᵸˑ(str, bArr2, i5, i2 - i5);
                    this.f9187 = i4;
                    m6092((i6 - i4) - iM60652);
                    this.f9187 = i6;
                } else {
                    int iM6319 = AbstractC2475.m6319(str);
                    m6092(iM6319);
                    this.f9187 = AbstractC2475.f9678.ᵸˑ(str, bArr2, this.f9187, iM6319);
                }
            } catch (ArrayIndexOutOfBoundsException e) {
                throw new IOException(e) { // from class: androidx.datastore.preferences.protobuf.CodedOutputStream$OutOfSpaceException
                };
            } catch (C2515 e2) {
                this.f9187 = i4;
                throw e2;
            }
        } catch (C2515 e3) {
            f9182.log(Level.WARNING, "Converting ill-formed UTF-16. Your Protocol Buffer will not round trip correctly!", (Throwable) e3);
            byte[] bytes = str.getBytes(AbstractC2819.f10860);
            try {
                m6074(bytes.length);
                m6094(bytes, 0, bytes.length);
            } catch (IndexOutOfBoundsException e4) {
                throw new IOException(e4) { // from class: androidx.datastore.preferences.protobuf.CodedOutputStream$OutOfSpaceException
                };
            }
        }
    }

    /* renamed from: ᵢˇᵭ, reason: contains not printable characters */
    public final void m6080(int i) throws IOException {
        if (i >= 0) {
            m6074(i);
        } else {
            m6086(i);
        }
    }

    /* renamed from: ᵤᵙᵷ, reason: contains not printable characters */
    public final void m6081(int i, String str) throws IOException {
        m6068(i, 2);
        m6079(str);
    }

    /* renamed from: ᵥᵯˎ, reason: contains not printable characters */
    public final void m6082(int i) {
        int i2 = this.f9187;
        int i3 = i2 + 1;
        this.f9187 = i3;
        byte[] bArr = this.f9186;
        bArr[i2] = (byte) (i & 255);
        int i4 = i2 + 2;
        this.f9187 = i4;
        bArr[i3] = (byte) ((i >> 8) & 255);
        int i5 = i2 + 3;
        this.f9187 = i5;
        bArr[i4] = (byte) ((i >> 16) & 255);
        this.f9187 = i2 + 4;
        bArr[i5] = (byte) ((i >> 24) & 255);
    }

    /* renamed from: ᵧᵾˀˈ, reason: contains not printable characters */
    public final void m6083(int i, int i2) throws IOException {
        m6071(20);
        m6073(i, 0);
        if (i2 >= 0) {
            m6092(i2);
        } else {
            m6069(i2);
        }
    }

    /* renamed from: ᵨᵳʝ, reason: contains not printable characters */
    public final void m6084(long j, int i) throws IOException {
        m6071(18);
        m6073(i, 1);
        m6089(j);
    }

    /* renamed from: ᵪʩ, reason: contains not printable characters */
    public final void m6085(byte[] bArr, int i, int i2) throws IOException {
        int i3 = this.f9187;
        int i4 = this.f9184;
        int i5 = i4 - i3;
        byte[] bArr2 = this.f9186;
        if (i5 >= i2) {
            System.arraycopy(bArr, i, bArr2, i3, i2);
            this.f9187 += i2;
            return;
        }
        System.arraycopy(bArr, i, bArr2, i3, i5);
        int i6 = i + i5;
        int i7 = i2 - i5;
        this.f9187 = i4;
        m6091();
        if (i7 > i4) {
            this.f9183.write(bArr, i6, i7);
        } else {
            System.arraycopy(bArr, i6, bArr2, 0, i7);
            this.f9187 = i7;
        }
    }

    /* renamed from: ᵮᵠـˌ, reason: contains not printable characters */
    public final void m6086(long j) throws IOException {
        m6071(10);
        m6069(j);
    }

    /* renamed from: ᵯٴᵦ, reason: contains not printable characters */
    public final void m6087(byte b) throws IOException {
        if (this.f9187 == this.f9184) {
            m6091();
        }
        int i = this.f9187;
        this.f9187 = i + 1;
        this.f9186[i] = b;
    }

    /* renamed from: ᵳٴᵶ, reason: contains not printable characters */
    public final void m6088(int i, boolean z) throws IOException {
        m6071(11);
        m6073(i, 0);
        byte b = z ? (byte) 1 : (byte) 0;
        int i2 = this.f9187;
        this.f9187 = i2 + 1;
        this.f9186[i2] = b;
    }

    /* renamed from: ᵴᵳ, reason: contains not printable characters */
    public final void m6089(long j) {
        int i = this.f9187;
        int i2 = i + 1;
        this.f9187 = i2;
        byte[] bArr = this.f9186;
        bArr[i] = (byte) (j & 255);
        int i3 = i + 2;
        this.f9187 = i3;
        bArr[i2] = (byte) ((j >> 8) & 255);
        int i4 = i + 3;
        this.f9187 = i4;
        bArr[i3] = (byte) ((j >> 16) & 255);
        int i5 = i + 4;
        this.f9187 = i5;
        bArr[i4] = (byte) (255 & (j >> 24));
        int i6 = i + 5;
        this.f9187 = i6;
        bArr[i5] = (byte) (((int) (j >> 32)) & 255);
        int i7 = i + 6;
        this.f9187 = i7;
        bArr[i6] = (byte) (((int) (j >> 40)) & 255);
        int i8 = i + 7;
        this.f9187 = i8;
        bArr[i7] = (byte) (((int) (j >> 48)) & 255);
        this.f9187 = i + 8;
        bArr[i8] = (byte) (((int) (j >> 56)) & 255);
    }

    /* renamed from: ᵸʓᵠ, reason: contains not printable characters */
    public final void m6090(int i, int i2) throws IOException {
        m6071(14);
        m6073(i, 5);
        m6082(i2);
    }

    /* renamed from: ᵸˑ, reason: contains not printable characters */
    public final void m6091() throws IOException {
        this.f9183.write(this.f9186, 0, this.f9187);
        this.f9187 = 0;
    }

    /* renamed from: ᵻᵢﹳᵰ, reason: contains not printable characters */
    public final void m6092(int i) {
        boolean z = f9181;
        byte[] bArr = this.f9186;
        if (z) {
            while (true) {
                int i2 = i & (-128);
                int i3 = this.f9187;
                if (i2 == 0) {
                    this.f9187 = i3 + 1;
                    AbstractC3453.m7843(bArr, i3, (byte) i);
                    return;
                } else {
                    this.f9187 = i3 + 1;
                    AbstractC3453.m7843(bArr, i3, (byte) ((i | 128) & 255));
                    i >>>= 7;
                }
            }
        } else {
            while (true) {
                int i4 = i & (-128);
                int i5 = this.f9187;
                if (i4 == 0) {
                    this.f9187 = i5 + 1;
                    bArr[i5] = (byte) i;
                    return;
                } else {
                    this.f9187 = i5 + 1;
                    bArr[i5] = (byte) ((i | 128) & 255);
                    i >>>= 7;
                }
            }
        }
    }

    /* renamed from: ᵾᵬ, reason: contains not printable characters */
    public final void m6093(int i, AbstractC0007 abstractC0007, InterfaceC4249 interfaceC4249) throws IOException {
        m6068(i, 2);
        m6074(abstractC0007.mo106(interfaceC4249));
        interfaceC4249.mo146(abstractC0007, this.f9185);
    }

    /* renamed from: ᵿʗᵝʽ, reason: contains not printable characters */
    public final void m6094(byte[] bArr, int i, int i2) throws IOException {
        m6085(bArr, i, i2);
    }
}
