package z0;

import android.net.Uri;
import android.system.ErrnoException;
import android.system.OsConstants;
import android.text.TextUtils;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

/* JADX INFO: loaded from: classes.dex */
public final class p extends AbstractC1779c {

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public RandomAccessFile f19167u;

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

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

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

    @Override // z0.h
    public final void close() {
        this.f19168v = null;
        try {
            try {
                RandomAccessFile randomAccessFile = this.f19167u;
                if (randomAccessFile != null) {
                    randomAccessFile.close();
                }
            } catch (IOException e8) {
                throw new o(e8, 2000);
            }
        } finally {
            this.f19167u = null;
            if (this.f19170x) {
                this.f19170x = false;
                j();
            }
        }
    }

    @Override // z0.h
    public final Uri e() {
        return this.f19168v;
    }

    @Override // z0.h
    public final long g(k kVar) throws o {
        Uri uri = kVar.f19138a;
        long j8 = kVar.f19142e;
        this.f19168v = uri;
        l();
        try {
            String path = uri.getPath();
            path.getClass();
            RandomAccessFile randomAccessFile = new RandomAccessFile(path, "r");
            this.f19167u = randomAccessFile;
            try {
                randomAccessFile.seek(j8);
                long length = kVar.f19143f;
                if (length == -1) {
                    length = this.f19167u.length() - j8;
                }
                this.f19169w = length;
                if (length < 0) {
                    throw new o(null, null, 2008);
                }
                this.f19170x = true;
                r(kVar);
                return this.f19169w;
            } catch (IOException e8) {
                throw new o(e8, 2000);
            }
        } catch (FileNotFoundException e9) {
            if (TextUtils.isEmpty(uri.getQuery()) && TextUtils.isEmpty(uri.getFragment())) {
                throw new o(e9, ((e9.getCause() instanceof ErrnoException) && ((ErrnoException) e9.getCause()).errno == OsConstants.EACCES) ? 2006 : 2005);
            }
            throw new o("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=" + uri.getPath() + ",query=" + uri.getQuery() + ",fragment=" + uri.getFragment(), e9, 1004);
        } catch (SecurityException e10) {
            throw new o(e10, 2006);
        } catch (RuntimeException e11) {
            throw new o(e11, 2000);
        }
    }

    @Override // u0.InterfaceC1571g
    public final int p(byte[] bArr, int i, int i5) throws o {
        if (i5 == 0) {
            return 0;
        }
        long j8 = this.f19169w;
        if (j8 == 0) {
            return -1;
        }
        try {
            RandomAccessFile randomAccessFile = this.f19167u;
            int i8 = x0.v.f18553a;
            int i9 = randomAccessFile.read(bArr, i, (int) Math.min(j8, i5));
            if (i9 > 0) {
                this.f19169w -= (long) i9;
                h(i9);
            }
            return i9;
        } catch (IOException e8) {
            throw new o(e8, 2000);
        }
    }
}
