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.k, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C0433k extends s7.d {

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

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public static final boolean f8101m = h0.f8084e;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public D f8102g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final byte[] f8103h;
    public final int i;

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

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

    public C0433k(OutputStream outputStream, int i) {
        if (i < 0) {
            throw new IllegalArgumentException("bufferSize must be >= 0");
        }
        int iMax = Math.max(i, 20);
        this.f8103h = new byte[iMax];
        this.i = iMax;
        if (outputStream == null) {
            throw new NullPointerException("out");
        }
        this.f8105k = outputStream;
    }

    public static int P(int i, C0429g c0429g) {
        int iR = R(i);
        int size = c0429g.size();
        return S(size) + size + iR;
    }

    public static int Q(String str) {
        int length;
        try {
            length = k0.a(str);
        } catch (j0 unused) {
            length = str.getBytes(AbstractC0444w.f8140a).length;
        }
        return S(length) + length;
    }

    public static int R(int i) {
        return S(i << 3);
    }

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

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

    @Override // s7.d
    public final void I(byte[] bArr, int i, int i5) throws IOException {
        X(bArr, i, i5);
    }

    public final void K(int i) {
        int i5 = this.f8104j;
        int i8 = i5 + 1;
        this.f8104j = i8;
        byte[] bArr = this.f8103h;
        bArr[i5] = (byte) (i & 255);
        int i9 = i5 + 2;
        this.f8104j = i9;
        bArr[i8] = (byte) ((i >> 8) & 255);
        int i10 = i5 + 3;
        this.f8104j = i10;
        bArr[i9] = (byte) ((i >> 16) & 255);
        this.f8104j = i5 + 4;
        bArr[i10] = (byte) ((i >> 24) & 255);
    }

    public final void L(long j8) {
        int i = this.f8104j;
        int i5 = i + 1;
        this.f8104j = i5;
        byte[] bArr = this.f8103h;
        bArr[i] = (byte) (j8 & 255);
        int i8 = i + 2;
        this.f8104j = i8;
        bArr[i5] = (byte) ((j8 >> 8) & 255);
        int i9 = i + 3;
        this.f8104j = i9;
        bArr[i8] = (byte) ((j8 >> 16) & 255);
        int i10 = i + 4;
        this.f8104j = i10;
        bArr[i9] = (byte) (255 & (j8 >> 24));
        int i11 = i + 5;
        this.f8104j = i11;
        bArr[i10] = (byte) (((int) (j8 >> 32)) & 255);
        int i12 = i + 6;
        this.f8104j = i12;
        bArr[i11] = (byte) (((int) (j8 >> 40)) & 255);
        int i13 = i + 7;
        this.f8104j = i13;
        bArr[i12] = (byte) (((int) (j8 >> 48)) & 255);
        this.f8104j = i + 8;
        bArr[i13] = (byte) (((int) (j8 >> 56)) & 255);
    }

    public final void M(int i, int i5) {
        N((i << 3) | i5);
    }

    public final void N(int i) {
        boolean z8 = f8101m;
        byte[] bArr = this.f8103h;
        if (z8) {
            while ((i & (-128)) != 0) {
                int i5 = this.f8104j;
                this.f8104j = i5 + 1;
                h0.j(bArr, i5, (byte) ((i | 128) & 255));
                i >>>= 7;
            }
            int i8 = this.f8104j;
            this.f8104j = i8 + 1;
            h0.j(bArr, i8, (byte) i);
            return;
        }
        while ((i & (-128)) != 0) {
            int i9 = this.f8104j;
            this.f8104j = i9 + 1;
            bArr[i9] = (byte) ((i | 128) & 255);
            i >>>= 7;
        }
        int i10 = this.f8104j;
        this.f8104j = i10 + 1;
        bArr[i10] = (byte) i;
    }

    public final void O(long j8) {
        boolean z8 = f8101m;
        byte[] bArr = this.f8103h;
        if (z8) {
            while ((j8 & (-128)) != 0) {
                int i = this.f8104j;
                this.f8104j = i + 1;
                h0.j(bArr, i, (byte) ((((int) j8) | 128) & 255));
                j8 >>>= 7;
            }
            int i5 = this.f8104j;
            this.f8104j = i5 + 1;
            h0.j(bArr, i5, (byte) j8);
            return;
        }
        while ((j8 & (-128)) != 0) {
            int i8 = this.f8104j;
            this.f8104j = i8 + 1;
            bArr[i8] = (byte) ((((int) j8) | 128) & 255);
            j8 >>>= 7;
        }
        int i9 = this.f8104j;
        this.f8104j = i9 + 1;
        bArr[i9] = (byte) j8;
    }

    public final void U() throws IOException {
        this.f8105k.write(this.f8103h, 0, this.f8104j);
        this.f8104j = 0;
    }

    public final void V(int i) throws IOException {
        if (this.i - this.f8104j < i) {
            U();
        }
    }

    public final void W(byte b8) throws IOException {
        if (this.f8104j == this.i) {
            U();
        }
        int i = this.f8104j;
        this.f8104j = i + 1;
        this.f8103h[i] = b8;
    }

    public final void X(byte[] bArr, int i, int i5) throws IOException {
        int i8 = this.f8104j;
        int i9 = this.i;
        int i10 = i9 - i8;
        byte[] bArr2 = this.f8103h;
        if (i10 >= i5) {
            System.arraycopy(bArr, i, bArr2, i8, i5);
            this.f8104j += i5;
            return;
        }
        System.arraycopy(bArr, i, bArr2, i8, i10);
        int i11 = i + i10;
        int i12 = i5 - i10;
        this.f8104j = i9;
        U();
        if (i12 > i9) {
            this.f8105k.write(bArr, i11, i12);
        } else {
            System.arraycopy(bArr, i11, bArr2, 0, i12);
            this.f8104j = i12;
        }
    }

    public final void Y(int i, boolean z8) throws IOException {
        V(11);
        M(i, 0);
        byte b8 = z8 ? (byte) 1 : (byte) 0;
        int i5 = this.f8104j;
        this.f8104j = i5 + 1;
        this.f8103h[i5] = b8;
    }

    public final void Z(int i, C0429g c0429g) {
        k0(i, 2);
        a0(c0429g);
    }

    public final void a0(C0429g c0429g) throws IOException {
        m0(c0429g.size());
        I(c0429g.f8072r, c0429g.g(), c0429g.size());
    }

    public final void b0(int i, int i5) {
        V(14);
        M(i, 5);
        K(i5);
    }

    public final void c0(int i) throws IOException {
        V(4);
        K(i);
    }

    public final void d0(long j8, int i) {
        V(18);
        M(i, 1);
        L(j8);
    }

    public final void e0(long j8) throws IOException {
        V(8);
        L(j8);
    }

    public final void f0(int i, int i5) throws IOException {
        V(20);
        M(i, 0);
        if (i5 >= 0) {
            N(i5);
        } else {
            O(i5);
        }
    }

    public final void g0(int i) throws IOException {
        if (i >= 0) {
            m0(i);
        } else {
            o0(i);
        }
    }

    public final void h0(int i, AbstractC0423a abstractC0423a, U u8) throws IOException {
        k0(i, 2);
        m0(abstractC0423a.a(u8));
        u8.g(abstractC0423a, this.f8102g);
    }

    public final void i0(int i, String str) throws IOException {
        k0(i, 2);
        j0(str);
    }

    public final void j0(String str) throws IOException {
        try {
            int length = str.length() * 3;
            int iS = S(length);
            int i = iS + length;
            int i5 = this.i;
            if (i > i5) {
                byte[] bArr = new byte[length];
                int iZ = k0.f8106a.z(str, bArr, 0, length);
                m0(iZ);
                X(bArr, 0, iZ);
                return;
            }
            if (i > i5 - this.f8104j) {
                U();
            }
            int iS2 = S(str.length());
            int i8 = this.f8104j;
            byte[] bArr2 = this.f8103h;
            try {
                try {
                    if (iS2 == iS) {
                        int i9 = i8 + iS2;
                        this.f8104j = i9;
                        int iZ2 = k0.f8106a.z(str, bArr2, i9, i5 - i9);
                        this.f8104j = i8;
                        N((iZ2 - i8) - iS2);
                        this.f8104j = iZ2;
                    } else {
                        int iA = k0.a(str);
                        N(iA);
                        this.f8104j = k0.f8106a.z(str, bArr2, this.f8104j, iA);
                    }
                } catch (j0 e8) {
                    this.f8104j = i8;
                    throw e8;
                }
            } catch (ArrayIndexOutOfBoundsException e9) {
                throw new F0.t(e9);
            }
        } catch (j0 e10) {
            f8100l.log(Level.WARNING, "Converting ill-formed UTF-16. Your Protocol Buffer will not round trip correctly!", (Throwable) e10);
            byte[] bytes = str.getBytes(AbstractC0444w.f8140a);
            try {
                m0(bytes.length);
                I(bytes, 0, bytes.length);
            } catch (IndexOutOfBoundsException e11) {
                throw new F0.t(e11);
            }
        }
    }

    public final void k0(int i, int i5) {
        m0((i << 3) | i5);
    }

    public final void l0(int i, int i5) throws IOException {
        V(20);
        M(i, 0);
        N(i5);
    }

    public final void m0(int i) throws IOException {
        V(5);
        N(i);
    }

    public final void n0(long j8, int i) {
        V(20);
        M(i, 0);
        O(j8);
    }

    public final void o0(long j8) throws IOException {
        V(10);
        O(j8);
    }
}
