package androidx.datastore.preferences.protobuf;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;

/* JADX INFO: renamed from: androidx.datastore.preferences.protobuf.i, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C0431i extends J3.e {

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final InputStream f8087c;

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

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

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

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public int f8092h;
    public int i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public int f8093j = Integer.MAX_VALUE;

    public C0431i(InputStream inputStream) {
        Charset charset = AbstractC0444w.f8140a;
        this.f8087c = inputStream;
        this.f8088d = new byte[4096];
        this.f8089e = 0;
        this.f8091g = 0;
        this.i = 0;
    }

    @Override // J3.e
    public final String A() throws C0446y {
        int iM = M();
        byte[] bArr = this.f8088d;
        if (iM > 0) {
            int i = this.f8089e;
            int i5 = this.f8091g;
            if (iM <= i - i5) {
                String str = new String(bArr, i5, iM, AbstractC0444w.f8140a);
                this.f8091g += iM;
                return str;
            }
        }
        if (iM == 0) {
            return "";
        }
        if (iM < 0) {
            throw C0446y.d();
        }
        if (iM > this.f8089e) {
            return new String(H(iM), AbstractC0444w.f8140a);
        }
        Q(iM);
        String str2 = new String(bArr, this.f8091g, iM, AbstractC0444w.f8140a);
        this.f8091g += iM;
        return str2;
    }

    @Override // J3.e
    public final String B() throws IOException {
        int iM = M();
        int i = this.f8091g;
        int i5 = this.f8089e;
        int i8 = i5 - i;
        byte[] bArrH = this.f8088d;
        if (iM <= i8 && iM > 0) {
            this.f8091g = i + iM;
        } else {
            if (iM == 0) {
                return "";
            }
            if (iM < 0) {
                throw C0446y.d();
            }
            i = 0;
            if (iM <= i5) {
                Q(iM);
                this.f8091g = iM;
            } else {
                bArrH = H(iM);
            }
        }
        return k0.f8106a.u(bArrH, i, iM);
    }

    @Override // J3.e
    public final int C() throws C0446y {
        if (g()) {
            this.f8092h = 0;
            return 0;
        }
        int iM = M();
        this.f8092h = iM;
        if ((iM >>> 3) != 0) {
            return iM;
        }
        throw new C0446y("Protocol message contained an invalid tag (zero).");
    }

    @Override // J3.e
    public final int D() {
        return M();
    }

    @Override // J3.e
    public final long E() {
        return N();
    }

    @Override // J3.e
    public final boolean F(int i) throws C0446y {
        int i5 = i & 7;
        int i8 = 0;
        if (i5 != 0) {
            if (i5 == 1) {
                R(8);
                return true;
            }
            if (i5 == 2) {
                R(M());
                return true;
            }
            if (i5 == 3) {
                G();
                b(((i >>> 3) << 3) | 4);
                return true;
            }
            if (i5 == 4) {
                return false;
            }
            if (i5 != 5) {
                throw C0446y.b();
            }
            R(4);
            return true;
        }
        int i9 = this.f8089e - this.f8091g;
        byte[] bArr = this.f8088d;
        if (i9 >= 10) {
            while (i8 < 10) {
                int i10 = this.f8091g;
                this.f8091g = i10 + 1;
                if (bArr[i10] < 0) {
                    i8++;
                }
            }
            throw C0446y.c();
        }
        while (i8 < 10) {
            if (this.f8091g == this.f8089e) {
                Q(1);
            }
            int i11 = this.f8091g;
            this.f8091g = i11 + 1;
            if (bArr[i11] < 0) {
                i8++;
            }
        }
        throw C0446y.c();
        return true;
    }

    public final byte[] H(int i) throws IOException {
        byte[] bArrI = I(i);
        if (bArrI != null) {
            return bArrI;
        }
        int i5 = this.f8091g;
        int i8 = this.f8089e;
        int length = i8 - i5;
        this.i += i8;
        this.f8091g = 0;
        this.f8089e = 0;
        ArrayList arrayListJ = J(i - length);
        byte[] bArr = new byte[i];
        System.arraycopy(this.f8088d, i5, bArr, 0, length);
        int size = arrayListJ.size();
        int i9 = 0;
        while (i9 < size) {
            Object obj = arrayListJ.get(i9);
            i9++;
            byte[] bArr2 = (byte[]) obj;
            System.arraycopy(bArr2, 0, bArr, length, bArr2.length);
            length += bArr2.length;
        }
        return bArr;
    }

    public final byte[] I(int i) throws IOException {
        if (i == 0) {
            return AbstractC0444w.f8141b;
        }
        if (i < 0) {
            throw C0446y.d();
        }
        int i5 = this.i;
        int i8 = this.f8091g;
        int i9 = i5 + i8 + i;
        if (i9 - Integer.MAX_VALUE > 0) {
            throw new C0446y("Protocol message was too large.  May be malicious.  Use CodedInputStream.setSizeLimit() to increase the size limit.");
        }
        int i10 = this.f8093j;
        if (i9 > i10) {
            R((i10 - i5) - i8);
            throw C0446y.e();
        }
        int i11 = this.f8089e - i8;
        int i12 = i - i11;
        InputStream inputStream = this.f8087c;
        if (i12 >= 4096) {
            try {
                if (i12 > inputStream.available()) {
                    return null;
                }
            } catch (C0446y e8) {
                e8.f8142q = true;
                throw e8;
            }
        }
        byte[] bArr = new byte[i];
        System.arraycopy(this.f8088d, this.f8091g, bArr, 0, i11);
        this.i += this.f8089e;
        this.f8091g = 0;
        this.f8089e = 0;
        while (i11 < i) {
            try {
                int i13 = inputStream.read(bArr, i11, i - i11);
                if (i13 == -1) {
                    throw C0446y.e();
                }
                this.i += i13;
                i11 += i13;
            } catch (C0446y e9) {
                e9.f8142q = true;
                throw e9;
            }
        }
        return bArr;
    }

    public final ArrayList J(int i) throws IOException {
        ArrayList arrayList = new ArrayList();
        while (i > 0) {
            int iMin = Math.min(i, 4096);
            byte[] bArr = new byte[iMin];
            int i5 = 0;
            while (i5 < iMin) {
                int i8 = this.f8087c.read(bArr, i5, iMin - i5);
                if (i8 == -1) {
                    throw C0446y.e();
                }
                this.i += i8;
                i5 += i8;
            }
            i -= iMin;
            arrayList.add(bArr);
        }
        return arrayList;
    }

    public final int K() throws C0446y {
        int i = this.f8091g;
        if (this.f8089e - i < 4) {
            Q(4);
            i = this.f8091g;
        }
        this.f8091g = i + 4;
        byte[] bArr = this.f8088d;
        return ((bArr[i + 3] & 255) << 24) | (bArr[i] & 255) | ((bArr[i + 1] & 255) << 8) | ((bArr[i + 2] & 255) << 16);
    }

    public final long L() throws C0446y {
        int i = this.f8091g;
        if (this.f8089e - i < 8) {
            Q(8);
            i = this.f8091g;
        }
        this.f8091g = i + 8;
        byte[] bArr = this.f8088d;
        return ((((long) bArr[i + 7]) & 255) << 56) | (((long) bArr[i]) & 255) | ((((long) bArr[i + 1]) & 255) << 8) | ((((long) bArr[i + 2]) & 255) << 16) | ((((long) bArr[i + 3]) & 255) << 24) | ((((long) bArr[i + 4]) & 255) << 32) | ((((long) bArr[i + 5]) & 255) << 40) | ((((long) bArr[i + 6]) & 255) << 48);
    }

    public final int M() {
        int i;
        int i5 = this.f8091g;
        int i8 = this.f8089e;
        if (i8 != i5) {
            int i9 = i5 + 1;
            byte[] bArr = this.f8088d;
            byte b8 = bArr[i5];
            if (b8 >= 0) {
                this.f8091g = i9;
                return b8;
            }
            if (i8 - i9 >= 9) {
                int i10 = i5 + 2;
                int i11 = (bArr[i9] << 7) ^ b8;
                if (i11 < 0) {
                    i = i11 ^ (-128);
                } else {
                    int i12 = i5 + 3;
                    int i13 = (bArr[i10] << 14) ^ i11;
                    if (i13 >= 0) {
                        i = i13 ^ 16256;
                    } else {
                        int i14 = i5 + 4;
                        int i15 = i13 ^ (bArr[i12] << 21);
                        if (i15 < 0) {
                            i = (-2080896) ^ i15;
                        } else {
                            i12 = i5 + 5;
                            byte b9 = bArr[i14];
                            int i16 = (i15 ^ (b9 << 28)) ^ 266354560;
                            if (b9 < 0) {
                                i14 = i5 + 6;
                                if (bArr[i12] < 0) {
                                    i12 = i5 + 7;
                                    if (bArr[i14] < 0) {
                                        i14 = i5 + 8;
                                        if (bArr[i12] < 0) {
                                            i12 = i5 + 9;
                                            if (bArr[i14] < 0) {
                                                int i17 = i5 + 10;
                                                if (bArr[i12] >= 0) {
                                                    i10 = i17;
                                                    i = i16;
                                                }
                                            }
                                        }
                                    }
                                }
                                i = i16;
                            }
                            i = i16;
                        }
                        i10 = i14;
                    }
                    i10 = i12;
                }
                this.f8091g = i10;
                return i;
            }
        }
        return (int) O();
    }

    public final long N() {
        long j8;
        long j9;
        long j10;
        long j11;
        int i = this.f8091g;
        int i5 = this.f8089e;
        if (i5 != i) {
            int i8 = i + 1;
            byte[] bArr = this.f8088d;
            byte b8 = bArr[i];
            if (b8 >= 0) {
                this.f8091g = i8;
                return b8;
            }
            if (i5 - i8 >= 9) {
                int i9 = i + 2;
                int i10 = (bArr[i8] << 7) ^ b8;
                if (i10 < 0) {
                    j8 = i10 ^ (-128);
                } else {
                    int i11 = i + 3;
                    int i12 = (bArr[i9] << 14) ^ i10;
                    if (i12 >= 0) {
                        j8 = i12 ^ 16256;
                        i9 = i11;
                    } else {
                        int i13 = i + 4;
                        int i14 = i12 ^ (bArr[i11] << 21);
                        if (i14 < 0) {
                            j11 = (-2080896) ^ i14;
                        } else {
                            long j12 = i14;
                            i9 = i + 5;
                            long j13 = j12 ^ (((long) bArr[i13]) << 28);
                            if (j13 >= 0) {
                                j10 = 266354560;
                            } else {
                                i13 = i + 6;
                                long j14 = j13 ^ (((long) bArr[i9]) << 35);
                                if (j14 < 0) {
                                    j9 = -34093383808L;
                                } else {
                                    i9 = i + 7;
                                    j13 = j14 ^ (((long) bArr[i13]) << 42);
                                    if (j13 >= 0) {
                                        j10 = 4363953127296L;
                                    } else {
                                        i13 = i + 8;
                                        j14 = j13 ^ (((long) bArr[i9]) << 49);
                                        if (j14 < 0) {
                                            j9 = -558586000294016L;
                                        } else {
                                            i9 = i + 9;
                                            long j15 = (j14 ^ (((long) bArr[i13]) << 56)) ^ 71499008037633920L;
                                            if (j15 < 0) {
                                                int i15 = i + 10;
                                                if (bArr[i9] >= 0) {
                                                    i9 = i15;
                                                }
                                            }
                                            j8 = j15;
                                        }
                                    }
                                }
                                j11 = j9 ^ j14;
                            }
                            j8 = j10 ^ j13;
                        }
                        i9 = i13;
                        j8 = j11;
                    }
                }
                this.f8091g = i9;
                return j8;
            }
        }
        return O();
    }

    public final long O() throws C0446y {
        long j8 = 0;
        for (int i = 0; i < 64; i += 7) {
            if (this.f8091g == this.f8089e) {
                Q(1);
            }
            int i5 = this.f8091g;
            this.f8091g = i5 + 1;
            byte b8 = this.f8088d[i5];
            j8 |= ((long) (b8 & 127)) << i;
            if ((b8 & 128) == 0) {
                return j8;
            }
        }
        throw C0446y.c();
    }

    public final void P() {
        int i = this.f8089e + this.f8090f;
        this.f8089e = i;
        int i5 = this.i + i;
        int i8 = this.f8093j;
        if (i5 <= i8) {
            this.f8090f = 0;
            return;
        }
        int i9 = i5 - i8;
        this.f8090f = i9;
        this.f8089e = i - i9;
    }

    public final void Q(int i) throws C0446y {
        if (S(i)) {
            return;
        }
        if (i <= (Integer.MAX_VALUE - this.i) - this.f8091g) {
            throw C0446y.e();
        }
        throw new C0446y("Protocol message was too large.  May be malicious.  Use CodedInputStream.setSizeLimit() to increase the size limit.");
    }

    public final void R(int i) throws C0446y {
        int i5 = this.f8089e;
        int i8 = this.f8091g;
        if (i <= i5 - i8 && i >= 0) {
            this.f8091g = i8 + i;
            return;
        }
        InputStream inputStream = this.f8087c;
        if (i < 0) {
            throw C0446y.d();
        }
        int i9 = this.i;
        int i10 = i9 + i8;
        int i11 = i10 + i;
        int i12 = this.f8093j;
        if (i11 > i12) {
            R((i12 - i9) - i8);
            throw C0446y.e();
        }
        this.i = i10;
        int i13 = i5 - i8;
        this.f8089e = 0;
        this.f8091g = 0;
        while (i13 < i) {
            long j8 = i - i13;
            try {
                try {
                    long jSkip = inputStream.skip(j8);
                    if (jSkip < 0 || jSkip > j8) {
                        throw new IllegalStateException(inputStream.getClass() + "#skip returned invalid result: " + jSkip + "\nThe InputStream implementation is buggy.");
                    }
                    if (jSkip == 0) {
                        break;
                    } else {
                        i13 += (int) jSkip;
                    }
                } catch (C0446y e8) {
                    e8.f8142q = true;
                    throw e8;
                }
            } catch (Throwable th) {
                this.i += i13;
                P();
                throw th;
            }
        }
        this.i += i13;
        P();
        if (i13 >= i) {
            return;
        }
        int i14 = this.f8089e;
        int i15 = i14 - this.f8091g;
        this.f8091g = i14;
        Q(1);
        while (true) {
            int i16 = i - i15;
            int i17 = this.f8089e;
            if (i16 <= i17) {
                this.f8091g = i16;
                return;
            } else {
                i15 += i17;
                this.f8091g = i17;
                Q(1);
            }
        }
    }

    public final boolean S(int i) throws IOException {
        int i5 = this.f8091g;
        int i8 = i5 + i;
        int i9 = this.f8089e;
        if (i8 <= i9) {
            throw new IllegalStateException(I1.a.k(i, "refillBuffer() called when ", " bytes were already available in buffer"));
        }
        int i10 = this.i;
        if (i <= (Integer.MAX_VALUE - i10) - i5 && i10 + i5 + i <= this.f8093j) {
            byte[] bArr = this.f8088d;
            if (i5 > 0) {
                if (i9 > i5) {
                    System.arraycopy(bArr, i5, bArr, 0, i9 - i5);
                }
                this.i += i5;
                this.f8089e -= i5;
                this.f8091g = 0;
            }
            int i11 = this.f8089e;
            int iMin = Math.min(bArr.length - i11, (Integer.MAX_VALUE - this.i) - i11);
            InputStream inputStream = this.f8087c;
            try {
                int i12 = inputStream.read(bArr, i11, iMin);
                if (i12 == 0 || i12 < -1 || i12 > bArr.length) {
                    throw new IllegalStateException(inputStream.getClass() + "#read(byte[]) returned invalid result: " + i12 + "\nThe InputStream implementation is buggy.");
                }
                if (i12 > 0) {
                    this.f8089e += i12;
                    P();
                    if (this.f8089e >= i) {
                        return true;
                    }
                    return S(i);
                }
            } catch (C0446y e8) {
                e8.f8142q = true;
                throw e8;
            }
        }
        return false;
    }

    @Override // J3.e
    public final void b(int i) throws C0446y {
        if (this.f8092h != i) {
            throw new C0446y("Protocol message end-group tag did not match expected tag.");
        }
    }

    @Override // J3.e
    public final int f() {
        return this.i + this.f8091g;
    }

    @Override // J3.e
    public final boolean g() {
        return this.f8091g == this.f8089e && !S(1);
    }

    @Override // J3.e
    public final void j(int i) {
        this.f8093j = i;
        P();
    }

    @Override // J3.e
    public final int l(int i) throws C0446y {
        if (i < 0) {
            throw C0446y.d();
        }
        int i5 = this.i + this.f8091g + i;
        if (i5 < 0) {
            throw new C0446y("Failed to parse the message.");
        }
        int i8 = this.f8093j;
        if (i5 > i8) {
            throw C0446y.e();
        }
        this.f8093j = i5;
        P();
        return i8;
    }

    @Override // J3.e
    public final boolean m() {
        return N() != 0;
    }

    @Override // J3.e
    public final C0429g n() throws IOException {
        int iM = M();
        int i = this.f8089e;
        int i5 = this.f8091g;
        int i8 = i - i5;
        byte[] bArr = this.f8088d;
        if (iM <= i8 && iM > 0) {
            C0429g c0429gC = C0429g.c(bArr, i5, iM);
            this.f8091g += iM;
            return c0429gC;
        }
        if (iM == 0) {
            return C0429g.f8069s;
        }
        if (iM < 0) {
            throw C0446y.d();
        }
        byte[] bArrI = I(iM);
        if (bArrI != null) {
            return C0429g.c(bArrI, 0, bArrI.length);
        }
        int i9 = this.f8091g;
        int i10 = this.f8089e;
        int length = i10 - i9;
        this.i += i10;
        this.f8091g = 0;
        this.f8089e = 0;
        ArrayList arrayListJ = J(iM - length);
        byte[] bArr2 = new byte[iM];
        System.arraycopy(bArr, i9, bArr2, 0, length);
        int size = arrayListJ.size();
        int i11 = 0;
        while (i11 < size) {
            Object obj = arrayListJ.get(i11);
            i11++;
            byte[] bArr3 = (byte[]) obj;
            System.arraycopy(bArr3, 0, bArr2, length, bArr3.length);
            length += bArr3.length;
        }
        C0429g c0429g = C0429g.f8069s;
        return new C0429g(bArr2);
    }

    @Override // J3.e
    public final double p() {
        return Double.longBitsToDouble(L());
    }

    @Override // J3.e
    public final int q() {
        return M();
    }

    @Override // J3.e
    public final int r() {
        return K();
    }

    @Override // J3.e
    public final long s() {
        return L();
    }

    @Override // J3.e
    public final float t() {
        return Float.intBitsToFloat(K());
    }

    @Override // J3.e
    public final int u() {
        return M();
    }

    @Override // J3.e
    public final long v() {
        return N();
    }

    @Override // J3.e
    public final int w() {
        return K();
    }

    @Override // J3.e
    public final long x() {
        return L();
    }

    @Override // J3.e
    public final int y() {
        int iM = M();
        return (-(iM & 1)) ^ (iM >>> 1);
    }

    @Override // J3.e
    public final long z() {
        long jN = N();
        return (-(jN & 1)) ^ (jN >>> 1);
    }
}
