package z0;

import a0.n;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.util.Log;
import androidx.media3.datasource.cache.CacheDataSink;
import b0.o;
import b0.x;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import k1.Y;

/* JADX INFO: renamed from: z0.a, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public class C0750a implements Handler.Callback {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private final Handler f7083a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private final HandlerThread f7084b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private InputStream f7085c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private final int f7086d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private final n.b f7087e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private final long f7088f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private final Lock f7089g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private final Condition f7090h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private volatile b f7091i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private volatile b f7092j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    private volatile long f7093k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    private long f7094l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    private long f7095m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    private long f7096n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    private volatile boolean f7097o;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    private final long f7098p;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    private final ReadWriteLock f7099q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    private volatile boolean f7100r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    private volatile boolean f7101s;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    private boolean f7103u;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    private volatile boolean f7102t = false;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    private boolean f7104v = false;

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    private boolean f7105w = true;

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    private boolean f7106x = false;

    /* JADX INFO: renamed from: z0.a$a, reason: collision with other inner class name */
    static class C0107a {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        public byte[] f7107a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public int f7108b;

        C0107a() {
        }
    }

    /* JADX INFO: renamed from: z0.a$b */
    static class b {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        private int f7109a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        private int f7110b = 0;

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        private volatile List f7111c = new ArrayList();

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        private volatile List f7112d = new ArrayList();

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        public volatile long f7113e = 0;

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        public volatile long f7114f = 0;

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        public volatile boolean f7115g = true;

        /* JADX INFO: renamed from: h, reason: collision with root package name */
        public volatile boolean f7116h = false;

        /* JADX INFO: renamed from: i, reason: collision with root package name */
        public volatile int f7117i = 1;

        /* JADX INFO: renamed from: j, reason: collision with root package name */
        public volatile int f7118j = -1000;

        public b(int i2) {
            this.f7109a = i2;
        }

        /* JADX INFO: Access modifiers changed from: private */
        public int g(int i2) {
            int i3 = 0;
            while (i2 > 0 && !this.f7111c.isEmpty()) {
                C0107a c0107a = (C0107a) this.f7111c.get(0);
                if (i2 < c0107a.f7108b) {
                    break;
                }
                this.f7111c.remove(0);
                int i4 = c0107a.f7108b;
                i2 -= i4;
                i3 += i4;
                h(c0107a);
            }
            this.f7110b -= i3;
            return i3;
        }

        public C0107a c() {
            if (!this.f7112d.isEmpty()) {
                return (C0107a) this.f7112d.remove(0);
            }
            C0107a c0107a = new C0107a();
            c0107a.f7107a = new byte[this.f7109a];
            c0107a.f7108b = 0;
            return c0107a;
        }

        public void d(C0107a c0107a) {
            this.f7110b += c0107a.f7108b;
            this.f7111c.add(c0107a);
        }

        public int e(int i2, byte[] bArr, int i3) {
            if (i3 == 0 || this.f7111c.isEmpty()) {
                return 0;
            }
            C0107a c0107a = (C0107a) this.f7111c.get(0);
            long j2 = 0;
            int i4 = 0;
            int i5 = 0;
            while (true) {
                if (i4 >= this.f7111c.size()) {
                    i4 = i5;
                    break;
                }
                c0107a = (C0107a) this.f7111c.get(i4);
                long j3 = i2;
                int i6 = c0107a.f7108b;
                if (j3 <= ((long) i6) + j2) {
                    break;
                }
                j2 += (long) i6;
                i5 = i4;
                i4++;
            }
            int i7 = (int) (((long) i2) - j2);
            int i8 = c0107a.f7108b;
            if (i8 - i7 >= i3) {
                System.arraycopy(c0107a.f7107a, i7, bArr, 0, i3);
                return i3;
            }
            int i9 = i8 - i7;
            System.arraycopy(c0107a.f7107a, i7, bArr, 0, i9);
            int i10 = i3 - i9;
            while (true) {
                i4++;
                if (i4 >= this.f7111c.size()) {
                    return i9;
                }
                C0107a c0107a2 = (C0107a) this.f7111c.get(i4);
                int i11 = c0107a2.f7108b;
                if (i11 >= i10) {
                    System.arraycopy(c0107a2.f7107a, 0, bArr, i9, i10);
                    return i9 + i10;
                }
                System.arraycopy(c0107a2.f7107a, 0, bArr, i9, i11);
                int i12 = c0107a2.f7108b;
                i9 += i12;
                i10 -= i12;
            }
        }

        public void f() {
            for (int i2 = 0; i2 < this.f7111c.size(); i2++) {
                ((C0107a) this.f7111c.get(i2)).f7107a = null;
            }
            for (int i3 = 0; i3 < this.f7112d.size(); i3++) {
                ((C0107a) this.f7112d.get(i3)).f7107a = null;
            }
        }

        public void h(C0107a c0107a) {
            c0107a.f7108b = 0;
            this.f7112d.add(c0107a);
        }
    }

    /* JADX INFO: renamed from: z0.a$c */
    private class c {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        public long f7119a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public byte[] f7120b;

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        public int f7121c;

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        public b f7122d;

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        public Object f7123e;

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        public boolean f7124f;

        private c() {
        }
    }

    public C0750a(InputStream inputStream, n.b bVar, long j2, int i2) {
        this.f7085c = inputStream;
        this.f7088f = j2;
        this.f7086d = i2;
        ReentrantLock reentrantLock = new ReentrantLock();
        this.f7089g = reentrantLock;
        this.f7090h = reentrantLock.newCondition();
        this.f7097o = false;
        this.f7101s = false;
        this.f7091i = new b(64512);
        this.f7092j = new b(64512);
        this.f7093k = 0L;
        this.f7094l = 0L;
        this.f7095m = 0L;
        this.f7096n = 0L;
        long j3 = j2 / 100;
        this.f7098p = j3 <= CacheDataSink.DEFAULT_FRAGMENT_SIZE ? 5242880L : j3;
        x xVar = new x(getClass().getSimpleName() + ":Handler", -20);
        this.f7084b = xVar;
        this.f7099q = new ReentrantReadWriteLock();
        Log.d("CachedSource", "create CachedSource thread");
        xVar.start();
        this.f7083a = new Handler(xVar.getLooper(), this);
    }

    private void a(long j2) {
        o(this.f7095m);
        this.f7089g.lock();
        this.f7090h.signal();
        this.f7089g.unlock();
        Log.d("CachedSource", "sendEmptyMessageDelayed(MSG_FETCH_MORE) 009");
        this.f7083a.sendEmptyMessageDelayed(1, 0L);
        this.f7104v = true;
    }

    private long b(long j2) {
        return 0L;
    }

    private void c(b bVar, long j2) {
        e(this.f7093k);
        try {
            C0107a c0107aC = bVar.c();
            if (bVar.f7110b == 0) {
                if (bVar.f7113e != this.f7094l) {
                    Log.d("CachedSource", "skipInputStream seek stream: new cache");
                    o(bVar.f7113e);
                    this.f7094l = bVar.f7113e;
                    Log.d("CachedSource", "inputStreamLastReadAtByteOffset = " + this.f7094l);
                }
            } else if (bVar.f7113e + ((long) bVar.f7110b) != this.f7094l) {
                Log.d("CachedSource", "test skipInputStream seek stream: default");
                o(bVar.f7113e + ((long) bVar.f7110b));
                this.f7094l = bVar.f7113e + ((long) bVar.f7110b);
            }
            try {
                long jCurrentTimeMillis = System.currentTimeMillis();
                o.d("CachedSource", "+mInputStream.read()", 64);
                int i2 = this.f7085c.read(c0107aC.f7107a, 0, 64512);
                o.d("CachedSource", "-mInputStream.read()", 64);
                o.d("CachedSource", "-inputStream.read, ret = " + i2 + ", spendTime(ms) = " + (System.currentTimeMillis() - jCurrentTimeMillis), 64);
                if (i2 == -1) {
                    bVar.f7117i = -1;
                    Log.d("CachedSource", "pageCache.finalStatus = STATUS_END_OF_STREAM");
                    bVar.h(c0107aC);
                } else {
                    if (i2 == -2) {
                        bVar.f7117i = -2;
                        bVar.h(c0107aC);
                        return;
                    }
                    bVar.f7117i = 1;
                    c0107aC.f7108b = i2;
                    bVar.d(c0107aC);
                    this.f7094l = bVar.f7113e + ((long) bVar.f7110b);
                    o.d("CachedSource", "005 inputStreamLastReadAtByteOffset = " + this.f7094l, 64);
                }
            } catch (IOException e2) {
                Log.e("CachedSource", "inputStream.read, catch IOException: " + e2.getMessage());
                bVar.h(c0107aC);
                bVar.f7117i = -3;
                Log.e("CachedSource", "pageCache.finalStatus = STATUS_READ_EXCEPTION");
            }
        } catch (Exception e3) {
            Log.e("CachedSource", "acquirePage(): exception:" + e3.toString());
        }
    }

    private boolean e(long j2) {
        int i2 = this.f7086d;
        if (i2 == 0 || i2 == 13) {
            this.f7092j.f7117i = -1;
            return true;
        }
        if (this.f7092j.f7113e > 0 && j2 >= this.f7092j.f7113e) {
            return false;
        }
        if (this.f7091i.f7113e + ((long) this.f7091i.f7110b) != this.f7088f && this.f7092j.f7113e == 0) {
            long j3 = this.f7088f;
            long j4 = this.f7098p;
            if (j3 > j4 && j2 > j3 - j4) {
                return false;
            }
        }
        return true;
    }

    private void f(long j2) {
        int i2 = this.f7086d;
        if (13 == i2 || i2 == 0) {
            Log.e("CachedSource", "keepStreamAndSkipStream mSeekType not support skip");
            return;
        }
        try {
            try {
                this.f7085c.skip(j2);
            } catch (IOException unused) {
                Log.d("CachedSource", "IOException e 001");
            }
        } catch (IOException unused2) {
            this.f7085c.close();
            Log.d("CachedSource", "IOException e 001");
        }
    }

    private void g() {
        boolean zE = e(this.f7093k);
        b bVar = zE ? this.f7091i : this.f7092j;
        o.d("CachedSource", "onFetch(), status(" + zE + ") = " + bVar.f7117i + ", fetching = " + bVar.f7115g + ", totalSize = " + bVar.f7110b + ", isCacheFull = " + bVar.f7116h, 64);
        if (bVar.f7117i != 1) {
            Log.d("CachedSource", "isFirstPart:" + zE);
            Log.d("CachedSource", "onFetch finalStatus:" + bVar.f7117i);
            bVar.f7115g = false;
        }
        if (bVar.f7115g) {
            c(bVar, this.f7093k);
        }
        if (bVar.f7115g && d(bVar) >= 20971520) {
            bVar.f7115g = false;
        }
        if (bVar.f7110b >= 10485760) {
            boolean z2 = !this.f7103u && this.f7086d == 0;
            o.d("CachedSource", "isFirstPart(" + zE + ") cache full, done prefetching for now, isForceRelease=" + z2, 64);
            m(bVar, zE, z2);
        }
        int i2 = bVar.f7115g ? 0 : 100;
        if (bVar.f7117i == -2 || bVar.f7117i == -3) {
            Log.d("CachedSource", "break pageCache.finalStatus" + bVar.f7117i);
        } else {
            if (this.f7091i.f7117i != -1) {
                try {
                    o.d("CachedSource", "sendMessageDelayed(MSG_FETCH_MORE, " + i2 + ") 006", 64);
                    Handler handler = this.f7083a;
                    handler.sendMessageDelayed(handler.obtainMessage(1, 0, 0), (long) i2);
                    this.f7104v = true;
                    return;
                } catch (IllegalStateException unused) {
                    return;
                }
            }
            Log.d("CachedSource", "globalPageCache.finalStatus" + this.f7091i.f7117i);
        }
        this.f7104v = false;
        Log.d("CachedSource", "isFirstPart:" + zE + "finalStatus: " + bVar.f7117i + " cancel fetch loop!");
    }

    private void h(c cVar) {
        int iK = k(cVar);
        if (iK == 2 || iK == 3) {
            Handler handler = this.f7083a;
            handler.sendMessageDelayed(handler.obtainMessage(2, cVar), 10L);
            return;
        }
        cVar.f7122d.f7118j = iK;
        this.f7089g.lock();
        synchronized (cVar.f7123e) {
            cVar.f7124f = false;
        }
        this.f7102t = true;
        this.f7090h.signal();
        this.f7089g.unlock();
    }

    private int k(c cVar) {
        long j2 = cVar.f7119a;
        byte[] bArr = cVar.f7120b;
        int i2 = cVar.f7121c;
        b bVar = cVar.f7122d;
        o.d("CachedSource", "readCacheInternal offset: " + cVar.f7119a + " size:" + cVar.f7121c + " cachedOffset:" + bVar.f7113e + " totalSize:" + bVar.f7110b + " finalStatus = " + bVar.f7117i + " lastAccessPosition:" + bVar.f7114f, 64);
        if (bVar.f7117i == -2 || bVar.f7117i == -3) {
            Log.d("CachedSource", "001 server retuen error pageCache.finalStatus" + bVar.f7117i);
            return bVar.f7117i;
        }
        if (j2 < bVar.f7113e || j2 >= bVar.f7113e + ((long) bVar.f7110b) || ((long) i2) + j2 >= bVar.f7113e + ((long) bVar.f7110b)) {
            n(j2, bVar);
        }
        int i3 = (int) (j2 - bVar.f7113e);
        if (bVar.f7117i == 1) {
            if (j2 + ((long) i2) <= bVar.f7113e + ((long) bVar.f7110b)) {
                o.d("CachedSource", "cache enough pageCache.copy(delta, data, size)", 64);
                bVar.e(i3, bArr, i2);
                return i2;
            }
            o.d("CachedSource", "deferring readCache, data is not enough", 64);
            if (this.f7104v) {
                return 2;
            }
            Log.d("CachedSource", "sendEmptyMessage(MSG_FETCH_MORE) 010");
            this.f7083a.sendEmptyMessage(1);
            this.f7104v = true;
            return 2;
        }
        Log.d("CachedSource", "pageCache.finalStatus != STATUS_OK 001" + bVar.f7117i);
        if (i3 > bVar.f7110b) {
            Log.d("CachedSource", "pageCache.finalStatus:" + bVar.f7117i);
            return bVar.f7117i;
        }
        int i4 = bVar.f7110b - i3;
        if (i4 <= i2) {
            i2 = i4;
        }
        bVar.e(i3, bArr, i2);
        return i2;
    }

    private void l(long j2) {
        int i2 = this.f7086d;
        if (13 == i2 || i2 == 0) {
            Log.e("CachedSource", "reopenStreamAndSkipStream mSeekType not support skip");
            return;
        }
        try {
            this.f7085c.close();
            Log.d("CachedSource", "inputStream.close 898");
        } catch (IOException unused) {
            Log.d("CachedSource", "inputStream.close IOException e 003");
        }
        this.f7085c = this.f7087e.c();
        Log.d("CachedSource", "newInputStream 902");
        try {
            try {
                this.f7085c.skip(j2);
            } catch (IOException unused2) {
                Log.d("CachedSource", "IOException e 004");
            }
        } catch (IOException unused3) {
            this.f7085c.close();
            Log.d("CachedSource", "IOException e 004");
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:24:0x0091 A[Catch: all -> 0x0084, Exception -> 0x00a4, TryCatch #1 {all -> 0x0084, blocks: (B:19:0x0051, B:22:0x0086, B:24:0x0091, B:25:0x0096, B:29:0x00a4), top: B:39:0x004f, outer: #0 }] */
    /* JADX WARN: Removed duplicated region for block: B:25:0x0096 A[Catch: all -> 0x0084, Exception -> 0x00a4, TRY_LEAVE, TryCatch #1 {all -> 0x0084, blocks: (B:19:0x0051, B:22:0x0086, B:24:0x0091, B:25:0x0096, B:29:0x00a4), top: B:39:0x004f, outer: #0 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    private synchronized void m(z0.C0750a.b r7, boolean r8, boolean r9) {
        /*
            r6 = this;
            monitor-enter(r6)
            r0 = 0
            r1 = 1
            if (r9 != 0) goto L45
            int r9 = r7.f7117i     // Catch: java.lang.Throwable -> L23
            if (r9 == r1) goto L26
            java.lang.String r9 = "CachedSource"
            java.lang.StringBuilder r2 = new java.lang.StringBuilder     // Catch: java.lang.Throwable -> L23
            r2.<init>()     // Catch: java.lang.Throwable -> L23
            java.lang.String r3 = "restart return. finalStatus"
            r2.append(r3)     // Catch: java.lang.Throwable -> L23
            int r3 = r7.f7117i     // Catch: java.lang.Throwable -> L23
            r2.append(r3)     // Catch: java.lang.Throwable -> L23
            java.lang.String r2 = r2.toString()     // Catch: java.lang.Throwable -> L23
            android.util.Log.d(r9, r2)     // Catch: java.lang.Throwable -> L23
            r9 = r0
            goto L27
        L23:
            r7 = move-exception
            goto Lbe
        L26:
            r9 = r1
        L27:
            long r2 = r6.d(r7)     // Catch: java.lang.Throwable -> L23
            r4 = 2097152(0x200000, double:1.036131E-317)
            int r2 = (r2 > r4 ? 1 : (r2 == r4 ? 0 : -1))
            if (r2 < 0) goto L46
            int r2 = z0.C0750a.b.a(r7)     // Catch: java.lang.Throwable -> L23
            r3 = 20971520(0x1400000, float:3.526483E-38)
            if (r2 >= r3) goto L46
            java.lang.String r9 = "CachedSource"
            java.lang.String r2 = "return, Still have more than 2 M data, cache is enough"
            r3 = 64
            b0.o.d(r9, r2, r3)     // Catch: java.lang.Throwable -> L23
            r9 = r0
            goto L46
        L45:
            r9 = r1
        L46:
            java.util.concurrent.locks.ReadWriteLock r2 = r6.f7099q     // Catch: java.lang.Throwable -> L23
            java.util.concurrent.locks.Lock r2 = r2.writeLock()     // Catch: java.lang.Throwable -> L23
            r2.lock()     // Catch: java.lang.Throwable -> L23
            if (r9 == 0) goto L86
            long r2 = r7.f7114f     // Catch: java.lang.Throwable -> L84 java.lang.Exception -> La4
            long r4 = r7.f7113e     // Catch: java.lang.Throwable -> L84 java.lang.Exception -> La4
            long r2 = r2 - r4
            int r9 = (int) r2     // Catch: java.lang.Throwable -> L84 java.lang.Exception -> La4
            int r9 = z0.C0750a.b.b(r7, r9)     // Catch: java.lang.Throwable -> L84 java.lang.Exception -> La4
            long r2 = r7.f7113e     // Catch: java.lang.Throwable -> L84 java.lang.Exception -> La4
            long r4 = (long) r9     // Catch: java.lang.Throwable -> L84 java.lang.Exception -> La4
            long r2 = r2 + r4
            r7.f7113e = r2     // Catch: java.lang.Throwable -> L84 java.lang.Exception -> La4
            java.lang.String r9 = "CachedSource"
            java.lang.StringBuilder r2 = new java.lang.StringBuilder     // Catch: java.lang.Throwable -> L84 java.lang.Exception -> La4
            r2.<init>()     // Catch: java.lang.Throwable -> L84 java.lang.Exception -> La4
            java.lang.String r3 = "isFirstPart("
            r2.append(r3)     // Catch: java.lang.Throwable -> L84 java.lang.Exception -> La4
            r2.append(r8)     // Catch: java.lang.Throwable -> L84 java.lang.Exception -> La4
            java.lang.String r8 = "), restarting prefetcher, totalSize = "
            r2.append(r8)     // Catch: java.lang.Throwable -> L84 java.lang.Exception -> La4
            int r8 = z0.C0750a.b.a(r7)     // Catch: java.lang.Throwable -> L84 java.lang.Exception -> La4
            r2.append(r8)     // Catch: java.lang.Throwable -> L84 java.lang.Exception -> La4
            java.lang.String r8 = r2.toString()     // Catch: java.lang.Throwable -> L84 java.lang.Exception -> La4
            android.util.Log.d(r9, r8)     // Catch: java.lang.Throwable -> L84 java.lang.Exception -> La4
            goto L86
        L84:
            r7 = move-exception
            goto Lb4
        L86:
            long r8 = r6.d(r7)     // Catch: java.lang.Throwable -> L84 java.lang.Exception -> La4
            r2 = 20971520(0x1400000, double:1.03613076E-316)
            int r8 = (r8 > r2 ? 1 : (r8 == r2 ? 0 : -1))
            if (r8 >= 0) goto L96
            r7.f7115g = r1     // Catch: java.lang.Throwable -> L84 java.lang.Exception -> La4
            r7.f7116h = r0     // Catch: java.lang.Throwable -> L84 java.lang.Exception -> La4
            goto L9a
        L96:
            r7.f7115g = r0     // Catch: java.lang.Throwable -> L84 java.lang.Exception -> La4
            r7.f7116h = r1     // Catch: java.lang.Throwable -> L84 java.lang.Exception -> La4
        L9a:
            java.util.concurrent.locks.ReadWriteLock r7 = r6.f7099q     // Catch: java.lang.Throwable -> L23
            java.util.concurrent.locks.Lock r7 = r7.writeLock()     // Catch: java.lang.Throwable -> L23
        La0:
            r7.unlock()     // Catch: java.lang.Throwable -> L23
            goto Lb2
        La4:
            java.lang.String r7 = "CachedSource"
            java.lang.String r8 = "mPageCacheLock.writeLock() catch Exception"
            android.util.Log.d(r7, r8)     // Catch: java.lang.Throwable -> L84
            java.util.concurrent.locks.ReadWriteLock r7 = r6.f7099q     // Catch: java.lang.Throwable -> L23
            java.util.concurrent.locks.Lock r7 = r7.writeLock()     // Catch: java.lang.Throwable -> L23
            goto La0
        Lb2:
            monitor-exit(r6)
            return
        Lb4:
            java.util.concurrent.locks.ReadWriteLock r8 = r6.f7099q     // Catch: java.lang.Throwable -> L23
            java.util.concurrent.locks.Lock r8 = r8.writeLock()     // Catch: java.lang.Throwable -> L23
            r8.unlock()     // Catch: java.lang.Throwable -> L23
            throw r7     // Catch: java.lang.Throwable -> L23
        Lbe:
            monitor-exit(r6)     // Catch: java.lang.Throwable -> L23
            throw r7
        */
        throw new UnsupportedOperationException("Method not decompiled: z0.C0750a.m(z0.a$b, boolean, boolean):void");
    }

    private void n(long j2, b bVar) {
        bVar.f7114f = j2;
        if (j2 >= bVar.f7113e && j2 <= bVar.f7113e + ((long) bVar.f7110b)) {
            Log.d("CachedSource", "cache matched, needn't seek ");
            return;
        }
        Log.d("CachedSource", "seekCacheInternal seek cache. offset:" + j2);
        this.f7093k = j2;
        bVar.f7113e = j2;
        int i2 = bVar.f7110b;
        this.f7099q.writeLock().lock();
        try {
            try {
                Log.d("CachedSource", "seekCacheInternal, release: " + bVar.g(i2));
                if (bVar.f7117i == -1) {
                    Log.d("CachedSource", "finalStatus is STATUS_END_OF_STREAM, refetch after seek! sendEmptyMessageDelayed(MSG_FETCH_MORE) 007");
                    this.f7083a.sendEmptyMessageDelayed(1, 0L);
                    this.f7104v = true;
                }
                bVar.f7117i = 1;
                bVar.f7115g = true;
                bVar.f7116h = false;
            } catch (Exception unused) {
                Log.d("CachedSource", "mPageCacheLock.writeLock catch Exception");
            }
            this.f7099q.writeLock().unlock();
        } catch (Throwable th) {
            this.f7099q.writeLock().unlock();
            throw th;
        }
    }

    private void o(long j2) {
        int i2 = this.f7086d;
        if (13 == i2 || i2 == 0) {
            Log.e("CachedSource", "mSeekType not support skip");
            return;
        }
        if (2 == i2) {
            Log.e("CachedSource", "TIME_ONLY timeskip");
            b(j2);
            return;
        }
        e(this.f7093k);
        long j3 = this.f7094l;
        if (j3 == j2) {
            Log.d("CachedSource", "001 inputStreamLastReadAtByteOffset == offset " + this.f7094l);
            return;
        }
        if (j3 < j2) {
            f(j2 - j3);
            return;
        }
        Log.d("CachedSource", "seek backward ");
        if (this.f7085c instanceof Y) {
            l(j2);
        }
    }

    private void q(long j2) {
        this.f7089g.lock();
        this.f7090h.signal();
        this.f7089g.unlock();
        Log.d("CachedSource", " sendEmptyMessageDelayed(MSG_FETCH_MORE) 008");
        this.f7104v = true;
        this.f7083a.sendEmptyMessageDelayed(1, 0L);
    }

    public long d(b bVar) {
        return (bVar.f7113e + ((long) bVar.f7110b)) - bVar.f7114f;
    }

    @Override // android.os.Handler.Callback
    public boolean handleMessage(Message message) {
        int i2 = message.what;
        if (i2 == 1) {
            o.d("CachedSource", "+MSG_FETCH_MORE", 64);
            long jCurrentTimeMillis = System.currentTimeMillis();
            if (this.f7101s) {
                Log.d("CachedSource", "issue handle");
            } else {
                g();
            }
            o.d("CachedSource", "-MSG_FETCH_MORE spendTime(ms)=" + (System.currentTimeMillis() - jCurrentTimeMillis), 64);
            return true;
        }
        if (i2 != 2) {
            if (i2 == 3) {
                q(this.f7096n);
                return true;
            }
            if (i2 == 4) {
                a(this.f7095m);
                return true;
            }
            Log.e("CachedSource", "unknown message type (" + message.what + ")");
            return false;
        }
        o.d("CachedSource", "+MSG_READ_CACHE", 64);
        long jCurrentTimeMillis2 = System.currentTimeMillis();
        c cVar = (c) message.obj;
        try {
            this.f7093k = cVar.f7119a;
            h(cVar);
        } catch (IOException e2) {
            Log.e("CachedSource", "onReadCache, catch IOException, e: " + e2.getMessage());
        }
        o.d("CachedSource", "-MSG_READ_CACHE spendTime(ms)=" + (System.currentTimeMillis() - jCurrentTimeMillis2), 64);
        return true;
    }

    public void i() {
        if (this.f7084b != null) {
            Log.d("CachedSource", "quit");
            this.f7101s = true;
            this.f7089g.lock();
            if (this.f7085c != null) {
                try {
                    synchronized (this) {
                        Log.d("CachedSource", "removeMessages(MSG_FETCH_MORE) 004");
                        this.f7083a.removeMessages(1);
                        this.f7104v = false;
                    }
                    if (this.f7085c instanceof Y) {
                        Log.d("CachedSource", "No need to abortRead() for jcifs-1.3.19");
                    }
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
            this.f7100r = false;
            this.f7090h.signal();
            this.f7089g.unlock();
            this.f7091i.f();
            this.f7092j.f();
            this.f7084b.quit();
        }
    }

    public int j(long j2, byte[] bArr, int i2) {
        Lock lock;
        synchronized (this) {
            try {
                boolean zE = e(j2);
                b bVar = zE ? this.f7091i : this.f7092j;
                this.f7099q.readLock().lock();
                o.d("CachedSource", "VIP readAt, isFirstPart(" + zE + ") offset " + j2 + ", size " + i2 + ", fileSize = " + this.f7088f + ", (offset1 " + this.f7091i.f7113e + ", size1 = " + this.f7091i.f7110b + ", offset2 = " + this.f7092j.f7113e + ", size2 = " + this.f7092j.f7110b + ")", 64);
                try {
                    try {
                    } finally {
                        this.f7099q.readLock().unlock();
                    }
                } catch (Exception unused) {
                    Log.d("CachedSource", "mPageCacheLock.readLock() catch Exception");
                    lock = this.f7099q.readLock();
                }
                if (j2 >= this.f7088f) {
                    Log.d("CachedSource", "offset >= fileSize return -1");
                    return -1;
                }
                if (j2 >= bVar.f7113e) {
                    long j3 = ((long) i2) + j2;
                    if (j3 <= bVar.f7113e + ((long) bVar.f7110b)) {
                        bVar.e((int) (j2 - bVar.f7113e), bArr, i2);
                        bVar.f7114f = j3;
                        this.f7093k = bVar.f7114f;
                        return i2;
                    }
                }
                if (j2 >= bVar.f7113e && bVar.f7117i == -1) {
                    int i3 = (int) (j2 - bVar.f7113e);
                    int i4 = bVar.f7110b - i3;
                    Log.d("CachedSource", "readAt, EOS, remainSize = " + i4);
                    if (i4 > 0) {
                        bVar.e(i3, bArr, i4);
                        bVar.f7114f += (long) i2;
                        this.f7093k = bVar.f7114f;
                    }
                    return i4;
                }
                if (bVar.f7117i == -2) {
                    Log.d("CachedSource", "finalStatus == STATUS_DMS_BUSY return -1");
                    return -1;
                }
                lock = this.f7099q.readLock();
                lock.unlock();
                c cVar = new c();
                cVar.f7119a = j2;
                cVar.f7120b = bArr;
                cVar.f7121c = i2;
                cVar.f7122d = bVar;
                cVar.f7124f = true;
                cVar.f7123e = new Object();
                o.d("CachedSource", "cache is not enough, send  read msg immediately", 64);
                this.f7083a.sendMessageAtFrontOfQueue(this.f7083a.obtainMessage(2, cVar));
                this.f7100r = true;
                while (this.f7100r) {
                    try {
                        try {
                            this.f7089g.lock();
                            if (!this.f7102t) {
                                this.f7090h.await();
                            }
                            synchronized (cVar.f7123e) {
                                try {
                                    if (this.f7100r) {
                                        this.f7100r = cVar.f7124f;
                                    }
                                } finally {
                                }
                            }
                        } catch (InterruptedException e2) {
                            Log.e("CachedSource", "readAt, catch InterruptedException, e:" + e2.getMessage());
                            synchronized (cVar.f7123e) {
                                try {
                                    if (this.f7100r) {
                                        this.f7100r = cVar.f7124f;
                                    }
                                } finally {
                                }
                            }
                        }
                        this.f7102t = false;
                        this.f7089g.unlock();
                    } catch (Throwable th) {
                        synchronized (cVar.f7123e) {
                            try {
                                if (this.f7100r) {
                                    this.f7100r = cVar.f7124f;
                                }
                                this.f7102t = false;
                                this.f7089g.unlock();
                                throw th;
                            } finally {
                            }
                        }
                    }
                }
                int i5 = bVar.f7118j;
                o.d("CachedSource", "read success, size = " + i5, 64);
                bVar.f7118j = -1000;
                if (i5 > 0) {
                    bVar.f7114f = j2 + ((long) i5);
                    this.f7093k = bVar.f7114f;
                }
                return i5;
            } catch (Throwable th2) {
                throw th2;
            }
        }
    }

    public void p() {
        if (this.f7097o) {
            return;
        }
        this.f7097o = true;
        Log.d("CachedSource", "startLoading");
        Log.d("CachedSource", "sendEmptyMessage(MSG_FETCH_MORE) 001");
        this.f7083a.sendEmptyMessage(1);
        this.f7104v = true;
    }
}
