CacheUtil.java 源代码


package com.mbridge.msdk.playercommon.exoplayer2.upstream.cache;

import android.net.Uri;
import com.mbridge.msdk.playercommon.exoplayer2.upstream.DataSource;
import com.mbridge.msdk.playercommon.exoplayer2.upstream.DataSpec;
import com.mbridge.msdk.playercommon.exoplayer2.upstream.cache.Cache;
import com.mbridge.msdk.playercommon.exoplayer2.util.Assertions;
import com.mbridge.msdk.playercommon.exoplayer2.util.PriorityTaskManager;
import com.mbridge.msdk.playercommon.exoplayer2.util.Util;
import java.io.EOFException;
import java.io.IOException;
import java.util.Iterator;
import java.util.concurrent.atomic.AtomicBoolean;

public final class CacheUtil {
    public static final int DEFAULT_BUFFER_SIZE_BYTES = 131072;

    public static class CachingCounters {
        public volatile long alreadyCachedBytes;
        public volatile long contentLength = -1;
        public volatile long newlyCachedBytes;

        public long totalCachedBytes() {
            return this.alreadyCachedBytes + this.newlyCachedBytes;
        }
    }

    private CacheUtil() {
    }

    public static void cache(DataSpec dataSpec, Cache cache, DataSource dataSource, CachingCounters cachingCounters, AtomicBoolean atomicBoolean) throws IOException, InterruptedException {
        cache(dataSpec, cache, new CacheDataSource(cache, dataSource), new byte[131072], null, 0, cachingCounters, atomicBoolean, false);
    }

    public static String generateKey(Uri uri) {
        return uri.toString();
    }

    public static void getCached(DataSpec dataSpec, Cache cache, CachingCounters cachingCounters) {
        long j6;
        String key = getKey(dataSpec);
        long j7 = dataSpec.absoluteStreamPosition;
        long j8 = dataSpec.length;
        if (j8 == -1) {
            j8 = cache.getContentLength(key);
        }
        cachingCounters.contentLength = j8;
        cachingCounters.alreadyCachedBytes = 0L;
        cachingCounters.newlyCachedBytes = 0L;
        long j9 = j7;
        long j10 = j8;
        while (j10 != 0) {
            if (j10 != -1) {
                j6 = j10;
            } else {
                j6 = Long.MAX_VALUE;
            }
            long cachedLength = cache.getCachedLength(key, j9, j6);
            if (cachedLength > 0) {
                cachingCounters.alreadyCachedBytes += cachedLength;
            } else {
                cachedLength = -cachedLength;
                if (cachedLength == Long.MAX_VALUE) {
                    return;
                }
            }
            j9 += cachedLength;
            if (j10 == -1) {
                cachedLength = 0;
            }
            j10 -= cachedLength;
        }
    }

    public static String getKey(DataSpec dataSpec) {
        String str = dataSpec.key;
        if (str == null) {
            return generateKey(dataSpec.uri);
        }
        return str;
    }

    private static long readAndDiscard(DataSpec dataSpec, long j6, long j7, DataSource dataSource, byte[] bArr, PriorityTaskManager priorityTaskManager, int i6, CachingCounters cachingCounters) throws IOException, InterruptedException {
        int length;
        DataSpec dataSpec2 = dataSpec;
        while (true) {
            if (priorityTaskManager != null) {
                priorityTaskManager.proceed(i6);
            }
            try {
                try {
                    if (!Thread.interrupted()) {
                        DataSpec dataSpec3 = new DataSpec(dataSpec2.uri, dataSpec2.postBody, j6, (dataSpec2.position + j6) - dataSpec2.absoluteStreamPosition, -1L, dataSpec2.key, dataSpec2.flags | 2);
                        try {
                            long open = dataSource.open(dataSpec3);
                            if (cachingCounters.contentLength == -1 && open != -1) {
                                cachingCounters.contentLength = dataSpec3.absoluteStreamPosition + open;
                            }
                            long j8 = 0;
                            while (true) {
                                if (j8 == j7) {
                                    break;
                                }
                                if (!Thread.interrupted()) {
                                    if (j7 != -1) {
                                        length = (int) Math.min(bArr.length, j7 - j8);
                                    } else {
                                        length = bArr.length;
                                    }
                                    int read = dataSource.read(bArr, 0, length);
                                    if (read == -1) {
                                        if (cachingCounters.contentLength == -1) {
                                            cachingCounters.contentLength = dataSpec3.absoluteStreamPosition + j8;
                                        }
                                    } else {
                                        long j9 = read;
                                        j8 += j9;
                                        cachingCounters.newlyCachedBytes += j9;
                                    }
                                } else {
                                    throw new InterruptedException();
                                }
                            }
                        } catch (PriorityTaskManager.PriorityTooLowException unused) {
                            dataSpec2 = dataSpec3;
                        }
                    } else {
                        throw new InterruptedException();
                    }
                } catch (PriorityTaskManager.PriorityTooLowException unused2) {
                }
                Util.closeQuietly(dataSource);
            } finally {
                Util.closeQuietly(dataSource);
            }
        }
    }

    public static void remove(Cache cache, String str) {
        Iterator<CacheSpan> it = cache.getCachedSpans(str).iterator();
        while (it.hasNext()) {
            try {
                cache.removeSpan(it.next());
            } catch (Cache.CacheException unused) {
            }
        }
    }

    public static void cache(DataSpec dataSpec, Cache cache, CacheDataSource cacheDataSource, byte[] bArr, PriorityTaskManager priorityTaskManager, int i6, CachingCounters cachingCounters, AtomicBoolean atomicBoolean, boolean z6) throws IOException, InterruptedException {
        CachingCounters cachingCounters2 = cachingCounters;
        Assertions.checkNotNull(cacheDataSource);
        Assertions.checkNotNull(bArr);
        if (cachingCounters2 != null) {
            getCached(dataSpec, cache, cachingCounters2);
        } else {
            cachingCounters2 = new CachingCounters();
        }
        CachingCounters cachingCounters3 = cachingCounters2;
        String key = getKey(dataSpec);
        long j6 = dataSpec.absoluteStreamPosition;
        long j7 = dataSpec.length;
        if (j7 == -1) {
            j7 = cache.getContentLength(key);
        }
        long j8 = j6;
        long j9 = j7;
        while (true) {
            long j10 = 0;
            if (j9 == 0) {
                return;
            }
            if (atomicBoolean != null && atomicBoolean.get()) {
                throw new InterruptedException();
            }
            long cachedLength = cache.getCachedLength(key, j8, j9 != -1 ? j9 : Long.MAX_VALUE);
            if (cachedLength <= 0) {
                long j11 = -cachedLength;
                if (readAndDiscard(dataSpec, j8, j11, cacheDataSource, bArr, priorityTaskManager, i6, cachingCounters3) < j11) {
                    if (z6 && j9 != -1) {
                        throw new EOFException();
                    }
                    return;
                }
                cachedLength = j11;
            }
            j8 += cachedLength;
            if (j9 != -1) {
                j10 = cachedLength;
            }
            j9 -= j10;
        }
    }
}