package androidx.datastore.preferences.protobuf;

import androidx.datastore.preferences.protobuf.ByteString;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;

/* JADX INFO: loaded from: classes.dex */
public final class j extends k {

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

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

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

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

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

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

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

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

    public j(FileInputStream fileInputStream) {
        Charset charset = b0.f2047a;
        this.f2090e = fileInputStream;
        this.f2091f = new byte[4096];
        this.f2092g = 0;
        this.f2094i = 0;
        this.f2096k = 0;
    }

    public final byte[] A(int i10) throws IOException {
        if (i10 == 0) {
            return b0.f2048b;
        }
        if (i10 < 0) {
            throw InvalidProtocolBufferException.e();
        }
        int i11 = this.f2096k;
        int i12 = this.f2094i;
        int i13 = i11 + i12 + i10;
        if (i13 - this.f2103c > 0) {
            throw new InvalidProtocolBufferException("Protocol message was too large.  May be malicious.  Use CodedInputStream.setSizeLimit() to increase the size limit.");
        }
        int i14 = this.f2097l;
        if (i13 > i14) {
            J((i14 - i11) - i12);
            throw InvalidProtocolBufferException.h();
        }
        int i15 = this.f2092g - i12;
        int i16 = i10 - i15;
        InputStream inputStream = this.f2090e;
        if (i16 >= 4096) {
            try {
                if (i16 > inputStream.available()) {
                    return null;
                }
            } catch (InvalidProtocolBufferException e10) {
                e10.f();
                throw e10;
            }
        }
        byte[] bArr = new byte[i10];
        System.arraycopy(this.f2091f, this.f2094i, bArr, 0, i15);
        this.f2096k += this.f2092g;
        this.f2094i = 0;
        this.f2092g = 0;
        while (i15 < i10) {
            try {
                int i17 = inputStream.read(bArr, i15, i10 - i15);
                if (i17 == -1) {
                    throw InvalidProtocolBufferException.h();
                }
                this.f2096k += i17;
                i15 += i17;
            } catch (InvalidProtocolBufferException e11) {
                e11.f();
                throw e11;
            }
        }
        return bArr;
    }

    public final ArrayList B(int i10) throws IOException {
        ArrayList arrayList = new ArrayList();
        while (i10 > 0) {
            int iMin = Math.min(i10, 4096);
            byte[] bArr = new byte[iMin];
            int i11 = 0;
            while (i11 < iMin) {
                int i12 = this.f2090e.read(bArr, i11, iMin - i11);
                if (i12 == -1) {
                    throw InvalidProtocolBufferException.h();
                }
                this.f2096k += i12;
                i11 += i12;
            }
            i10 -= iMin;
            arrayList.add(bArr);
        }
        return arrayList;
    }

    public final int C() throws InvalidProtocolBufferException {
        int i10 = this.f2094i;
        if (this.f2092g - i10 < 4) {
            I(4);
            i10 = this.f2094i;
        }
        this.f2094i = i10 + 4;
        byte[] bArr = this.f2091f;
        return ((bArr[i10 + 3] & 255) << 24) | (bArr[i10] & 255) | ((bArr[i10 + 1] & 255) << 8) | ((bArr[i10 + 2] & 255) << 16);
    }

    public final long D() throws InvalidProtocolBufferException {
        int i10 = this.f2094i;
        if (this.f2092g - i10 < 8) {
            I(8);
            i10 = this.f2094i;
        }
        this.f2094i = i10 + 8;
        byte[] bArr = this.f2091f;
        return ((((long) bArr[i10 + 7]) & 255) << 56) | (((long) bArr[i10]) & 255) | ((((long) bArr[i10 + 1]) & 255) << 8) | ((((long) bArr[i10 + 2]) & 255) << 16) | ((((long) bArr[i10 + 3]) & 255) << 24) | ((((long) bArr[i10 + 4]) & 255) << 32) | ((((long) bArr[i10 + 5]) & 255) << 40) | ((((long) bArr[i10 + 6]) & 255) << 48);
    }

    public final int E() {
        int i10;
        int i11 = this.f2094i;
        int i12 = this.f2092g;
        if (i12 != i11) {
            int i13 = i11 + 1;
            byte[] bArr = this.f2091f;
            byte b10 = bArr[i11];
            if (b10 >= 0) {
                this.f2094i = i13;
                return b10;
            }
            if (i12 - i13 >= 9) {
                int i14 = i11 + 2;
                int i15 = (bArr[i13] << 7) ^ b10;
                if (i15 < 0) {
                    i10 = i15 ^ (-128);
                } else {
                    int i16 = i11 + 3;
                    int i17 = (bArr[i14] << 14) ^ i15;
                    if (i17 >= 0) {
                        i10 = i17 ^ 16256;
                    } else {
                        int i18 = i11 + 4;
                        int i19 = i17 ^ (bArr[i16] << 21);
                        if (i19 < 0) {
                            i10 = (-2080896) ^ i19;
                        } else {
                            i16 = i11 + 5;
                            byte b11 = bArr[i18];
                            int i20 = (i19 ^ (b11 << 28)) ^ 266354560;
                            if (b11 < 0) {
                                i18 = i11 + 6;
                                if (bArr[i16] < 0) {
                                    i16 = i11 + 7;
                                    if (bArr[i18] < 0) {
                                        i18 = i11 + 8;
                                        if (bArr[i16] < 0) {
                                            i16 = i11 + 9;
                                            if (bArr[i18] < 0) {
                                                int i21 = i11 + 10;
                                                if (bArr[i16] >= 0) {
                                                    i14 = i21;
                                                    i10 = i20;
                                                }
                                            }
                                        }
                                    }
                                }
                                i10 = i20;
                            }
                            i10 = i20;
                        }
                        i14 = i18;
                    }
                    i14 = i16;
                }
                this.f2094i = i14;
                return i10;
            }
        }
        return (int) G();
    }

    public final long F() {
        long j10;
        long j11;
        long j12;
        long j13;
        int i10 = this.f2094i;
        int i11 = this.f2092g;
        if (i11 != i10) {
            int i12 = i10 + 1;
            byte[] bArr = this.f2091f;
            byte b10 = bArr[i10];
            if (b10 >= 0) {
                this.f2094i = i12;
                return b10;
            }
            if (i11 - i12 >= 9) {
                int i13 = i10 + 2;
                int i14 = (bArr[i12] << 7) ^ b10;
                if (i14 < 0) {
                    j10 = i14 ^ (-128);
                } else {
                    int i15 = i10 + 3;
                    int i16 = (bArr[i13] << 14) ^ i14;
                    if (i16 >= 0) {
                        j10 = i16 ^ 16256;
                        i13 = i15;
                    } else {
                        int i17 = i10 + 4;
                        int i18 = i16 ^ (bArr[i15] << 21);
                        if (i18 < 0) {
                            j13 = (-2080896) ^ i18;
                        } else {
                            long j14 = i18;
                            i13 = i10 + 5;
                            long j15 = j14 ^ (((long) bArr[i17]) << 28);
                            if (j15 >= 0) {
                                j12 = 266354560;
                            } else {
                                i17 = i10 + 6;
                                long j16 = j15 ^ (((long) bArr[i13]) << 35);
                                if (j16 < 0) {
                                    j11 = -34093383808L;
                                } else {
                                    i13 = i10 + 7;
                                    j15 = j16 ^ (((long) bArr[i17]) << 42);
                                    if (j15 >= 0) {
                                        j12 = 4363953127296L;
                                    } else {
                                        i17 = i10 + 8;
                                        j16 = j15 ^ (((long) bArr[i13]) << 49);
                                        if (j16 < 0) {
                                            j11 = -558586000294016L;
                                        } else {
                                            i13 = i10 + 9;
                                            long j17 = (j16 ^ (((long) bArr[i17]) << 56)) ^ 71499008037633920L;
                                            if (j17 < 0) {
                                                int i19 = i10 + 10;
                                                if (bArr[i13] >= 0) {
                                                    i13 = i19;
                                                }
                                            }
                                            j10 = j17;
                                        }
                                    }
                                }
                                j13 = j11 ^ j16;
                            }
                            j10 = j12 ^ j15;
                        }
                        i13 = i17;
                        j10 = j13;
                    }
                }
                this.f2094i = i13;
                return j10;
            }
        }
        return G();
    }

    public final long G() throws InvalidProtocolBufferException {
        long j10 = 0;
        for (int i10 = 0; i10 < 64; i10 += 7) {
            if (this.f2094i == this.f2092g) {
                I(1);
            }
            int i11 = this.f2094i;
            this.f2094i = i11 + 1;
            byte b10 = this.f2091f[i11];
            j10 |= ((long) (b10 & 127)) << i10;
            if ((b10 & 128) == 0) {
                return j10;
            }
        }
        throw InvalidProtocolBufferException.d();
    }

    public final void H() {
        int i10 = this.f2092g + this.f2093h;
        this.f2092g = i10;
        int i11 = this.f2096k + i10;
        int i12 = this.f2097l;
        if (i11 <= i12) {
            this.f2093h = 0;
            return;
        }
        int i13 = i11 - i12;
        this.f2093h = i13;
        this.f2092g = i10 - i13;
    }

    public final void I(int i10) throws InvalidProtocolBufferException {
        if (K(i10)) {
            return;
        }
        if (i10 <= (this.f2103c - this.f2096k) - this.f2094i) {
            throw InvalidProtocolBufferException.h();
        }
        throw new InvalidProtocolBufferException("Protocol message was too large.  May be malicious.  Use CodedInputStream.setSizeLimit() to increase the size limit.");
    }

    public final void J(int i10) throws InvalidProtocolBufferException {
        int i11 = this.f2092g;
        int i12 = this.f2094i;
        if (i10 <= i11 - i12 && i10 >= 0) {
            this.f2094i = i12 + i10;
            return;
        }
        InputStream inputStream = this.f2090e;
        if (i10 < 0) {
            throw InvalidProtocolBufferException.e();
        }
        int i13 = this.f2096k;
        int i14 = i13 + i12;
        int i15 = i14 + i10;
        int i16 = this.f2097l;
        if (i15 > i16) {
            J((i16 - i13) - i12);
            throw InvalidProtocolBufferException.h();
        }
        this.f2096k = i14;
        int i17 = i11 - i12;
        this.f2092g = 0;
        this.f2094i = 0;
        while (i17 < i10) {
            long j10 = i10 - i17;
            try {
                try {
                    long jSkip = inputStream.skip(j10);
                    if (jSkip < 0 || jSkip > j10) {
                        throw new IllegalStateException(inputStream.getClass() + "#skip returned invalid result: " + jSkip + "\nThe InputStream implementation is buggy.");
                    }
                    if (jSkip == 0) {
                        break;
                    } else {
                        i17 += (int) jSkip;
                    }
                } catch (InvalidProtocolBufferException e10) {
                    e10.f();
                    throw e10;
                }
            } catch (Throwable th) {
                this.f2096k += i17;
                H();
                throw th;
            }
        }
        this.f2096k += i17;
        H();
        if (i17 >= i10) {
            return;
        }
        int i18 = this.f2092g;
        int i19 = i18 - this.f2094i;
        this.f2094i = i18;
        I(1);
        while (true) {
            int i20 = i10 - i19;
            int i21 = this.f2092g;
            if (i20 <= i21) {
                this.f2094i = i20;
                return;
            } else {
                i19 += i21;
                this.f2094i = i21;
                I(1);
            }
        }
    }

    public final boolean K(int i10) throws IOException {
        int i11 = this.f2094i;
        int i12 = i11 + i10;
        int i13 = this.f2092g;
        if (i12 <= i13) {
            throw new IllegalStateException(defpackage.e.e("refillBuffer() called when ", i10, " bytes were already available in buffer"));
        }
        int i14 = this.f2096k;
        int i15 = this.f2103c;
        if (i10 > (i15 - i14) - i11 || i14 + i11 + i10 > this.f2097l) {
            return false;
        }
        byte[] bArr = this.f2091f;
        if (i11 > 0) {
            if (i13 > i11) {
                System.arraycopy(bArr, i11, bArr, 0, i13 - i11);
            }
            this.f2096k += i11;
            this.f2092g -= i11;
            this.f2094i = 0;
        }
        int i16 = this.f2092g;
        int iMin = Math.min(bArr.length - i16, (i15 - this.f2096k) - i16);
        InputStream inputStream = this.f2090e;
        try {
            int i17 = inputStream.read(bArr, i16, iMin);
            if (i17 == 0 || i17 < -1 || i17 > bArr.length) {
                throw new IllegalStateException(inputStream.getClass() + "#read(byte[]) returned invalid result: " + i17 + "\nThe InputStream implementation is buggy.");
            }
            if (i17 <= 0) {
                return false;
            }
            this.f2092g += i17;
            H();
            if (this.f2092g >= i10) {
                return true;
            }
            return K(i10);
        } catch (InvalidProtocolBufferException e10) {
            e10.f();
            throw e10;
        }
    }

    @Override // androidx.datastore.preferences.protobuf.k
    public final void a(int i10) throws InvalidProtocolBufferException {
        if (this.f2095j != i10) {
            throw new InvalidProtocolBufferException("Protocol message end-group tag did not match expected tag.");
        }
    }

    @Override // androidx.datastore.preferences.protobuf.k
    public final int b() {
        return this.f2096k + this.f2094i;
    }

    @Override // androidx.datastore.preferences.protobuf.k
    public final boolean c() {
        return this.f2094i == this.f2092g && !K(1);
    }

    @Override // androidx.datastore.preferences.protobuf.k
    public final void d(int i10) {
        this.f2097l = i10;
        H();
    }

    @Override // androidx.datastore.preferences.protobuf.k
    public final int e(int i10) throws InvalidProtocolBufferException {
        if (i10 < 0) {
            throw InvalidProtocolBufferException.e();
        }
        int i11 = this.f2096k + this.f2094i + i10;
        if (i11 < 0) {
            throw new InvalidProtocolBufferException("Failed to parse the message.");
        }
        int i12 = this.f2097l;
        if (i11 > i12) {
            throw InvalidProtocolBufferException.h();
        }
        this.f2097l = i11;
        H();
        return i12;
    }

    @Override // androidx.datastore.preferences.protobuf.k
    public final boolean f() {
        return F() != 0;
    }

    @Override // androidx.datastore.preferences.protobuf.k
    public final ByteString g() throws IOException {
        int iE = E();
        int i10 = this.f2092g;
        int i11 = this.f2094i;
        int i12 = i10 - i11;
        byte[] bArr = this.f2091f;
        if (iE <= i12 && iE > 0) {
            ByteString byteStringG = ByteString.g(i11, iE, bArr);
            this.f2094i += iE;
            return byteStringG;
        }
        if (iE == 0) {
            return ByteString.f1995a;
        }
        if (iE < 0) {
            throw InvalidProtocolBufferException.e();
        }
        byte[] bArrA = A(iE);
        if (bArrA != null) {
            return ByteString.g(0, bArrA.length, bArrA);
        }
        int i13 = this.f2094i;
        int i14 = this.f2092g;
        int length = i14 - i13;
        this.f2096k += i14;
        this.f2094i = 0;
        this.f2092g = 0;
        ArrayList<byte[]> arrayListB = B(iE - length);
        byte[] bArr2 = new byte[iE];
        System.arraycopy(bArr, i13, bArr2, 0, length);
        for (byte[] bArr3 : arrayListB) {
            System.arraycopy(bArr3, 0, bArr2, length, bArr3.length);
            length += bArr3.length;
        }
        ByteString byteString = ByteString.f1995a;
        return new ByteString.LiteralByteString(bArr2);
    }

    @Override // androidx.datastore.preferences.protobuf.k
    public final double h() {
        return Double.longBitsToDouble(D());
    }

    @Override // androidx.datastore.preferences.protobuf.k
    public final int i() {
        return E();
    }

    @Override // androidx.datastore.preferences.protobuf.k
    public final int j() {
        return C();
    }

    @Override // androidx.datastore.preferences.protobuf.k
    public final long k() {
        return D();
    }

    @Override // androidx.datastore.preferences.protobuf.k
    public final float l() {
        return Float.intBitsToFloat(C());
    }

    @Override // androidx.datastore.preferences.protobuf.k
    public final int m() {
        return E();
    }

    @Override // androidx.datastore.preferences.protobuf.k
    public final long n() {
        return F();
    }

    @Override // androidx.datastore.preferences.protobuf.k
    public final int o() {
        return C();
    }

    @Override // androidx.datastore.preferences.protobuf.k
    public final long p() {
        return D();
    }

    @Override // androidx.datastore.preferences.protobuf.k
    public final int q() {
        int iE = E();
        return (-(iE & 1)) ^ (iE >>> 1);
    }

    @Override // androidx.datastore.preferences.protobuf.k
    public final long r() {
        long jF = F();
        return (-(jF & 1)) ^ (jF >>> 1);
    }

    @Override // androidx.datastore.preferences.protobuf.k
    public final String s() throws InvalidProtocolBufferException {
        int iE = E();
        byte[] bArr = this.f2091f;
        if (iE > 0) {
            int i10 = this.f2092g;
            int i11 = this.f2094i;
            if (iE <= i10 - i11) {
                String str = new String(bArr, i11, iE, b0.f2047a);
                this.f2094i += iE;
                return str;
            }
        }
        if (iE == 0) {
            return "";
        }
        if (iE < 0) {
            throw InvalidProtocolBufferException.e();
        }
        if (iE > this.f2092g) {
            return new String(z(iE), b0.f2047a);
        }
        I(iE);
        String str2 = new String(bArr, this.f2094i, iE, b0.f2047a);
        this.f2094i += iE;
        return str2;
    }

    @Override // androidx.datastore.preferences.protobuf.k
    public final String t() throws IOException {
        int iE = E();
        int i10 = this.f2094i;
        int i11 = this.f2092g;
        int i12 = i11 - i10;
        byte[] bArrZ = this.f2091f;
        if (iE <= i12 && iE > 0) {
            this.f2094i = i10 + iE;
        } else {
            if (iE == 0) {
                return "";
            }
            if (iE < 0) {
                throw InvalidProtocolBufferException.e();
            }
            i10 = 0;
            if (iE <= i11) {
                I(iE);
                this.f2094i = iE;
            } else {
                bArrZ = z(iE);
            }
        }
        return p1.f2130a.i(i10, iE, bArrZ);
    }

    @Override // androidx.datastore.preferences.protobuf.k
    public final int u() throws InvalidProtocolBufferException {
        if (c()) {
            this.f2095j = 0;
            return 0;
        }
        int iE = E();
        this.f2095j = iE;
        if ((iE >>> 3) != 0) {
            return iE;
        }
        throw new InvalidProtocolBufferException("Protocol message contained an invalid tag (zero).");
    }

    @Override // androidx.datastore.preferences.protobuf.k
    public final int v() {
        return E();
    }

    @Override // androidx.datastore.preferences.protobuf.k
    public final long w() {
        return F();
    }

    @Override // androidx.datastore.preferences.protobuf.k
    public final boolean x(int i10) throws InvalidProtocolBufferException {
        int i11 = i10 & 7;
        int i12 = 0;
        if (i11 != 0) {
            if (i11 == 1) {
                J(8);
                return true;
            }
            if (i11 == 2) {
                J(E());
                return true;
            }
            if (i11 == 3) {
                y();
                a(((i10 >>> 3) << 3) | 4);
                return true;
            }
            if (i11 == 4) {
                return false;
            }
            if (i11 != 5) {
                throw InvalidProtocolBufferException.c();
            }
            J(4);
            return true;
        }
        int i13 = this.f2092g - this.f2094i;
        byte[] bArr = this.f2091f;
        if (i13 >= 10) {
            while (i12 < 10) {
                int i14 = this.f2094i;
                this.f2094i = i14 + 1;
                if (bArr[i14] < 0) {
                    i12++;
                }
            }
            throw InvalidProtocolBufferException.d();
        }
        while (i12 < 10) {
            if (this.f2094i == this.f2092g) {
                I(1);
            }
            int i15 = this.f2094i;
            this.f2094i = i15 + 1;
            if (bArr[i15] < 0) {
                i12++;
            }
        }
        throw InvalidProtocolBufferException.d();
        return true;
    }

    public final byte[] z(int i10) throws IOException {
        byte[] bArrA = A(i10);
        if (bArrA != null) {
            return bArrA;
        }
        int i11 = this.f2094i;
        int i12 = this.f2092g;
        int length = i12 - i11;
        this.f2096k += i12;
        this.f2094i = 0;
        this.f2092g = 0;
        ArrayList<byte[]> arrayListB = B(i10 - length);
        byte[] bArr = new byte[i10];
        System.arraycopy(this.f2091f, i11, bArr, 0, length);
        for (byte[] bArr2 : arrayListB) {
            System.arraycopy(bArr2, 0, bArr, length, bArr2.length);
            length += bArr2.length;
        }
        return bArr;
    }
}
