package androidx.datastore.preferences.protobuf; import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; import java.util.Iterator; /* loaded from: classes.dex */ class IterableByteBufferInputStream extends InputStream { private long currentAddress; private byte[] currentArray; private int currentArrayOffset; private ByteBuffer currentByteBuffer; private int currentByteBufferPos; private int currentIndex; private int dataSize = 0; private boolean hasArray; private Iterator iterator; public IterableByteBufferInputStream(Iterable iterable) { this.iterator = iterable.iterator(); for (ByteBuffer byteBuffer : iterable) { this.dataSize++; } this.currentIndex = -1; if (getNextByteBuffer()) { return; } this.currentByteBuffer = Internal.EMPTY_BYTE_BUFFER; this.currentIndex = 0; this.currentByteBufferPos = 0; this.currentAddress = 0L; } private boolean getNextByteBuffer() { this.currentIndex++; if (!this.iterator.hasNext()) { return false; } ByteBuffer next = this.iterator.next(); this.currentByteBuffer = next; this.currentByteBufferPos = next.position(); if (this.currentByteBuffer.hasArray()) { this.hasArray = true; this.currentArray = this.currentByteBuffer.array(); this.currentArrayOffset = this.currentByteBuffer.arrayOffset(); } else { this.hasArray = false; this.currentAddress = UnsafeUtil.addressOffset(this.currentByteBuffer); this.currentArray = null; } return true; } private void updateCurrentByteBufferPos(int i) { int i2 = this.currentByteBufferPos + i; this.currentByteBufferPos = i2; if (i2 == this.currentByteBuffer.limit()) { getNextByteBuffer(); } } @Override // java.io.InputStream public int read() throws IOException { if (this.currentIndex == this.dataSize) { return -1; } if (this.hasArray) { int i = this.currentArray[this.currentByteBufferPos + this.currentArrayOffset] & 255; updateCurrentByteBufferPos(1); return i; } int i2 = UnsafeUtil.getByte(this.currentByteBufferPos + this.currentAddress) & 255; updateCurrentByteBufferPos(1); return i2; } @Override // java.io.InputStream public int read(byte[] bArr, int i, int i2) throws IOException { if (this.currentIndex == this.dataSize) { return -1; } int limit = this.currentByteBuffer.limit(); int i3 = this.currentByteBufferPos; int i4 = limit - i3; if (i2 > i4) { i2 = i4; } if (this.hasArray) { System.arraycopy(this.currentArray, i3 + this.currentArrayOffset, bArr, i, i2); updateCurrentByteBufferPos(i2); } else { int position = this.currentByteBuffer.position(); this.currentByteBuffer.position(this.currentByteBufferPos); this.currentByteBuffer.get(bArr, i, i2); this.currentByteBuffer.position(position); updateCurrentByteBufferPos(i2); } return i2; } }