package androidx.media3.datasource.cache;

import androidx.media3.common.util.UnstableApi;
import androidx.media3.datasource.DataSourceUtil;
import androidx.media3.datasource.DataSpec;
import java.io.IOException;
import java.io.InterruptedIOException;

/* JADX INFO: loaded from: classes.dex */
@UnstableApi
public final class CacheWriter {
    public static final int DEFAULT_BUFFER_SIZE_BYTES = 131072;
    private long bytesCached;
    private final Cache cache;
    private final String cacheKey;
    private final CacheDataSource dataSource;
    private final DataSpec dataSpec;
    private long endPosition;
    private volatile boolean isCanceled;
    private long nextPosition;
    private final ProgressListener progressListener;
    private final byte[] temporaryBuffer;

    public interface ProgressListener {
        void onProgress(long j4, long j6, long j7);
    }

    public CacheWriter(CacheDataSource cacheDataSource, DataSpec dataSpec, byte[] bArr, ProgressListener progressListener) {
        this.dataSource = cacheDataSource;
        this.cache = cacheDataSource.getCache();
        this.dataSpec = dataSpec;
        this.temporaryBuffer = bArr == null ? new byte[131072] : bArr;
        this.progressListener = progressListener;
        this.cacheKey = cacheDataSource.getCacheKeyFactory().buildCacheKey(dataSpec);
        this.nextPosition = dataSpec.position;
    }

    private long getLength() {
        long j4 = this.endPosition;
        if (j4 == -1) {
            return -1L;
        }
        return j4 - this.dataSpec.position;
    }

    private void onNewBytesCached(long j4) {
        this.bytesCached += j4;
        ProgressListener progressListener = this.progressListener;
        if (progressListener != null) {
            progressListener.onProgress(getLength(), this.bytesCached, j4);
        }
    }

    private void onRequestEndPosition(long j4) {
        if (this.endPosition == j4) {
            return;
        }
        this.endPosition = j4;
        ProgressListener progressListener = this.progressListener;
        if (progressListener != null) {
            progressListener.onProgress(getLength(), this.bytesCached, 0L);
        }
    }

    private long readBlockToCache(long j4, long j6) throws IOException {
        long jOpen;
        boolean z6 = true;
        boolean z7 = j4 + j6 == this.endPosition || j6 == -1;
        if (j6 != -1) {
            try {
                jOpen = this.dataSource.open(this.dataSpec.buildUpon().setPosition(j4).setLength(j6).build());
            } catch (IOException unused) {
                DataSourceUtil.closeQuietly(this.dataSource);
                z6 = false;
                jOpen = -1;
            }
        } else {
            z6 = false;
            jOpen = -1;
        }
        if (!z6) {
            throwIfCanceled();
            try {
                jOpen = this.dataSource.open(this.dataSpec.buildUpon().setPosition(j4).setLength(-1L).build());
            } catch (IOException e6) {
                DataSourceUtil.closeQuietly(this.dataSource);
                throw e6;
            }
        }
        if (z7 && jOpen != -1) {
            try {
                onRequestEndPosition(jOpen + j4);
            } catch (IOException e7) {
                DataSourceUtil.closeQuietly(this.dataSource);
                throw e7;
            }
        }
        int i6 = 0;
        int i7 = 0;
        while (i6 != -1) {
            throwIfCanceled();
            CacheDataSource cacheDataSource = this.dataSource;
            byte[] bArr = this.temporaryBuffer;
            i6 = cacheDataSource.read(bArr, 0, bArr.length);
            if (i6 != -1) {
                onNewBytesCached(i6);
                i7 += i6;
            }
        }
        if (z7) {
            onRequestEndPosition(j4 + ((long) i7));
        }
        this.dataSource.close();
        return i7;
    }

    private void throwIfCanceled() throws InterruptedIOException {
        if (this.isCanceled) {
            throw new InterruptedIOException();
        }
    }

    public void cache() throws InterruptedIOException {
        throwIfCanceled();
        Cache cache = this.cache;
        String str = this.cacheKey;
        DataSpec dataSpec = this.dataSpec;
        this.bytesCached = cache.getCachedBytes(str, dataSpec.position, dataSpec.length);
        DataSpec dataSpec2 = this.dataSpec;
        long j4 = dataSpec2.length;
        if (j4 != -1) {
            this.endPosition = dataSpec2.position + j4;
        } else {
            long contentLength = ContentMetadata.getContentLength(this.cache.getContentMetadata(this.cacheKey));
            if (contentLength == -1) {
                contentLength = -1;
            }
            this.endPosition = contentLength;
        }
        ProgressListener progressListener = this.progressListener;
        if (progressListener != null) {
            progressListener.onProgress(getLength(), this.bytesCached, 0L);
        }
        while (true) {
            long j6 = this.endPosition;
            if (j6 != -1 && this.nextPosition >= j6) {
                return;
            }
            throwIfCanceled();
            long j7 = this.endPosition;
            long cachedLength = this.cache.getCachedLength(this.cacheKey, this.nextPosition, j7 == -1 ? Long.MAX_VALUE : j7 - this.nextPosition);
            if (cachedLength > 0) {
                this.nextPosition += cachedLength;
            } else {
                long j8 = -cachedLength;
                if (j8 == Long.MAX_VALUE) {
                    j8 = -1;
                }
                long j9 = this.nextPosition;
                this.nextPosition = j9 + readBlockToCache(j9, j8);
            }
        }
    }

    public void cancel() {
        this.isCanceled = true;
    }
}
