package a0;

import android.net.Uri;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.util.Log;
import b0.D;
import java.io.IOException;

/* JADX INFO: loaded from: classes.dex */
public class u implements Handler.Callback {

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

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

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private v f2029d;

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

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

    public u(Uri uri, f fVar, int i2) {
        Log.d("ReadbackableDataCache", "ReadbackableDataCache~");
        this.f2026a = uri;
        this.f2028c = fVar;
        this.f2029d = new v(i2);
        b0.x xVar = new b0.x("ReadbackableDataCache:Handler", -16);
        this.f2031f = xVar;
        xVar.start();
        this.f2030e = new Handler(xVar.getLooper(), this);
    }

    private synchronized int a(long j2, int i2) {
        long j3 = 0;
        try {
            if (j2 > 0) {
                b0.o.c("ReadbackableDataCache", 64, "+fetchInternal position=", Long.valueOf(j2), " size=", Integer.valueOf(i2));
            } else {
                b0.o.c("ReadbackableDataCache", 64, "+fetchInternal:");
            }
            if (j2 >= 0 && this.f2029d.f(j2, i2)) {
                g(j2);
            }
            if (this.f2029d.c() && j2 == -1 && i2 == -1) {
                f(50L);
                b0.o.c("ReadbackableDataCache", 64, "dataBuffer.isFull()");
                return -1;
            }
            byte[] bArr = new byte[32768];
            int i3 = 0;
            while (true) {
                int i4 = this.f2028c.read(bArr, 0, 32768);
                if (i4 <= 0) {
                    j3 = 100;
                    break;
                }
                this.f2029d.g(bArr, i4);
                i3 += i4;
                if (i3 >= 32768) {
                    break;
                }
            }
            b0.o.c("ReadbackableDataCache", 64, "-fetchInternal fetchlength=", Integer.valueOf(i3));
            if (this.f2030e.getLooper().getThread().isAlive()) {
                f(j3);
                return i3 > 0 ? i3 : -1;
            }
            b0.o.d("ReadbackableDataCache", "got a dead thread", 64);
            throw new IOException();
        } catch (Throwable th) {
            throw th;
        }
    }

    private void f(long j2) {
        if (j2 > 0) {
            this.f2030e.sendEmptyMessageDelayed(1, j2);
        } else {
            this.f2030e.sendEmptyMessage(1);
        }
    }

    private void g(long j2) {
        Log.d("ReadbackableDataCache", "seekDatasource() position=" + j2);
        D.g(this.f2028c);
        long jA = this.f2028c.a(new h(this.f2026a, j2, -1L, null));
        if (j2 == 0) {
            this.f2027b = jA;
            Log.d("ReadbackableDataCache", "contentLength=" + this.f2027b);
        }
    }

    public long b() {
        return this.f2027b;
    }

    public void c() {
        Log.d("ReadbackableDataCache", "initDatasource()");
        g(0L);
        this.f2030e.sendEmptyMessage(1);
    }

    public int d(long j2, byte[] bArr, int i2, int i3) {
        int iD;
        b0.o.c("ReadbackableDataCache", 64, "+readAt position=", Long.valueOf(j2), " size=", Integer.valueOf(i3));
        int i4 = 0;
        int iA = 0;
        while (true) {
            iD = this.f2029d.d(((long) i4) + j2, bArr, i2 + i4, i3 - i4) + i4;
            if (iD >= i3) {
                break;
            }
            this.f2030e.removeMessages(1);
            try {
                iA = a(((long) iD) + j2, i3 - iD);
            } catch (Exception e2) {
                Log.e("ReadbackableDataCache", "Fetch error: ", e2);
                this.f2030e.removeMessages(1);
                iA = -1;
            }
            if (iA <= 0) {
                break;
            }
            i4 = iD;
        }
        if (iA < 0) {
            int iD2 = this.f2029d.d(((long) iD) + j2, bArr, i2 + iD, i3 - iD);
            iD += iD2;
            Log.i("ReadbackableDataCache", "Reach EOS, try to read from dataBuffer length:" + iD2 + ", totalSize:" + iD + ", result:" + iA);
        }
        b0.o.c("ReadbackableDataCache", 64, "-readAt totalSize=", Integer.valueOf(iD));
        if (iD > 0) {
            return iD;
        }
        return -1;
    }

    public void e() {
        HandlerThread handlerThread = this.f2031f;
        if (handlerThread != null) {
            handlerThread.quit();
        }
        D.g(this.f2028c);
    }

    protected void finalize() throws Throwable {
        try {
            e();
        } finally {
            super.finalize();
        }
    }

    @Override // android.os.Handler.Callback
    public boolean handleMessage(Message message) {
        try {
            if (message.what != 1) {
                return false;
            }
            a(-1L, -1);
            return true;
        } catch (IOException e2) {
            Log.e("ReadbackableDataCache", "Fetch internal error: ", e2);
            return true;
        } catch (RuntimeException e3) {
            Log.e("ReadbackableDataCache", "Fetch runtime error: ", e3);
            return true;
        }
    }
}
