package androidx.media3.extractor; import androidx.media3.common.DataReader; import androidx.media3.common.MediaLibraryInfo; import androidx.media3.common.util.Assertions; import androidx.media3.common.util.Util; import java.io.EOFException; import java.io.IOException; import java.io.InterruptedIOException; import java.util.Arrays; /* loaded from: classes2.dex */ public final class DefaultExtractorInput implements ExtractorInput { private static final int PEEK_MAX_FREE_SPACE = 524288; private static final int PEEK_MIN_FREE_SPACE_AFTER_RESIZE = 65536; private static final int SCRATCH_SPACE_SIZE = 4096; private final DataReader dataReader; private int peekBufferLength; private int peekBufferPosition; private long position; private final long streamLength; private byte[] peekBuffer = new byte[65536]; private final byte[] scratchSpace = new byte[4096]; private void commitBytesRead(int i) { if (i != -1) { this.position += i; } } @Override // androidx.media3.extractor.ExtractorInput public long getLength() { return this.streamLength; } @Override // androidx.media3.extractor.ExtractorInput public long getPeekPosition() { return this.position + this.peekBufferPosition; } @Override // androidx.media3.extractor.ExtractorInput public long getPosition() { return this.position; } @Override // androidx.media3.extractor.ExtractorInput public void resetPeekPosition() { this.peekBufferPosition = 0; } static { MediaLibraryInfo.registerModule("media3.extractor"); } public DefaultExtractorInput(DataReader dataReader, long j, long j2) { this.dataReader = dataReader; this.position = j; this.streamLength = j2; } @Override // androidx.media3.extractor.ExtractorInput, androidx.media3.common.DataReader public int read(byte[] bArr, int i, int i2) throws IOException { int readFromPeekBuffer = readFromPeekBuffer(bArr, i, i2); if (readFromPeekBuffer == 0) { readFromPeekBuffer = readFromUpstream(bArr, i, i2, 0, true); } commitBytesRead(readFromPeekBuffer); return readFromPeekBuffer; } @Override // androidx.media3.extractor.ExtractorInput public boolean readFully(byte[] bArr, int i, int i2, boolean z) throws IOException { int readFromPeekBuffer = readFromPeekBuffer(bArr, i, i2); while (readFromPeekBuffer < i2 && readFromPeekBuffer != -1) { readFromPeekBuffer = readFromUpstream(bArr, i, i2, readFromPeekBuffer, z); } commitBytesRead(readFromPeekBuffer); return readFromPeekBuffer != -1; } @Override // androidx.media3.extractor.ExtractorInput public void readFully(byte[] bArr, int i, int i2) throws IOException { readFully(bArr, i, i2, false); } @Override // androidx.media3.extractor.ExtractorInput public int skip(int i) throws IOException { int skipFromPeekBuffer = skipFromPeekBuffer(i); if (skipFromPeekBuffer == 0) { byte[] bArr = this.scratchSpace; skipFromPeekBuffer = readFromUpstream(bArr, 0, Math.min(i, bArr.length), 0, true); } commitBytesRead(skipFromPeekBuffer); return skipFromPeekBuffer; } @Override // androidx.media3.extractor.ExtractorInput public boolean skipFully(int i, boolean z) throws IOException { int skipFromPeekBuffer = skipFromPeekBuffer(i); while (skipFromPeekBuffer < i && skipFromPeekBuffer != -1) { skipFromPeekBuffer = readFromUpstream(this.scratchSpace, -skipFromPeekBuffer, Math.min(i, this.scratchSpace.length + skipFromPeekBuffer), skipFromPeekBuffer, z); } commitBytesRead(skipFromPeekBuffer); return skipFromPeekBuffer != -1; } @Override // androidx.media3.extractor.ExtractorInput public void skipFully(int i) throws IOException { skipFully(i, false); } @Override // androidx.media3.extractor.ExtractorInput public int peek(byte[] bArr, int i, int i2) throws IOException { int min; ensureSpaceForPeek(i2); int i3 = this.peekBufferLength; int i4 = this.peekBufferPosition; int i5 = i3 - i4; if (i5 == 0) { min = readFromUpstream(this.peekBuffer, i4, i2, 0, true); if (min == -1) { return -1; } this.peekBufferLength += min; } else { min = Math.min(i2, i5); } System.arraycopy(this.peekBuffer, this.peekBufferPosition, bArr, i, min); this.peekBufferPosition += min; return min; } @Override // androidx.media3.extractor.ExtractorInput public boolean peekFully(byte[] bArr, int i, int i2, boolean z) throws IOException { if (!advancePeekPosition(i2, z)) { return false; } System.arraycopy(this.peekBuffer, this.peekBufferPosition - i2, bArr, i, i2); return true; } @Override // androidx.media3.extractor.ExtractorInput public void peekFully(byte[] bArr, int i, int i2) throws IOException { peekFully(bArr, i, i2, false); } @Override // androidx.media3.extractor.ExtractorInput public boolean advancePeekPosition(int i, boolean z) throws IOException { ensureSpaceForPeek(i); int i2 = this.peekBufferLength - this.peekBufferPosition; while (i2 < i) { i2 = readFromUpstream(this.peekBuffer, this.peekBufferPosition, i, i2, z); if (i2 == -1) { return false; } this.peekBufferLength = this.peekBufferPosition + i2; } this.peekBufferPosition += i; return true; } @Override // androidx.media3.extractor.ExtractorInput public void advancePeekPosition(int i) throws IOException { advancePeekPosition(i, false); } @Override // androidx.media3.extractor.ExtractorInput public void setRetryPosition(long j, E e) throws Throwable { Assertions.checkArgument(j >= 0); this.position = j; throw e; } private void ensureSpaceForPeek(int i) { int i2 = this.peekBufferPosition + i; byte[] bArr = this.peekBuffer; if (i2 > bArr.length) { this.peekBuffer = Arrays.copyOf(this.peekBuffer, Util.constrainValue(bArr.length * 2, 65536 + i2, i2 + 524288)); } } private int skipFromPeekBuffer(int i) { int min = Math.min(this.peekBufferLength, i); updatePeekBuffer(min); return min; } private int readFromPeekBuffer(byte[] bArr, int i, int i2) { int i3 = this.peekBufferLength; if (i3 == 0) { return 0; } int min = Math.min(i3, i2); System.arraycopy(this.peekBuffer, 0, bArr, i, min); updatePeekBuffer(min); return min; } private void updatePeekBuffer(int i) { int i2 = this.peekBufferLength - i; this.peekBufferLength = i2; this.peekBufferPosition = 0; byte[] bArr = this.peekBuffer; byte[] bArr2 = i2 < bArr.length - 524288 ? new byte[65536 + i2] : bArr; System.arraycopy(bArr, i, bArr2, 0, i2); this.peekBuffer = bArr2; } private int readFromUpstream(byte[] bArr, int i, int i2, int i3, boolean z) throws IOException { if (Thread.interrupted()) { throw new InterruptedIOException(); } int read = this.dataReader.read(bArr, i + i3, i2 - i3); if (read != -1) { return i3 + read; } if (i3 == 0 && z) { return -1; } throw new EOFException(); } }