package d6;

import android.content.Context;
import android.media.MediaDataSource;
import android.text.TextUtils;
import com.bykv.vk.openvk.fm.fm.fm.lb.lb;
import e6.b;
import java.io.File;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.concurrent.ConcurrentHashMap;

/* JADX INFO: loaded from: classes.dex */
public final class a extends MediaDataSource {

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final ConcurrentHashMap f25000e = new ConcurrentHashMap();

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final b f25001a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public long f25002b = -2147483648L;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final Context f25003c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final lb f25004d;

    public a(Context context, lb lbVar) {
        this.f25003c = context;
        this.f25004d = lbVar;
        this.f25001a = new b(lbVar);
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public final void close() {
        this.f25004d.irt();
        b bVar = this.f25001a;
        if (bVar != null) {
            try {
                if (!bVar.f25502h) {
                    bVar.f25504j.close();
                }
                File file = bVar.f25498d;
                if (file != null) {
                    file.setLastModified(System.currentTimeMillis());
                }
                File file2 = bVar.f25499e;
                if (file2 != null) {
                    file2.setLastModified(System.currentTimeMillis());
                }
            } catch (Throwable unused) {
            }
            bVar.f25502h = true;
        }
        f25000e.remove(this.f25004d.qhl());
    }

    @Override // android.media.MediaDataSource
    public final long getSize() {
        if (this.f25002b == -2147483648L) {
            long j10 = -1;
            if (this.f25003c == null || TextUtils.isEmpty(this.f25004d.irt())) {
                return -1L;
            }
            b bVar = this.f25001a;
            if (bVar.f25499e.exists()) {
                bVar.f25496b = bVar.f25499e.length();
            } else {
                synchronized (bVar.f25497c) {
                    int i10 = 0;
                    while (bVar.f25496b == -2147483648L && !bVar.f25502h) {
                        try {
                            i10 += 15;
                            try {
                                bVar.f25497c.wait(5L);
                                if (i10 > 20000) {
                                    break;
                                }
                            } catch (InterruptedException unused) {
                                throw new IOException("total length InterruptException");
                            }
                        } catch (Throwable th) {
                            throw th;
                        }
                    }
                }
                this.f25002b = j10;
            }
            j10 = bVar.f25496b;
            this.f25002b = j10;
        }
        return this.f25002b;
    }

    @Override // android.media.MediaDataSource
    public final int readAt(long j10, byte[] bArr, int i10, int i11) throws IOException {
        b bVar = this.f25001a;
        bVar.getClass();
        try {
            int i12 = -1;
            if (j10 != bVar.f25496b) {
                int i13 = 0;
                int i14 = 0;
                do {
                    if (!bVar.f25502h) {
                        synchronized (bVar.f25497c) {
                            try {
                                File file = bVar.f25499e;
                                if (j10 < (file.exists() ? file.length() : bVar.f25498d.length())) {
                                    bVar.f25504j.seek(j10);
                                    i14 = bVar.f25504j.read(bArr, i10, i11);
                                } else {
                                    i13 += 33;
                                    bVar.f25506l = j10;
                                    bVar.f25497c.wait(33L);
                                    bVar.f25506l = -1L;
                                }
                            } finally {
                            }
                        }
                        if (i14 > 0) {
                            i12 = i14;
                        } else {
                            lb lbVar = bVar.f25505k;
                            if (lbVar != null && lbVar.fm() && bVar.f25501g != -100 && (!bVar.f25503i || bVar.f25496b == -1)) {
                                throw new IOException();
                            }
                        }
                    }
                } while (i13 < 20000);
                throw new SocketTimeoutException();
            }
            int length = bArr.length;
            return i12;
        } catch (Throwable th) {
            if (th instanceof IOException) {
                throw th;
            }
            throw new IOException();
        }
    }
}
