package androidx.datastore.preferences.protobuf; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InvalidObjectException; import java.io.ObjectInputStream; import java.io.OutputStream; import java.io.Serializable; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.nio.charset.UnsupportedCharsetException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; /* loaded from: classes.dex */ public abstract class ByteString implements Iterable, Serializable { static final int CONCATENATE_BY_COPY_SIZE = 128; public static final ByteString EMPTY = new LiteralByteString(Internal.EMPTY_BYTE_ARRAY); static final int MAX_READ_FROM_CHUNK_SIZE = 8192; static final int MIN_READ_FROM_CHUNK_SIZE = 256; private static final int UNSIGNED_BYTE_MASK = 255; private static final Comparator UNSIGNED_LEXICOGRAPHICAL_COMPARATOR; private static final ByteArrayCopier byteArrayCopier; private int hash = 0; public interface ByteArrayCopier { byte[] copyFrom(byte[] bArr, int i, int i2); } public interface ByteIterator extends Iterator { byte nextByte(); } /* JADX INFO: Access modifiers changed from: private */ public static int toInt(byte b) { return b & 255; } public static Comparator unsignedLexicographicalComparator() { return UNSIGNED_LEXICOGRAPHICAL_COMPARATOR; } public abstract ByteBuffer asReadOnlyByteBuffer(); public abstract List asReadOnlyByteBufferList(); public abstract byte byteAt(int i); public abstract void copyTo(ByteBuffer byteBuffer); public abstract void copyToInternal(byte[] bArr, int i, int i2, int i3); public abstract boolean equals(Object obj); public abstract int getTreeDepth(); public abstract byte internalByteAt(int i); public abstract boolean isBalanced(); public abstract boolean isValidUtf8(); public abstract CodedInputStream newCodedInput(); public abstract InputStream newInput(); public abstract int partialHash(int i, int i2, int i3); public abstract int partialIsValidUtf8(int i, int i2, int i3); public final int peekCachedHashCode() { return this.hash; } public abstract int size(); public abstract ByteString substring(int i, int i2); public abstract String toStringInternal(Charset charset); public abstract void writeTo(ByteOutput byteOutput) throws IOException; public abstract void writeTo(OutputStream outputStream) throws IOException; public abstract void writeToInternal(OutputStream outputStream, int i, int i2) throws IOException; public abstract void writeToReverse(ByteOutput byteOutput) throws IOException; static { byteArrayCopier = Android.isOnAndroidDevice() ? new SystemByteArrayCopier() : new ArraysByteArrayCopier(); UNSIGNED_LEXICOGRAPHICAL_COMPARATOR = new Comparator() { // from class: androidx.datastore.preferences.protobuf.ByteString.2 /* JADX WARN: Type inference failed for: r0v0, types: [androidx.datastore.preferences.protobuf.ByteString$ByteIterator, java.util.Iterator] */ /* JADX WARN: Type inference failed for: r1v0, types: [androidx.datastore.preferences.protobuf.ByteString$ByteIterator, java.util.Iterator] */ @Override // java.util.Comparator public int compare(ByteString byteString, ByteString byteString2) { ?? iterator2 = byteString.iterator2(); ?? iterator22 = byteString2.iterator2(); while (iterator2.hasNext() && iterator22.hasNext()) { int compare = Integer.compare(ByteString.toInt(iterator2.nextByte()), ByteString.toInt(iterator22.nextByte())); if (compare != 0) { return compare; } } return Integer.compare(byteString.size(), byteString2.size()); } }; } public static final class SystemByteArrayCopier implements ByteArrayCopier { private SystemByteArrayCopier() { } @Override // androidx.datastore.preferences.protobuf.ByteString.ByteArrayCopier public byte[] copyFrom(byte[] bArr, int i, int i2) { byte[] bArr2 = new byte[i2]; System.arraycopy(bArr, i, bArr2, 0, i2); return bArr2; } } public static final class ArraysByteArrayCopier implements ByteArrayCopier { private ArraysByteArrayCopier() { } @Override // androidx.datastore.preferences.protobuf.ByteString.ByteArrayCopier public byte[] copyFrom(byte[] bArr, int i, int i2) { return Arrays.copyOfRange(bArr, i, i2 + i); } } @Override // java.lang.Iterable /* renamed from: iterator, reason: merged with bridge method [inline-methods] */ public Iterator iterator2() { return new AbstractByteIterator() { // from class: androidx.datastore.preferences.protobuf.ByteString.1 private final int limit; private int position = 0; @Override // java.util.Iterator public boolean hasNext() { return this.position < this.limit; } { this.limit = ByteString.this.size(); } @Override // androidx.datastore.preferences.protobuf.ByteString.ByteIterator public byte nextByte() { int i = this.position; if (i >= this.limit) { throw new NoSuchElementException(); } this.position = i + 1; return ByteString.this.internalByteAt(i); } }; } public static abstract class AbstractByteIterator implements ByteIterator { /* JADX WARN: Can't rename method to resolve collision */ @Override // java.util.Iterator public final Byte next() { return Byte.valueOf(nextByte()); } @Override // java.util.Iterator public final void remove() { throw new UnsupportedOperationException(); } } public final boolean isEmpty() { return size() == 0; } public final ByteString substring(int i) { return substring(i, size()); } public final boolean startsWith(ByteString byteString) { return size() >= byteString.size() && substring(0, byteString.size()).equals(byteString); } public final boolean endsWith(ByteString byteString) { return size() >= byteString.size() && substring(size() - byteString.size()).equals(byteString); } public static ByteString copyFrom(byte[] bArr, int i, int i2) { checkRange(i, i + i2, bArr.length); return new LiteralByteString(byteArrayCopier.copyFrom(bArr, i, i2)); } public static ByteString copyFrom(byte[] bArr) { return copyFrom(bArr, 0, bArr.length); } public static ByteString wrap(ByteBuffer byteBuffer) { if (byteBuffer.hasArray()) { return wrap(byteBuffer.array(), byteBuffer.arrayOffset() + byteBuffer.position(), byteBuffer.remaining()); } return new NioByteString(byteBuffer); } public static ByteString wrap(byte[] bArr) { return new LiteralByteString(bArr); } public static ByteString wrap(byte[] bArr, int i, int i2) { return new BoundedByteString(bArr, i, i2); } public static ByteString copyFrom(ByteBuffer byteBuffer, int i) { checkRange(0, i, byteBuffer.remaining()); byte[] bArr = new byte[i]; byteBuffer.get(bArr); return new LiteralByteString(bArr); } public static ByteString copyFrom(ByteBuffer byteBuffer) { return copyFrom(byteBuffer, byteBuffer.remaining()); } public static ByteString copyFrom(String str, String str2) throws UnsupportedEncodingException { return new LiteralByteString(str.getBytes(str2)); } public static ByteString copyFrom(String str, Charset charset) { return new LiteralByteString(str.getBytes(charset)); } public static ByteString copyFromUtf8(String str) { return new LiteralByteString(str.getBytes(Internal.UTF_8)); } public static ByteString readFrom(InputStream inputStream) throws IOException { return readFrom(inputStream, 256, 8192); } public static ByteString readFrom(InputStream inputStream, int i) throws IOException { return readFrom(inputStream, i, i); } public static ByteString readFrom(InputStream inputStream, int i, int i2) throws IOException { ArrayList arrayList = new ArrayList(); while (true) { ByteString readChunk = readChunk(inputStream, i); if (readChunk != null) { arrayList.add(readChunk); i = Math.min(i * 2, i2); } else { return copyFrom(arrayList); } } } private static ByteString readChunk(InputStream inputStream, int i) throws IOException { byte[] bArr = new byte[i]; int i2 = 0; while (i2 < i) { int read = inputStream.read(bArr, i2, i - i2); if (read == -1) { break; } i2 += read; } if (i2 == 0) { return null; } return copyFrom(bArr, 0, i2); } public final ByteString concat(ByteString byteString) { if (Integer.MAX_VALUE - size() < byteString.size()) { throw new IllegalArgumentException("ByteString would be too long: " + size() + "+" + byteString.size()); } return RopeByteString.concatenate(this, byteString); } public static ByteString copyFrom(Iterable iterable) { int size; if (!(iterable instanceof Collection)) { Iterator it = iterable.iterator(); size = 0; while (it.hasNext()) { it.next(); size++; } } else { size = ((Collection) iterable).size(); } return size == 0 ? EMPTY : balancedConcat(iterable.iterator(), size); } private static ByteString balancedConcat(Iterator it, int i) { if (i < 1) { throw new IllegalArgumentException(String.format("length (%s) must be >= 1", Integer.valueOf(i))); } if (i == 1) { return it.next(); } int i2 = i >>> 1; return balancedConcat(it, i2).concat(balancedConcat(it, i - i2)); } public void copyTo(byte[] bArr, int i) { copyTo(bArr, 0, i, size()); } @Deprecated public final void copyTo(byte[] bArr, int i, int i2, int i3) { checkRange(i, i + i3, size()); checkRange(i2, i2 + i3, bArr.length); if (i3 > 0) { copyToInternal(bArr, i, i2, i3); } } public final byte[] toByteArray() { int size = size(); if (size == 0) { return Internal.EMPTY_BYTE_ARRAY; } byte[] bArr = new byte[size]; copyToInternal(bArr, 0, 0, size); return bArr; } public final void writeTo(OutputStream outputStream, int i, int i2) throws IOException { checkRange(i, i + i2, size()); if (i2 > 0) { writeToInternal(outputStream, i, i2); } } public final String toString(String str) throws UnsupportedEncodingException { try { return toString(Charset.forName(str)); } catch (UnsupportedCharsetException e) { UnsupportedEncodingException unsupportedEncodingException = new UnsupportedEncodingException(str); unsupportedEncodingException.initCause(e); throw unsupportedEncodingException; } } public final String toString(Charset charset) { return size() == 0 ? "" : toStringInternal(charset); } public final String toStringUtf8() { return toString(Internal.UTF_8); } public static abstract class LeafByteString extends ByteString { public abstract boolean equalsRange(ByteString byteString, int i, int i2); @Override // androidx.datastore.preferences.protobuf.ByteString public final int getTreeDepth() { return 0; } @Override // androidx.datastore.preferences.protobuf.ByteString public final boolean isBalanced() { return true; } @Override // androidx.datastore.preferences.protobuf.ByteString, java.lang.Iterable public /* bridge */ /* synthetic */ Iterator iterator() { return super.iterator2(); } @Override // androidx.datastore.preferences.protobuf.ByteString public void writeToReverse(ByteOutput byteOutput) throws IOException { writeTo(byteOutput); } } public final int hashCode() { int i = this.hash; if (i == 0) { int size = size(); i = partialHash(size, 0, size); if (i == 0) { i = 1; } this.hash = i; } return i; } public static Output newOutput(int i) { return new Output(i); } public static Output newOutput() { return new Output(128); } public static final class Output extends OutputStream { private static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; private byte[] buffer; private int bufferPos; private final ArrayList flushedBuffers; private int flushedBuffersTotalBytes; private final int initialCapacity; public Output(int i) { if (i < 0) { throw new IllegalArgumentException("Buffer size < 0"); } this.initialCapacity = i; this.flushedBuffers = new ArrayList<>(); this.buffer = new byte[i]; } @Override // java.io.OutputStream public synchronized void write(int i) { try { if (this.bufferPos == this.buffer.length) { flushFullBuffer(1); } byte[] bArr = this.buffer; int i2 = this.bufferPos; this.bufferPos = i2 + 1; bArr[i2] = (byte) i; } catch (Throwable th) { throw th; } } @Override // java.io.OutputStream public synchronized void write(byte[] bArr, int i, int i2) { try { byte[] bArr2 = this.buffer; int length = bArr2.length; int i3 = this.bufferPos; if (i2 <= length - i3) { System.arraycopy(bArr, i, bArr2, i3, i2); this.bufferPos += i2; } else { int length2 = bArr2.length - i3; System.arraycopy(bArr, i, bArr2, i3, length2); int i4 = i2 - length2; flushFullBuffer(i4); System.arraycopy(bArr, i + length2, this.buffer, 0, i4); this.bufferPos = i4; } } catch (Throwable th) { throw th; } } public synchronized ByteString toByteString() { flushLastBuffer(); return ByteString.copyFrom(this.flushedBuffers); } private byte[] copyArray(byte[] bArr, int i) { byte[] bArr2 = new byte[i]; System.arraycopy(bArr, 0, bArr2, 0, Math.min(bArr.length, i)); return bArr2; } public void writeTo(OutputStream outputStream) throws IOException { ByteString[] byteStringArr; byte[] bArr; int i; synchronized (this) { ArrayList arrayList = this.flushedBuffers; byteStringArr = (ByteString[]) arrayList.toArray(new ByteString[arrayList.size()]); bArr = this.buffer; i = this.bufferPos; } for (ByteString byteString : byteStringArr) { byteString.writeTo(outputStream); } outputStream.write(copyArray(bArr, i)); } public synchronized int size() { return this.flushedBuffersTotalBytes + this.bufferPos; } public synchronized void reset() { this.flushedBuffers.clear(); this.flushedBuffersTotalBytes = 0; this.bufferPos = 0; } public String toString() { return String.format("", Integer.toHexString(System.identityHashCode(this)), Integer.valueOf(size())); } private void flushFullBuffer(int i) { this.flushedBuffers.add(new LiteralByteString(this.buffer)); int length = this.flushedBuffersTotalBytes + this.buffer.length; this.flushedBuffersTotalBytes = length; this.buffer = new byte[Math.max(this.initialCapacity, Math.max(i, length >>> 1))]; this.bufferPos = 0; } private void flushLastBuffer() { int i = this.bufferPos; byte[] bArr = this.buffer; if (i >= bArr.length) { this.flushedBuffers.add(new LiteralByteString(this.buffer)); this.buffer = EMPTY_BYTE_ARRAY; } else if (i > 0) { this.flushedBuffers.add(new LiteralByteString(copyArray(bArr, i))); } this.flushedBuffersTotalBytes += this.bufferPos; this.bufferPos = 0; } } public static CodedBuilder newCodedBuilder(int i) { return new CodedBuilder(i); } public static final class CodedBuilder { private final byte[] buffer; private final CodedOutputStream output; public CodedOutputStream getCodedOutput() { return this.output; } private CodedBuilder(int i) { byte[] bArr = new byte[i]; this.buffer = bArr; this.output = CodedOutputStream.newInstance(bArr); } public ByteString build() { this.output.checkNoSpaceLeft(); return new LiteralByteString(this.buffer); } } public static void checkIndex(int i, int i2) { if (((i2 - (i + 1)) | i) < 0) { if (i < 0) { throw new ArrayIndexOutOfBoundsException("Index < 0: " + i); } throw new ArrayIndexOutOfBoundsException("Index > length: " + i + ", " + i2); } } public static int checkRange(int i, int i2, int i3) { int i4 = i2 - i; if ((i | i2 | i4 | (i3 - i2)) >= 0) { return i4; } if (i < 0) { throw new IndexOutOfBoundsException("Beginning index: " + i + " < 0"); } if (i2 < i) { throw new IndexOutOfBoundsException("Beginning index larger than ending index: " + i + ", " + i2); } throw new IndexOutOfBoundsException("End index: " + i2 + " >= " + i3); } public final String toString() { return String.format("", Integer.toHexString(System.identityHashCode(this)), Integer.valueOf(size())); } public static class LiteralByteString extends LeafByteString { private static final long serialVersionUID = 1; protected final byte[] bytes; public int getOffsetIntoBytes() { return 0; } public LiteralByteString(byte[] bArr) { bArr.getClass(); this.bytes = bArr; } @Override // androidx.datastore.preferences.protobuf.ByteString public byte byteAt(int i) { return this.bytes[i]; } @Override // androidx.datastore.preferences.protobuf.ByteString public byte internalByteAt(int i) { return this.bytes[i]; } @Override // androidx.datastore.preferences.protobuf.ByteString public int size() { return this.bytes.length; } @Override // androidx.datastore.preferences.protobuf.ByteString public final ByteString substring(int i, int i2) { int checkRange = ByteString.checkRange(i, i2, size()); return checkRange == 0 ? ByteString.EMPTY : new BoundedByteString(this.bytes, getOffsetIntoBytes() + i, checkRange); } @Override // androidx.datastore.preferences.protobuf.ByteString public void copyToInternal(byte[] bArr, int i, int i2, int i3) { System.arraycopy(this.bytes, i, bArr, i2, i3); } @Override // androidx.datastore.preferences.protobuf.ByteString public final void copyTo(ByteBuffer byteBuffer) { byteBuffer.put(this.bytes, getOffsetIntoBytes(), size()); } @Override // androidx.datastore.preferences.protobuf.ByteString public final ByteBuffer asReadOnlyByteBuffer() { return ByteBuffer.wrap(this.bytes, getOffsetIntoBytes(), size()).asReadOnlyBuffer(); } @Override // androidx.datastore.preferences.protobuf.ByteString public final List asReadOnlyByteBufferList() { return Collections.singletonList(asReadOnlyByteBuffer()); } @Override // androidx.datastore.preferences.protobuf.ByteString public final void writeTo(OutputStream outputStream) throws IOException { outputStream.write(toByteArray()); } @Override // androidx.datastore.preferences.protobuf.ByteString public final void writeToInternal(OutputStream outputStream, int i, int i2) throws IOException { outputStream.write(this.bytes, getOffsetIntoBytes() + i, i2); } @Override // androidx.datastore.preferences.protobuf.ByteString public final void writeTo(ByteOutput byteOutput) throws IOException { byteOutput.writeLazy(this.bytes, getOffsetIntoBytes(), size()); } @Override // androidx.datastore.preferences.protobuf.ByteString public final String toStringInternal(Charset charset) { return new String(this.bytes, getOffsetIntoBytes(), size(), charset); } @Override // androidx.datastore.preferences.protobuf.ByteString public final boolean isValidUtf8() { int offsetIntoBytes = getOffsetIntoBytes(); return Utf8.isValidUtf8(this.bytes, offsetIntoBytes, size() + offsetIntoBytes); } @Override // androidx.datastore.preferences.protobuf.ByteString public final int partialIsValidUtf8(int i, int i2, int i3) { int offsetIntoBytes = getOffsetIntoBytes() + i2; return Utf8.partialIsValidUtf8(i, this.bytes, offsetIntoBytes, i3 + offsetIntoBytes); } @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) { LiteralByteString literalByteString = (LiteralByteString) obj; int peekCachedHashCode = peekCachedHashCode(); int peekCachedHashCode2 = literalByteString.peekCachedHashCode(); if (peekCachedHashCode == 0 || peekCachedHashCode2 == 0 || peekCachedHashCode == peekCachedHashCode2) { return equalsRange(literalByteString, 0, size()); } return false; } return obj.equals(this); } @Override // androidx.datastore.preferences.protobuf.ByteString.LeafByteString public final boolean equalsRange(ByteString byteString, int i, int i2) { if (i2 > byteString.size()) { throw new IllegalArgumentException("Length too large: " + i2 + size()); } int i3 = i + i2; if (i3 > byteString.size()) { throw new IllegalArgumentException("Ran off end of other: " + i + ", " + i2 + ", " + byteString.size()); } if (byteString instanceof LiteralByteString) { LiteralByteString literalByteString = (LiteralByteString) byteString; byte[] bArr = this.bytes; byte[] bArr2 = literalByteString.bytes; int offsetIntoBytes = getOffsetIntoBytes() + i2; int offsetIntoBytes2 = getOffsetIntoBytes(); int offsetIntoBytes3 = literalByteString.getOffsetIntoBytes() + i; while (offsetIntoBytes2 < offsetIntoBytes) { if (bArr[offsetIntoBytes2] != bArr2[offsetIntoBytes3]) { return false; } offsetIntoBytes2++; offsetIntoBytes3++; } return true; } return byteString.substring(i, i3).equals(substring(0, i2)); } @Override // androidx.datastore.preferences.protobuf.ByteString public final int partialHash(int i, int i2, int i3) { return Internal.partialHash(i, this.bytes, getOffsetIntoBytes() + i2, i3); } @Override // androidx.datastore.preferences.protobuf.ByteString public final InputStream newInput() { return new ByteArrayInputStream(this.bytes, getOffsetIntoBytes(), size()); } @Override // androidx.datastore.preferences.protobuf.ByteString public final CodedInputStream newCodedInput() { return CodedInputStream.newInstance(this.bytes, getOffsetIntoBytes(), size(), true); } } public static final class BoundedByteString extends LiteralByteString { private static final long serialVersionUID = 1; private final int bytesLength; private final int bytesOffset; @Override // androidx.datastore.preferences.protobuf.ByteString.LiteralByteString public int getOffsetIntoBytes() { return this.bytesOffset; } @Override // androidx.datastore.preferences.protobuf.ByteString.LiteralByteString, androidx.datastore.preferences.protobuf.ByteString public int size() { return this.bytesLength; } public BoundedByteString(byte[] bArr, int i, int i2) { super(bArr); ByteString.checkRange(i, i + i2, bArr.length); this.bytesOffset = i; this.bytesLength = i2; } @Override // androidx.datastore.preferences.protobuf.ByteString.LiteralByteString, androidx.datastore.preferences.protobuf.ByteString public byte byteAt(int i) { ByteString.checkIndex(i, size()); return this.bytes[this.bytesOffset + i]; } @Override // androidx.datastore.preferences.protobuf.ByteString.LiteralByteString, androidx.datastore.preferences.protobuf.ByteString public byte internalByteAt(int i) { return this.bytes[this.bytesOffset + i]; } @Override // androidx.datastore.preferences.protobuf.ByteString.LiteralByteString, androidx.datastore.preferences.protobuf.ByteString public void copyToInternal(byte[] bArr, int i, int i2, int i3) { System.arraycopy(this.bytes, getOffsetIntoBytes() + i, bArr, i2, i3); } public Object writeReplace() { return ByteString.wrap(toByteArray()); } private void readObject(ObjectInputStream objectInputStream) throws IOException { throw new InvalidObjectException("BoundedByteStream instances are not to be serialized directly"); } } }