package androidx.datastore.preferences.protobuf;

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

/* JADX INFO: renamed from: androidx.datastore.preferences.protobuf.j, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C1021j extends c0 {

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static final Logger f11406h = Logger.getLogger(C1021j.class.getName());

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public static final boolean f11407i = q0.f11438e;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public F f11408c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final byte[] f11409d;

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

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

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final OutputStream f11412g;

    public C1021j(OutputStream outputStream, int i6) {
        if (i6 < 0) {
            throw new IllegalArgumentException("bufferSize must be >= 0");
        }
        int iMax = Math.max(i6, 20);
        this.f11409d = new byte[iMax];
        this.f11410e = iMax;
        if (outputStream == null) {
            throw new NullPointerException("out");
        }
        this.f11412g = outputStream;
    }

    public static int l(int i6, C1018g c1018g) {
        return m(c1018g) + s(i6);
    }

    public static int m(C1018g c1018g) {
        int size = c1018g.size();
        return t(size) + size;
    }

    public static int n(int i6) {
        return s(i6) + 4;
    }

    public static int o(int i6) {
        return s(i6) + 8;
    }

    public static int p(int i6, AbstractC1012a abstractC1012a, X x6) {
        return abstractC1012a.a(x6) + (s(i6) * 2);
    }

    public static int q(int i6) {
        if (i6 >= 0) {
            return t(i6);
        }
        return 10;
    }

    public static int r(String str) {
        int length;
        try {
            length = t0.b(str);
        } catch (s0 unused) {
            length = str.getBytes(AbstractC1032v.f11445a).length;
        }
        return t(length) + length;
    }

    public static int s(int i6) {
        return t(i6 << 3);
    }

    public static int t(int i6) {
        if ((i6 & (-128)) == 0) {
            return 1;
        }
        if ((i6 & (-16384)) == 0) {
            return 2;
        }
        if (((-2097152) & i6) == 0) {
            return 3;
        }
        return (i6 & (-268435456)) == 0 ? 4 : 5;
    }

    public static int u(long j4) {
        int i6;
        if (((-128) & j4) == 0) {
            return 1;
        }
        if (j4 < 0) {
            return 10;
        }
        if (((-34359738368L) & j4) != 0) {
            j4 >>>= 28;
            i6 = 6;
        } else {
            i6 = 2;
        }
        if (((-2097152) & j4) != 0) {
            i6 += 2;
            j4 >>>= 14;
        }
        return (j4 & (-16384)) != 0 ? i6 + 1 : i6;
    }

    public final void A(int i6, C1018g c1018g) {
        L(i6, 2);
        B(c1018g);
    }

    public final void B(C1018g c1018g) throws IOException {
        N(c1018g.size());
        f(c1018g.f11386b, c1018g.k(), c1018g.size());
    }

    public final void C(int i6, int i7) {
        w(14);
        i(i6, 5);
        g(i7);
    }

    public final void D(int i6) throws IOException {
        w(4);
        g(i6);
    }

    public final void E(int i6, long j4) {
        w(18);
        i(i6, 1);
        h(j4);
    }

    public final void F(long j4) throws IOException {
        w(8);
        h(j4);
    }

    public final void G(int i6, int i7) throws IOException {
        w(20);
        i(i6, 0);
        if (i7 >= 0) {
            j(i7);
        } else {
            k(i7);
        }
    }

    public final void H(int i6) throws IOException {
        if (i6 >= 0) {
            N(i6);
        } else {
            P(i6);
        }
    }

    public final void I(int i6, AbstractC1012a abstractC1012a, X x6) throws IOException {
        L(i6, 2);
        N(abstractC1012a.a(x6));
        x6.e(abstractC1012a, this.f11408c);
    }

    public final void J(int i6, String str) throws IOException {
        L(i6, 2);
        K(str);
    }

    public final void K(String str) throws IOException {
        try {
            int length = str.length() * 3;
            int iT = t(length);
            int i6 = iT + length;
            int i7 = this.f11410e;
            if (i6 > i7) {
                byte[] bArr = new byte[length];
                int iB = t0.f11444a.b(str, bArr, 0, length);
                N(iB);
                y(bArr, 0, iB);
                return;
            }
            if (i6 > i7 - this.f11411f) {
                v();
            }
            int iT2 = t(str.length());
            int i8 = this.f11411f;
            byte[] bArr2 = this.f11409d;
            try {
                if (iT2 == iT) {
                    int i9 = i8 + iT2;
                    this.f11411f = i9;
                    int iB2 = t0.f11444a.b(str, bArr2, i9, i7 - i9);
                    this.f11411f = i8;
                    j((iB2 - i8) - iT2);
                    this.f11411f = iB2;
                } else {
                    int iB3 = t0.b(str);
                    j(iB3);
                    this.f11411f = t0.f11444a.b(str, bArr2, this.f11411f, iB3);
                }
            } catch (s0 e6) {
                this.f11411f = i8;
                throw e6;
            } catch (ArrayIndexOutOfBoundsException e7) {
                throw new H4.c(e7);
            }
        } catch (s0 e8) {
            f11406h.log(Level.WARNING, "Converting ill-formed UTF-16. Your Protocol Buffer will not round trip correctly!", (Throwable) e8);
            byte[] bytes = str.getBytes(AbstractC1032v.f11445a);
            try {
                N(bytes.length);
                f(bytes, 0, bytes.length);
            } catch (IndexOutOfBoundsException e9) {
                throw new H4.c(e9);
            }
        }
    }

    public final void L(int i6, int i7) {
        N((i6 << 3) | i7);
    }

    public final void M(int i6, int i7) throws IOException {
        w(20);
        i(i6, 0);
        j(i7);
    }

    public final void N(int i6) throws IOException {
        w(5);
        j(i6);
    }

    public final void O(int i6, long j4) {
        w(20);
        i(i6, 0);
        k(j4);
    }

    public final void P(long j4) throws IOException {
        w(10);
        k(j4);
    }

    @Override // androidx.datastore.preferences.protobuf.c0
    public final void f(byte[] bArr, int i6, int i7) throws IOException {
        y(bArr, i6, i7);
    }

    public final void g(int i6) {
        int i7 = this.f11411f;
        int i8 = i7 + 1;
        this.f11411f = i8;
        byte[] bArr = this.f11409d;
        bArr[i7] = (byte) (i6 & 255);
        int i9 = i7 + 2;
        this.f11411f = i9;
        bArr[i8] = (byte) ((i6 >> 8) & 255);
        int i10 = i7 + 3;
        this.f11411f = i10;
        bArr[i9] = (byte) ((i6 >> 16) & 255);
        this.f11411f = i7 + 4;
        bArr[i10] = (byte) ((i6 >> 24) & 255);
    }

    public final void h(long j4) {
        int i6 = this.f11411f;
        int i7 = i6 + 1;
        this.f11411f = i7;
        byte[] bArr = this.f11409d;
        bArr[i6] = (byte) (j4 & 255);
        int i8 = i6 + 2;
        this.f11411f = i8;
        bArr[i7] = (byte) ((j4 >> 8) & 255);
        int i9 = i6 + 3;
        this.f11411f = i9;
        bArr[i8] = (byte) ((j4 >> 16) & 255);
        int i10 = i6 + 4;
        this.f11411f = i10;
        bArr[i9] = (byte) (255 & (j4 >> 24));
        int i11 = i6 + 5;
        this.f11411f = i11;
        bArr[i10] = (byte) (((int) (j4 >> 32)) & 255);
        int i12 = i6 + 6;
        this.f11411f = i12;
        bArr[i11] = (byte) (((int) (j4 >> 40)) & 255);
        int i13 = i6 + 7;
        this.f11411f = i13;
        bArr[i12] = (byte) (((int) (j4 >> 48)) & 255);
        this.f11411f = i6 + 8;
        bArr[i13] = (byte) (((int) (j4 >> 56)) & 255);
    }

    public final void i(int i6, int i7) {
        j((i6 << 3) | i7);
    }

    public final void j(int i6) {
        boolean z6 = f11407i;
        byte[] bArr = this.f11409d;
        if (z6) {
            while ((i6 & (-128)) != 0) {
                int i7 = this.f11411f;
                this.f11411f = i7 + 1;
                q0.k(bArr, i7, (byte) ((i6 & 127) | 128));
                i6 >>>= 7;
            }
            int i8 = this.f11411f;
            this.f11411f = i8 + 1;
            q0.k(bArr, i8, (byte) i6);
            return;
        }
        while ((i6 & (-128)) != 0) {
            int i9 = this.f11411f;
            this.f11411f = i9 + 1;
            bArr[i9] = (byte) ((i6 & 127) | 128);
            i6 >>>= 7;
        }
        int i10 = this.f11411f;
        this.f11411f = i10 + 1;
        bArr[i10] = (byte) i6;
    }

    public final void k(long j4) {
        boolean z6 = f11407i;
        byte[] bArr = this.f11409d;
        if (z6) {
            while ((j4 & (-128)) != 0) {
                int i6 = this.f11411f;
                this.f11411f = i6 + 1;
                q0.k(bArr, i6, (byte) ((((int) j4) & 127) | 128));
                j4 >>>= 7;
            }
            int i7 = this.f11411f;
            this.f11411f = i7 + 1;
            q0.k(bArr, i7, (byte) j4);
            return;
        }
        while ((j4 & (-128)) != 0) {
            int i8 = this.f11411f;
            this.f11411f = i8 + 1;
            bArr[i8] = (byte) ((((int) j4) & 127) | 128);
            j4 >>>= 7;
        }
        int i9 = this.f11411f;
        this.f11411f = i9 + 1;
        bArr[i9] = (byte) j4;
    }

    public final void v() throws IOException {
        this.f11412g.write(this.f11409d, 0, this.f11411f);
        this.f11411f = 0;
    }

    public final void w(int i6) throws IOException {
        if (this.f11410e - this.f11411f < i6) {
            v();
        }
    }

    public final void x(byte b5) throws IOException {
        if (this.f11411f == this.f11410e) {
            v();
        }
        int i6 = this.f11411f;
        this.f11411f = i6 + 1;
        this.f11409d[i6] = b5;
    }

    public final void y(byte[] bArr, int i6, int i7) throws IOException {
        int i8 = this.f11411f;
        int i9 = this.f11410e;
        int i10 = i9 - i8;
        byte[] bArr2 = this.f11409d;
        if (i10 >= i7) {
            System.arraycopy(bArr, i6, bArr2, i8, i7);
            this.f11411f += i7;
            return;
        }
        System.arraycopy(bArr, i6, bArr2, i8, i10);
        int i11 = i6 + i10;
        int i12 = i7 - i10;
        this.f11411f = i9;
        v();
        if (i12 > i9) {
            this.f11412g.write(bArr, i11, i12);
        } else {
            System.arraycopy(bArr, i11, bArr2, 0, i12);
            this.f11411f = i12;
        }
    }

    public final void z(int i6, boolean z6) throws IOException {
        w(11);
        i(i6, 0);
        byte b5 = z6 ? (byte) 1 : (byte) 0;
        int i7 = this.f11411f;
        this.f11411f = i7 + 1;
        this.f11409d[i7] = b5;
    }
}
