package com.google.android.gms.internal.ads;

import android.content.Context;
import android.net.Uri;
import android.os.SystemClock;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicLong;

/* JADX INFO: loaded from: classes2.dex */
public final class j40 implements sv1 {

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

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

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

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final boolean f12698e;

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

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public Uri f12701h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public volatile yk f12702i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public boolean f12703j = false;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public boolean f12704k = false;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public dy1 f12705l;

    public j40(Context context, z12 z12Var, String str, int i10) {
        this.f12694a = context;
        this.f12695b = z12Var;
        this.f12696c = str;
        this.f12697d = i10;
        new AtomicLong(-1L);
        this.f12698e = ((Boolean) n7.y.f31093e.f31096c.b(ao.f8979n2)).booleanValue();
    }

    @Override // com.google.android.gms.internal.ads.sv1
    public final long b(dy1 dy1Var) throws IOException {
        boolean z10;
        boolean z11;
        if (this.f12700g) {
            throw new IOException("Attempt to open an already open CacheDataSource.");
        }
        this.f12700g = true;
        Uri uri = dy1Var.f10564a;
        this.f12701h = uri;
        this.f12705l = dy1Var;
        this.f12702i = yk.zza(uri);
        tn tnVar = ao.f8767a5;
        n7.y yVar = n7.y.f31093e;
        vk vkVarB = null;
        if (!((Boolean) yVar.f31096c.b(tnVar)).booleanValue()) {
            if (this.f12702i != null) {
                this.f12702i.f18864g = dy1Var.f10566c;
                yk ykVar = this.f12702i;
                String str = this.f12696c;
                ykVar.f18865h = str != null ? str : "";
                this.f12702i.f18866i = this.f12697d;
                vkVarB = m7.n.C.f30471j.b(this.f12702i);
            }
            if (vkVarB != null && vkVarB.w()) {
                synchronized (vkVarB) {
                    z10 = vkVarB.f17669d;
                }
                this.f12703j = z10;
                synchronized (vkVarB) {
                    z11 = vkVarB.f17667b;
                }
                this.f12704k = z11;
                if (!l()) {
                    this.f12699f = vkVarB.zzb();
                    return -1L;
                }
            }
        } else if (this.f12702i != null) {
            this.f12702i.f18864g = dy1Var.f10566c;
            yk ykVar2 = this.f12702i;
            String str2 = this.f12696c;
            ykVar2.f18865h = str2 != null ? str2 : "";
            this.f12702i.f18866i = this.f12697d;
            long jLongValue = (this.f12702i.f18863f ? (Long) yVar.f31096c.b(ao.f8801c5) : (Long) yVar.f31096c.b(ao.f8784b5)).longValue();
            m7.n.C.f30472k.getClass();
            SystemClock.elapsedRealtime();
            al alVarA = new el(this.f12694a).a(this.f12702i);
            try {
                try {
                    try {
                        gl glVar = (gl) alVarA.f10610a.get(jLongValue, TimeUnit.MILLISECONDS);
                        glVar.getClass();
                        this.f12703j = glVar.f11782c;
                        this.f12704k = glVar.f11784e;
                        if (!l()) {
                            this.f12699f = glVar.f11780a;
                        }
                    } catch (ExecutionException | TimeoutException unused) {
                        alVarA.cancel(false);
                    }
                } catch (InterruptedException unused2) {
                    alVarA.cancel(false);
                    Thread.currentThread().interrupt();
                }
            } catch (Throwable unused3) {
            }
            m7.n.C.f30472k.getClass();
            SystemClock.elapsedRealtime();
            throw null;
        }
        if (this.f12702i != null) {
            ox1 ox1Var = new ox1(dy1Var);
            ox1Var.a(Uri.parse(this.f12702i.zza));
            this.f12705l = ox1Var.b();
        }
        return this.f12695b.b(this.f12705l);
    }

    @Override // com.google.android.gms.internal.ads.eh2
    public final int d(byte[] bArr, int i10, int i11) throws IOException {
        if (!this.f12700g) {
            throw new IOException("Attempt to read closed CacheDataSource.");
        }
        InputStream inputStream = this.f12699f;
        return inputStream != null ? inputStream.read(bArr, i10, i11) : this.f12695b.d(bArr, i10, i11);
    }

    @Override // com.google.android.gms.internal.ads.sv1
    public final void g(u92 u92Var) {
    }

    public final boolean l() {
        if (!this.f12698e) {
            return false;
        }
        tn tnVar = ao.f8817d5;
        n7.y yVar = n7.y.f31093e;
        if (!((Boolean) yVar.f31096c.b(tnVar)).booleanValue() || this.f12703j) {
            return ((Boolean) yVar.f31096c.b(ao.f8834e5)).booleanValue() && !this.f12704k;
        }
        return true;
    }

    @Override // com.google.android.gms.internal.ads.sv1, com.google.android.gms.internal.ads.n72
    public final Uri zzc() {
        return this.f12701h;
    }

    @Override // com.google.android.gms.internal.ads.sv1
    public final void zzd() throws IOException {
        if (!this.f12700g) {
            throw new IOException("Attempt to close an already closed CacheDataSource.");
        }
        this.f12700g = false;
        this.f12701h = null;
        InputStream inputStream = this.f12699f;
        if (inputStream == null) {
            this.f12695b.zzd();
        } else {
            hb.a.d(inputStream);
            this.f12699f = null;
        }
    }
}
