package androidx.datastore.preferences.protobuf;

import com.google.android.gms.measurement.internal.u1;
import java.io.IOException;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Locale;

/* JADX INFO: loaded from: classes.dex */
@CheckReturnValue
public abstract class ByteString implements Iterable<Byte>, Serializable {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final ByteString f1995a = new LiteralByteString(b0.f2048b);

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final h f1996b;
    private static final long serialVersionUID = 1;
    private int hash = 0;

    public static final class BoundedByteString extends LiteralByteString {
        private static final long serialVersionUID = 1;
        private final int bytesLength;
        private final int bytesOffset;

        public BoundedByteString(byte[] bArr, int i10, int i11) {
            super(bArr);
            ByteString.f(i10, i10 + i11, bArr.length);
            this.bytesOffset = i10;
            this.bytesLength = i11;
        }

        private void readObject(ObjectInputStream objectInputStream) throws IOException {
            throw new InvalidObjectException("BoundedByteStream instances are not to be serialized directly");
        }

        @Override // androidx.datastore.preferences.protobuf.ByteString.LiteralByteString, androidx.datastore.preferences.protobuf.ByteString
        public final byte c(int i10) {
            int i11 = this.bytesLength;
            if (((i11 - (i10 + 1)) | i10) >= 0) {
                return this.bytes[this.bytesOffset + i10];
            }
            if (i10 < 0) {
                throw new ArrayIndexOutOfBoundsException(u1.g("Index < 0: ", i10));
            }
            throw new ArrayIndexOutOfBoundsException(defpackage.e.f("Index > length: ", i10, ", ", i11));
        }

        @Override // androidx.datastore.preferences.protobuf.ByteString.LiteralByteString, androidx.datastore.preferences.protobuf.ByteString
        public final void h(int i10, byte[] bArr) {
            System.arraycopy(this.bytes, this.bytesOffset, bArr, 0, i10);
        }

        @Override // androidx.datastore.preferences.protobuf.ByteString.LiteralByteString, androidx.datastore.preferences.protobuf.ByteString
        public final byte i(int i10) {
            return this.bytes[this.bytesOffset + i10];
        }

        @Override // androidx.datastore.preferences.protobuf.ByteString.LiteralByteString
        public final int k() {
            return this.bytesOffset;
        }

        @Override // androidx.datastore.preferences.protobuf.ByteString.LiteralByteString, androidx.datastore.preferences.protobuf.ByteString
        public final int size() {
            return this.bytesLength;
        }

        public Object writeReplace() {
            byte[] bArr;
            int size = size();
            if (size == 0) {
                bArr = b0.f2048b;
            } else {
                byte[] bArr2 = new byte[size];
                h(size, bArr2);
                bArr = bArr2;
            }
            return new LiteralByteString(bArr);
        }
    }

    public static abstract class LeafByteString extends ByteString {
        private static final long serialVersionUID = 1;

        public /* synthetic */ LeafByteString(int i10) {
            this();
        }

        @Override // java.lang.Iterable
        public final Iterator<Byte> iterator() {
            return new f(this);
        }

        private LeafByteString() {
        }
    }

    public static class LiteralByteString extends LeafByteString {
        private static final long serialVersionUID = 1;
        protected final byte[] bytes;

        public LiteralByteString(byte[] bArr) {
            super(0);
            bArr.getClass();
            this.bytes = bArr;
        }

        @Override // androidx.datastore.preferences.protobuf.ByteString
        public byte c(int i10) {
            return this.bytes[i10];
        }

        @Override // androidx.datastore.preferences.protobuf.ByteString
        public final boolean equals(Object obj) {
            if (obj == this) {
                return true;
            }
            if (!(obj instanceof ByteString) || size() != ((ByteString) obj).size()) {
                return false;
            }
            if (size() == 0) {
                return true;
            }
            if (!(obj instanceof LiteralByteString)) {
                return obj.equals(this);
            }
            LiteralByteString literalByteString = (LiteralByteString) obj;
            int iJ = j();
            int iJ2 = literalByteString.j();
            if (iJ != 0 && iJ2 != 0 && iJ != iJ2) {
                return false;
            }
            int size = size();
            if (size > literalByteString.size()) {
                throw new IllegalArgumentException("Length too large: " + size + size());
            }
            if (size > literalByteString.size()) {
                StringBuilder sbQ = defpackage.e.q("Ran off end of other: 0, ", size, ", ");
                sbQ.append(literalByteString.size());
                throw new IllegalArgumentException(sbQ.toString());
            }
            byte[] bArr = this.bytes;
            byte[] bArr2 = literalByteString.bytes;
            int iK = k() + size;
            int iK2 = k();
            int iK3 = literalByteString.k();
            while (iK2 < iK) {
                if (bArr[iK2] != bArr2[iK3]) {
                    return false;
                }
                iK2++;
                iK3++;
            }
            return true;
        }

        @Override // androidx.datastore.preferences.protobuf.ByteString
        public void h(int i10, byte[] bArr) {
            System.arraycopy(this.bytes, 0, bArr, 0, i10);
        }

        @Override // androidx.datastore.preferences.protobuf.ByteString
        public byte i(int i10) {
            return this.bytes[i10];
        }

        public int k() {
            return 0;
        }

        @Override // androidx.datastore.preferences.protobuf.ByteString
        public int size() {
            return this.bytes.length;
        }
    }

    static {
        int i10 = 0;
        f1996b = d.a() ? new h(1, i10) : new h(i10, i10);
    }

    public static int f(int i10, int i11, int i12) {
        int i13 = i11 - i10;
        if ((i10 | i11 | i13 | (i12 - i11)) >= 0) {
            return i13;
        }
        if (i10 < 0) {
            throw new IndexOutOfBoundsException(defpackage.e.e("Beginning index: ", i10, " < 0"));
        }
        if (i11 < i10) {
            throw new IndexOutOfBoundsException(defpackage.e.f("Beginning index larger than ending index: ", i10, ", ", i11));
        }
        throw new IndexOutOfBoundsException(defpackage.e.f("End index: ", i11, " >= ", i12));
    }

    public static ByteString g(int i10, int i11, byte[] bArr) {
        byte[] bArrCopyOfRange;
        int i12 = i10 + i11;
        f(i10, i12, bArr.length);
        switch (f1996b.f2073a) {
            case 0:
                bArrCopyOfRange = Arrays.copyOfRange(bArr, i10, i12);
                break;
            default:
                bArrCopyOfRange = new byte[i11];
                System.arraycopy(bArr, i10, bArrCopyOfRange, 0, i11);
                break;
        }
        return new LiteralByteString(bArrCopyOfRange);
    }

    public abstract byte c(int i10);

    public abstract boolean equals(Object obj);

    public abstract void h(int i10, byte[] bArr);

    public final int hashCode() {
        int i10 = this.hash;
        if (i10 == 0) {
            int size = size();
            LiteralByteString literalByteString = (LiteralByteString) this;
            byte[] bArr = literalByteString.bytes;
            int iK = literalByteString.k();
            int i11 = size;
            for (int i12 = iK; i12 < iK + size; i12++) {
                i11 = (i11 * 31) + bArr[i12];
            }
            i10 = i11 == 0 ? 1 : i11;
            this.hash = i10;
        }
        return i10;
    }

    public abstract byte i(int i10);

    public final int j() {
        return this.hash;
    }

    public abstract int size();

    public final String toString() {
        String string;
        Locale locale = Locale.ROOT;
        Object[] objArr = new Object[3];
        objArr[0] = Integer.toHexString(System.identityHashCode(this));
        objArr[1] = Integer.valueOf(size());
        if (size() <= 50) {
            string = hb.a.Y(this);
        } else {
            StringBuilder sb = new StringBuilder();
            LiteralByteString literalByteString = (LiteralByteString) this;
            int iF = f(0, 47, literalByteString.size());
            sb.append(hb.a.Y(iF == 0 ? f1995a : new BoundedByteString(literalByteString.bytes, literalByteString.k(), iF)));
            sb.append("...");
            string = sb.toString();
        }
        objArr[2] = string;
        return String.format(locale, "<ByteString@%s size=%d contents=\"%s\">", objArr);
    }
}
