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 C1020i extends F1.L {

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

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

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

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

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

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public int f11404k;
    public int l;

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

    public C1020i(InputStream inputStream) {
        Charset charset = AbstractC1032v.f11445a;
        this.f11399c = inputStream;
        this.f11400d = new byte[4096];
        this.f11401e = 0;
        this.f11403j = 0;
        this.l = 0;
    }

    @Override // F1.L
    public final long A() {
        return R();
    }

    @Override // F1.L
    public final int B() {
        return O();
    }

    @Override // F1.L
    public final long C() {
        return P();
    }

    @Override // F1.L
    public final int D() {
        int iQ = Q();
        return (-(iQ & 1)) ^ (iQ >>> 1);
    }

    @Override // F1.L
    public final long E() {
        long jR = R();
        return (-(jR & 1)) ^ (jR >>> 1);
    }

    @Override // F1.L
    public final String F() throws C1034x {
        int iQ = Q();
        byte[] bArr = this.f11400d;
        if (iQ > 0) {
            int i6 = this.f11401e;
            int i7 = this.f11403j;
            if (iQ <= i6 - i7) {
                String str = new String(bArr, i7, iQ, AbstractC1032v.f11445a);
                this.f11403j += iQ;
                return str;
            }
        }
        if (iQ == 0) {
            return "";
        }
        if (iQ > this.f11401e) {
            return new String(L(iQ), AbstractC1032v.f11445a);
        }
        U(iQ);
        String str2 = new String(bArr, this.f11403j, iQ, AbstractC1032v.f11445a);
        this.f11403j += iQ;
        return str2;
    }

    @Override // F1.L
    public final String G() throws IOException {
        int iQ = Q();
        int i6 = this.f11403j;
        int i7 = this.f11401e;
        int i8 = i7 - i6;
        byte[] bArrL = this.f11400d;
        if (iQ <= i8 && iQ > 0) {
            this.f11403j = i6 + iQ;
        } else {
            if (iQ == 0) {
                return "";
            }
            i6 = 0;
            if (iQ <= i7) {
                U(iQ);
                this.f11403j = iQ;
            } else {
                bArrL = L(iQ);
            }
        }
        return t0.f11444a.a(bArrL, i6, iQ);
    }

    @Override // F1.L
    public final int H() throws C1034x {
        if (h()) {
            this.f11404k = 0;
            return 0;
        }
        int iQ = Q();
        this.f11404k = iQ;
        if ((iQ >>> 3) != 0) {
            return iQ;
        }
        throw new C1034x("Protocol message contained an invalid tag (zero).");
    }

    @Override // F1.L
    public final int I() {
        return Q();
    }

    @Override // F1.L
    public final long J() {
        return R();
    }

    @Override // F1.L
    public final boolean K(int i6) throws C1034x {
        int iH;
        int i7 = i6 & 7;
        int i8 = 0;
        if (i7 == 0) {
            int i9 = this.f11401e - this.f11403j;
            byte[] bArr = this.f11400d;
            if (i9 >= 10) {
                while (i8 < 10) {
                    int i10 = this.f11403j;
                    this.f11403j = i10 + 1;
                    if (bArr[i10] < 0) {
                        i8++;
                    }
                }
                throw C1034x.c();
            }
            while (i8 < 10) {
                if (this.f11403j == this.f11401e) {
                    U(1);
                }
                int i11 = this.f11403j;
                this.f11403j = i11 + 1;
                if (bArr[i11] < 0) {
                    i8++;
                }
            }
            throw C1034x.c();
            return true;
        }
        if (i7 == 1) {
            V(8);
            return true;
        }
        if (i7 == 2) {
            V(Q());
            return true;
        }
        if (i7 != 3) {
            if (i7 == 4) {
                return false;
            }
            if (i7 != 5) {
                throw C1034x.b();
            }
            V(4);
            return true;
        }
        do {
            iH = H();
            if (iH == 0) {
                break;
            }
        } while (K(iH));
        c(((i6 >>> 3) << 3) | 4);
        return true;
    }

    public final byte[] L(int i6) throws IOException {
        byte[] bArrM = M(i6);
        if (bArrM != null) {
            return bArrM;
        }
        int i7 = this.f11403j;
        int i8 = this.f11401e;
        int length = i8 - i7;
        this.l += i8;
        this.f11403j = 0;
        this.f11401e = 0;
        ArrayList<byte[]> arrayListN = N(i6 - length);
        byte[] bArr = new byte[i6];
        System.arraycopy(this.f11400d, i7, bArr, 0, length);
        for (byte[] bArr2 : arrayListN) {
            System.arraycopy(bArr2, 0, bArr, length, bArr2.length);
            length += bArr2.length;
        }
        return bArr;
    }

    public final byte[] M(int i6) throws IOException {
        if (i6 == 0) {
            return AbstractC1032v.f11446b;
        }
        if (i6 < 0) {
            throw C1034x.d();
        }
        int i7 = this.l;
        int i8 = this.f11403j;
        int i9 = i7 + i8 + i6;
        if (i9 - Integer.MAX_VALUE > 0) {
            throw new C1034x("Protocol message was too large.  May be malicious.  Use CodedInputStream.setSizeLimit() to increase the size limit.");
        }
        int i10 = this.f11405m;
        if (i9 > i10) {
            V((i10 - i7) - i8);
            throw C1034x.e();
        }
        int i11 = this.f11401e - i8;
        int i12 = i6 - i11;
        InputStream inputStream = this.f11399c;
        if (i12 >= 4096) {
            try {
                if (i12 > inputStream.available()) {
                    return null;
                }
            } catch (C1034x e6) {
                e6.f11447a = true;
                throw e6;
            }
        }
        byte[] bArr = new byte[i6];
        System.arraycopy(this.f11400d, this.f11403j, bArr, 0, i11);
        this.l += this.f11401e;
        this.f11403j = 0;
        this.f11401e = 0;
        while (i11 < i6) {
            try {
                int i13 = inputStream.read(bArr, i11, i6 - i11);
                if (i13 == -1) {
                    throw C1034x.e();
                }
                this.l += i13;
                i11 += i13;
            } catch (C1034x e7) {
                e7.f11447a = true;
                throw e7;
            }
        }
        return bArr;
    }

    public final ArrayList N(int i6) throws IOException {
        ArrayList arrayList = new ArrayList();
        while (i6 > 0) {
            int iMin = Math.min(i6, 4096);
            byte[] bArr = new byte[iMin];
            int i7 = 0;
            while (i7 < iMin) {
                int i8 = this.f11399c.read(bArr, i7, iMin - i7);
                if (i8 == -1) {
                    throw C1034x.e();
                }
                this.l += i8;
                i7 += i8;
            }
            i6 -= iMin;
            arrayList.add(bArr);
        }
        return arrayList;
    }

    public final int O() throws C1034x {
        int i6 = this.f11403j;
        if (this.f11401e - i6 < 4) {
            U(4);
            i6 = this.f11403j;
        }
        this.f11403j = i6 + 4;
        byte[] bArr = this.f11400d;
        return ((bArr[i6 + 3] & 255) << 24) | (bArr[i6] & 255) | ((bArr[i6 + 1] & 255) << 8) | ((bArr[i6 + 2] & 255) << 16);
    }

    public final long P() throws C1034x {
        int i6 = this.f11403j;
        if (this.f11401e - i6 < 8) {
            U(8);
            i6 = this.f11403j;
        }
        this.f11403j = i6 + 8;
        byte[] bArr = this.f11400d;
        return ((((long) bArr[i6 + 7]) & 255) << 56) | (((long) bArr[i6]) & 255) | ((((long) bArr[i6 + 1]) & 255) << 8) | ((((long) bArr[i6 + 2]) & 255) << 16) | ((((long) bArr[i6 + 3]) & 255) << 24) | ((((long) bArr[i6 + 4]) & 255) << 32) | ((((long) bArr[i6 + 5]) & 255) << 40) | ((((long) bArr[i6 + 6]) & 255) << 48);
    }

    public final int Q() {
        int i6;
        int i7 = this.f11403j;
        int i8 = this.f11401e;
        if (i8 != i7) {
            int i9 = i7 + 1;
            byte[] bArr = this.f11400d;
            byte b5 = bArr[i7];
            if (b5 >= 0) {
                this.f11403j = i9;
                return b5;
            }
            if (i8 - i9 >= 9) {
                int i10 = i7 + 2;
                int i11 = (bArr[i9] << 7) ^ b5;
                if (i11 < 0) {
                    i6 = i11 ^ (-128);
                } else {
                    int i12 = i7 + 3;
                    int i13 = (bArr[i10] << 14) ^ i11;
                    if (i13 >= 0) {
                        i6 = i13 ^ 16256;
                    } else {
                        int i14 = i7 + 4;
                        int i15 = i13 ^ (bArr[i12] << 21);
                        if (i15 < 0) {
                            i6 = (-2080896) ^ i15;
                        } else {
                            i12 = i7 + 5;
                            byte b6 = bArr[i14];
                            int i16 = (i15 ^ (b6 << 28)) ^ 266354560;
                            if (b6 < 0) {
                                i14 = i7 + 6;
                                if (bArr[i12] < 0) {
                                    i12 = i7 + 7;
                                    if (bArr[i14] < 0) {
                                        i14 = i7 + 8;
                                        if (bArr[i12] < 0) {
                                            i12 = i7 + 9;
                                            if (bArr[i14] < 0) {
                                                int i17 = i7 + 10;
                                                if (bArr[i12] >= 0) {
                                                    i10 = i17;
                                                    i6 = i16;
                                                }
                                            }
                                        }
                                    }
                                }
                                i6 = i16;
                            }
                            i6 = i16;
                        }
                        i10 = i14;
                    }
                    i10 = i12;
                }
                this.f11403j = i10;
                return i6;
            }
        }
        return (int) S();
    }

    public final long R() {
        long j4;
        long j6;
        long j7;
        long j8;
        int i6 = this.f11403j;
        int i7 = this.f11401e;
        if (i7 != i6) {
            int i8 = i6 + 1;
            byte[] bArr = this.f11400d;
            byte b5 = bArr[i6];
            if (b5 >= 0) {
                this.f11403j = i8;
                return b5;
            }
            if (i7 - i8 >= 9) {
                int i9 = i6 + 2;
                int i10 = (bArr[i8] << 7) ^ b5;
                if (i10 < 0) {
                    j4 = i10 ^ (-128);
                } else {
                    int i11 = i6 + 3;
                    int i12 = (bArr[i9] << 14) ^ i10;
                    if (i12 >= 0) {
                        j4 = i12 ^ 16256;
                        i9 = i11;
                    } else {
                        int i13 = i6 + 4;
                        int i14 = i12 ^ (bArr[i11] << 21);
                        if (i14 < 0) {
                            j8 = (-2080896) ^ i14;
                        } else {
                            long j9 = i14;
                            i9 = i6 + 5;
                            long j10 = j9 ^ (((long) bArr[i13]) << 28);
                            if (j10 >= 0) {
                                j7 = 266354560;
                            } else {
                                i13 = i6 + 6;
                                long j11 = j10 ^ (((long) bArr[i9]) << 35);
                                if (j11 < 0) {
                                    j6 = -34093383808L;
                                } else {
                                    i9 = i6 + 7;
                                    j10 = j11 ^ (((long) bArr[i13]) << 42);
                                    if (j10 >= 0) {
                                        j7 = 4363953127296L;
                                    } else {
                                        i13 = i6 + 8;
                                        j11 = j10 ^ (((long) bArr[i9]) << 49);
                                        if (j11 < 0) {
                                            j6 = -558586000294016L;
                                        } else {
                                            i9 = i6 + 9;
                                            long j12 = (j11 ^ (((long) bArr[i13]) << 56)) ^ 71499008037633920L;
                                            if (j12 < 0) {
                                                int i15 = i6 + 10;
                                                if (bArr[i9] >= 0) {
                                                    i9 = i15;
                                                }
                                            }
                                            j4 = j12;
                                        }
                                    }
                                }
                                j8 = j6 ^ j11;
                            }
                            j4 = j7 ^ j10;
                        }
                        i9 = i13;
                        j4 = j8;
                    }
                }
                this.f11403j = i9;
                return j4;
            }
        }
        return S();
    }

    public final long S() throws C1034x {
        long j4 = 0;
        for (int i6 = 0; i6 < 64; i6 += 7) {
            if (this.f11403j == this.f11401e) {
                U(1);
            }
            int i7 = this.f11403j;
            this.f11403j = i7 + 1;
            byte b5 = this.f11400d[i7];
            j4 |= ((long) (b5 & 127)) << i6;
            if ((b5 & 128) == 0) {
                return j4;
            }
        }
        throw C1034x.c();
    }

    public final void T() {
        int i6 = this.f11401e + this.f11402f;
        this.f11401e = i6;
        int i7 = this.l + i6;
        int i8 = this.f11405m;
        if (i7 <= i8) {
            this.f11402f = 0;
            return;
        }
        int i9 = i7 - i8;
        this.f11402f = i9;
        this.f11401e = i6 - i9;
    }

    public final void U(int i6) throws C1034x {
        if (W(i6)) {
            return;
        }
        if (i6 <= (Integer.MAX_VALUE - this.l) - this.f11403j) {
            throw C1034x.e();
        }
        throw new C1034x("Protocol message was too large.  May be malicious.  Use CodedInputStream.setSizeLimit() to increase the size limit.");
    }

    public final void V(int i6) throws C1034x {
        int i7 = this.f11401e;
        int i8 = this.f11403j;
        if (i6 <= i7 - i8 && i6 >= 0) {
            this.f11403j = i8 + i6;
            return;
        }
        InputStream inputStream = this.f11399c;
        if (i6 < 0) {
            throw C1034x.d();
        }
        int i9 = this.l;
        int i10 = i9 + i8;
        int i11 = i10 + i6;
        int i12 = this.f11405m;
        if (i11 > i12) {
            V((i12 - i9) - i8);
            throw C1034x.e();
        }
        this.l = i10;
        int i13 = i7 - i8;
        this.f11401e = 0;
        this.f11403j = 0;
        while (i13 < i6) {
            long j4 = i6 - i13;
            try {
                try {
                    long jSkip = inputStream.skip(j4);
                    if (jSkip < 0 || jSkip > j4) {
                        throw new IllegalStateException(inputStream.getClass() + "#skip returned invalid result: " + jSkip + "\nThe InputStream implementation is buggy.");
                    }
                    if (jSkip == 0) {
                        break;
                    } else {
                        i13 += (int) jSkip;
                    }
                } catch (C1034x e6) {
                    e6.f11447a = true;
                    throw e6;
                }
            } catch (Throwable th) {
                this.l += i13;
                T();
                throw th;
            }
        }
        this.l += i13;
        T();
        if (i13 >= i6) {
            return;
        }
        int i14 = this.f11401e;
        int i15 = i14 - this.f11403j;
        this.f11403j = i14;
        U(1);
        while (true) {
            int i16 = i6 - i15;
            int i17 = this.f11401e;
            if (i16 <= i17) {
                this.f11403j = i16;
                return;
            } else {
                i15 += i17;
                this.f11403j = i17;
                U(1);
            }
        }
    }

    public final boolean W(int i6) throws IOException {
        int i7 = this.f11403j;
        int i8 = i7 + i6;
        int i9 = this.f11401e;
        if (i8 <= i9) {
            throw new IllegalStateException(Z0.o.j(i6, "refillBuffer() called when ", " bytes were already available in buffer"));
        }
        int i10 = this.l;
        if (i6 <= (Integer.MAX_VALUE - i10) - i7 && i10 + i7 + i6 <= this.f11405m) {
            byte[] bArr = this.f11400d;
            if (i7 > 0) {
                if (i9 > i7) {
                    System.arraycopy(bArr, i7, bArr, 0, i9 - i7);
                }
                this.l += i7;
                this.f11401e -= i7;
                this.f11403j = 0;
            }
            int i11 = this.f11401e;
            int iMin = Math.min(bArr.length - i11, (Integer.MAX_VALUE - this.l) - i11);
            InputStream inputStream = this.f11399c;
            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.f11401e += i12;
                    T();
                    if (this.f11401e >= i6) {
                        return true;
                    }
                    return W(i6);
                }
            } catch (C1034x e6) {
                e6.f11447a = true;
                throw e6;
            }
        }
        return false;
    }

    @Override // F1.L
    public final void c(int i6) throws C1034x {
        if (this.f11404k != i6) {
            throw new C1034x("Protocol message end-group tag did not match expected tag.");
        }
    }

    @Override // F1.L
    public final int g() {
        return this.l + this.f11403j;
    }

    @Override // F1.L
    public final boolean h() {
        return this.f11403j == this.f11401e && !W(1);
    }

    @Override // F1.L
    public final void o(int i6) {
        this.f11405m = i6;
        T();
    }

    @Override // F1.L
    public final int q(int i6) throws C1034x {
        if (i6 < 0) {
            throw C1034x.d();
        }
        int i7 = this.l + this.f11403j + i6;
        int i8 = this.f11405m;
        if (i7 > i8) {
            throw C1034x.e();
        }
        this.f11405m = i7;
        T();
        return i8;
    }

    @Override // F1.L
    public final boolean r() {
        return R() != 0;
    }

    @Override // F1.L
    public final C1018g s() throws IOException {
        int iQ = Q();
        int i6 = this.f11401e;
        int i7 = this.f11403j;
        int i8 = i6 - i7;
        byte[] bArr = this.f11400d;
        if (iQ <= i8 && iQ > 0) {
            C1018g c1018gH = C1018g.h(bArr, i7, iQ);
            this.f11403j += iQ;
            return c1018gH;
        }
        if (iQ == 0) {
            return C1018g.f11383c;
        }
        byte[] bArrM = M(iQ);
        if (bArrM != null) {
            return C1018g.h(bArrM, 0, bArrM.length);
        }
        int i9 = this.f11403j;
        int i10 = this.f11401e;
        int length = i10 - i9;
        this.l += i10;
        this.f11403j = 0;
        this.f11401e = 0;
        ArrayList<byte[]> arrayListN = N(iQ - length);
        byte[] bArr2 = new byte[iQ];
        System.arraycopy(bArr, i9, bArr2, 0, length);
        for (byte[] bArr3 : arrayListN) {
            System.arraycopy(bArr3, 0, bArr2, length, bArr3.length);
            length += bArr3.length;
        }
        C1018g c1018g = C1018g.f11383c;
        return new C1018g(bArr2);
    }

    @Override // F1.L
    public final double u() {
        return Double.longBitsToDouble(P());
    }

    @Override // F1.L
    public final int v() {
        return Q();
    }

    @Override // F1.L
    public final int w() {
        return O();
    }

    @Override // F1.L
    public final long x() {
        return P();
    }

    @Override // F1.L
    public final float y() {
        return Float.intBitsToFloat(O());
    }

    @Override // F1.L
    public final int z() {
        return Q();
    }
}
