package androidx.media3.datasource; import android.net.Uri; import androidx.media3.common.util.Assertions; import java.io.IOException; import java.util.List; import java.util.Map; /* loaded from: classes2.dex */ public final class TeeDataSource implements DataSource { private long bytesRemaining; private final DataSink dataSink; private boolean dataSinkNeedsClosing; private final DataSource upstream; public TeeDataSource(DataSource dataSource, DataSink dataSink) { this.upstream = (DataSource) Assertions.checkNotNull(dataSource); this.dataSink = (DataSink) Assertions.checkNotNull(dataSink); } @Override // androidx.media3.datasource.DataSource public void addTransferListener(TransferListener transferListener) { Assertions.checkNotNull(transferListener); this.upstream.addTransferListener(transferListener); } @Override // androidx.media3.datasource.DataSource public long open(DataSpec dataSpec) throws IOException { long open = this.upstream.open(dataSpec); this.bytesRemaining = open; if (open == 0) { return 0L; } if (dataSpec.length == -1) { long j = this.bytesRemaining; if (j != -1) { dataSpec = dataSpec.subrange(0L, j); } } this.dataSinkNeedsClosing = true; this.dataSink.open(dataSpec); return this.bytesRemaining; } @Override // androidx.media3.common.DataReader public int read(byte[] bArr, int i, int i2) throws IOException { if (this.bytesRemaining == 0) { return -1; } int read = this.upstream.read(bArr, i, i2); if (read > 0) { this.dataSink.write(bArr, i, read); long j = this.bytesRemaining; if (j != -1) { this.bytesRemaining = j - read; } } return read; } @Override // androidx.media3.datasource.DataSource public Uri getUri() { return this.upstream.getUri(); } @Override // androidx.media3.datasource.DataSource public Map> getResponseHeaders() { return this.upstream.getResponseHeaders(); } @Override // androidx.media3.datasource.DataSource public void close() throws IOException { try { this.upstream.close(); } finally { if (this.dataSinkNeedsClosing) { this.dataSinkNeedsClosing = false; this.dataSink.close(); } } } }