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

import android.net.Uri;
import android.system.ErrnoException;
import android.system.OsConstants;
import android.text.TextUtils;
import androidx.annotation.Nullable;
import androidx.media3.common.PlaybackException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

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

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

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

    @Nullable
    private RandomAccessFile zza;

    @Nullable
    private Uri zzb;

    @Override // com.google.android.gms.internal.ads.sv1
    public final long b(dy1 dy1Var) throws zzhn {
        Uri uri = dy1Var.f10564a;
        this.zzb = uri;
        l(dy1Var);
        int i10 = PlaybackException.ERROR_CODE_IO_NO_PERMISSION;
        try {
            String path = uri.getPath();
            path.getClass();
            RandomAccessFile randomAccessFile = new RandomAccessFile(path, "r");
            this.zza = randomAccessFile;
            try {
                long j10 = dy1Var.f10566c;
                randomAccessFile.seek(j10);
                long length = dy1Var.f10567d;
                if (length == -1) {
                    length = this.zza.length() - j10;
                }
                this.f18304d = length;
                if (length < 0) {
                    throw new zzhn(null, null, 2008);
                }
                this.f18305e = true;
                m(dy1Var);
                return this.f18304d;
            } catch (IOException e10) {
                throw new zzhn(e10, 2000);
            }
        } catch (FileNotFoundException e11) {
            if (!TextUtils.isEmpty(uri.getQuery()) || !TextUtils.isEmpty(uri.getFragment())) {
                throw new zzhn(String.format("uri has query and/or fragment, which are not supported. Did you call Uri.parse() on a string containing '?' or '#'? Use Uri.fromFile(new File(path)) to avoid this. path=%s,query=%s,fragment=%s", uri.getPath(), uri.getQuery(), uri.getFragment()), e11, 1004);
            }
            if (!(e11.getCause() instanceof ErrnoException) || ((ErrnoException) e11.getCause()).errno != OsConstants.EACCES) {
                i10 = 2005;
            }
            throw new zzhn(e11, i10);
        } catch (SecurityException e12) {
            throw new zzhn(e12, PlaybackException.ERROR_CODE_IO_NO_PERMISSION);
        } catch (RuntimeException e13) {
            throw new zzhn(e13, 2000);
        }
    }

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

    @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 {
                RandomAccessFile randomAccessFile = this.zza;
                if (randomAccessFile != null) {
                    randomAccessFile.close();
                }
                this.zza = null;
                if (this.f18305e) {
                    this.f18305e = false;
                    o();
                }
            } catch (IOException e10) {
                throw new zzhn(e10, 2000);
            }
        } catch (Throwable th) {
            this.zza = null;
            if (this.f18305e) {
                this.f18305e = false;
                o();
            }
            throw th;
        }
    }
}
