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

import android.content.Context;
import android.content.res.AssetManager;
import android.net.Uri;
import androidx.annotation.Nullable;
import androidx.media3.common.PlaybackException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/* JADX INFO: loaded from: classes2.dex */
public final class xq1 extends qr1 {

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

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

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

    @Nullable
    private Uri zzb;

    @Nullable
    private InputStream zzc;

    public xq1(Context context) {
        super(false);
        this.f18518d = context.getAssets();
    }

    @Override // com.google.android.gms.internal.ads.sv1
    public final long b(dy1 dy1Var) throws zzgr {
        try {
            Uri uri = dy1Var.f10564a;
            this.zzb = uri;
            String path = uri.getPath();
            path.getClass();
            if (path.startsWith("/android_asset/")) {
                path = path.substring(15);
            } else if (path.startsWith("/")) {
                path = path.substring(1);
            }
            l(dy1Var);
            InputStream inputStreamOpen = this.f18518d.open(path, 1);
            this.zzc = inputStreamOpen;
            long j10 = dy1Var.f10566c;
            if (inputStreamOpen.skip(j10) < j10) {
                throw new zzgr(null, 2008);
            }
            long j11 = dy1Var.f10567d;
            if (j11 != -1) {
                this.f18519e = j11;
            } else {
                long jAvailable = this.zzc.available();
                this.f18519e = jAvailable;
                if (jAvailable == 2147483647L) {
                    this.f18519e = -1L;
                }
            }
            this.f18520f = true;
            m(dy1Var);
            return this.f18519e;
        } catch (zzgr e10) {
            throw e10;
        } catch (IOException e11) {
            throw new zzgr(e11, true != (e11 instanceof FileNotFoundException) ? 2000 : PlaybackException.ERROR_CODE_IO_FILE_NOT_FOUND);
        }
    }

    @Override // com.google.android.gms.internal.ads.eh2
    public final int d(byte[] bArr, int i10, int i11) throws zzgr {
        if (i11 == 0) {
            return 0;
        }
        long j10 = this.f18519e;
        if (j10 == 0) {
            return -1;
        }
        if (j10 != -1) {
            try {
                i11 = (int) Math.min(j10, i11);
            } catch (IOException e10) {
                throw new zzgr(e10, 2000);
            }
        }
        InputStream inputStream = this.zzc;
        String str = bb1.f9474a;
        int i12 = inputStream.read(bArr, i10, i11);
        if (i12 == -1) {
            return -1;
        }
        long j11 = this.f18519e;
        if (j11 != -1) {
            this.f18519e = j11 - ((long) i12);
        }
        n(i12);
        return i12;
    }

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

    @Override // com.google.android.gms.internal.ads.sv1
    public final void zzd() {
        this.zzb = null;
        try {
            try {
                InputStream inputStream = this.zzc;
                if (inputStream != null) {
                    inputStream.close();
                }
                this.zzc = null;
                if (this.f18520f) {
                    this.f18520f = false;
                    o();
                }
            } catch (IOException e10) {
                throw new zzgr(e10, 2000);
            }
        } catch (Throwable th) {
            this.zzc = null;
            if (this.f18520f) {
                this.f18520f = false;
                o();
            }
            throw th;
        }
    }
}
