package defpackage;

import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.AssetFileDescriptor;
import android.content.res.Resources;
import android.net.Uri;
import android.text.TextUtils;
import androidx.media3.datasource.DataSourceException;
import androidx.media3.datasource.RawResourceDataSource$RawResourceDataSourceException;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.List;

/* JADX INFO: renamed from: ᵢᵴˁʾ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes3.dex */
public final class C3309 extends AbstractC0824 {

    /* JADX INFO: renamed from: ˈᵲᵢ, reason: contains not printable characters */
    public AssetFileDescriptor f12732;

    /* JADX INFO: renamed from: ˌᵨʠᵫ, reason: contains not printable characters */
    public FileInputStream f12733;

    /* JADX INFO: renamed from: ˑˊᵲ, reason: contains not printable characters */
    public boolean f12734;

    /* JADX INFO: renamed from: ᵯᵷʽ, reason: contains not printable characters */
    public final Context f12735;

    /* JADX INFO: renamed from: ᵶˇᵱ, reason: contains not printable characters */
    public C0084 f12736;

    /* JADX INFO: renamed from: ᵸˆˁ, reason: contains not printable characters */
    public long f12737;

    public C3309(Context context) {
        super(false);
        this.f12735 = context.getApplicationContext();
    }

    @Deprecated
    public static Uri buildRawResourceUri(int i) {
        return Uri.parse("rawresource:///" + i);
    }

    @Override // defpackage.InterfaceC1070
    public final void close() {
        this.f12736 = null;
        try {
            try {
                FileInputStream fileInputStream = this.f12733;
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
                this.f12733 = null;
                try {
                    try {
                        AssetFileDescriptor assetFileDescriptor = this.f12732;
                        if (assetFileDescriptor != null) {
                            assetFileDescriptor.close();
                        }
                    } catch (IOException e) {
                        throw new RawResourceDataSource$RawResourceDataSourceException(null, e, 2000);
                    }
                } finally {
                    this.f12732 = null;
                    if (this.f12734) {
                        this.f12734 = false;
                        m2945();
                    }
                }
            } catch (IOException e2) {
                throw new RawResourceDataSource$RawResourceDataSourceException(null, e2, 2000);
            }
        } catch (Throwable th) {
            this.f12733 = null;
            try {
                try {
                    AssetFileDescriptor assetFileDescriptor2 = this.f12732;
                    if (assetFileDescriptor2 != null) {
                        assetFileDescriptor2.close();
                    }
                    this.f12732 = null;
                    if (this.f12734) {
                        this.f12734 = false;
                        m2945();
                    }
                    throw th;
                } catch (IOException e3) {
                    throw new RawResourceDataSource$RawResourceDataSourceException(null, e3, 2000);
                }
            } finally {
                this.f12732 = null;
                if (this.f12734) {
                    this.f12734 = false;
                    m2945();
                }
            }
        }
    }

    @Override // defpackage.InterfaceC0372
    public final int read(byte[] bArr, int i, int i2) throws RawResourceDataSource$RawResourceDataSourceException {
        if (i2 == 0) {
            return 0;
        }
        long j = this.f12737;
        if (j != 0) {
            if (j != -1) {
                try {
                    i2 = (int) Math.min(j, i2);
                } catch (IOException e) {
                    throw new RawResourceDataSource$RawResourceDataSourceException(null, e, 2000);
                }
            }
            FileInputStream fileInputStream = this.f12733;
            String str = AbstractC0956.f4222;
            int i3 = fileInputStream.read(bArr, i, i2);
            long j2 = this.f12737;
            if (i3 != -1) {
                if (j2 != -1) {
                    this.f12737 = j2 - ((long) i3);
                }
                m2946(i3);
                return i3;
            }
            if (j2 != -1) {
                throw new RawResourceDataSource$RawResourceDataSourceException("End of stream reached having not read sufficient data.", new EOFException(), 2000);
            }
        }
        return -1;
    }

    @Override // defpackage.InterfaceC1070
    /* JADX INFO: renamed from: ᵯᵷʽ */
    public final Uri mo1307() {
        C0084 c0084 = this.f12736;
        if (c0084 != null) {
            return c0084.f1291;
        }
        return null;
    }

    @Override // defpackage.InterfaceC1070
    /* JADX INFO: renamed from: ᵵᵽ */
    public final long mo1308(C0084 c0084) throws RawResourceDataSource$RawResourceDataSourceException {
        Resources resourcesForApplication;
        int identifier;
        int i;
        Resources resources;
        this.f12736 = c0084;
        m2948();
        Uri uri = c0084.f1291;
        long j = c0084.f1294;
        long j2 = c0084.f1290;
        Uri uriNormalizeScheme = uri.normalizeScheme();
        boolean zEquals = TextUtils.equals("rawresource", uriNormalizeScheme.getScheme());
        Context context = this.f12735;
        if (zEquals) {
            resources = context.getResources();
            List<String> pathSegments = uriNormalizeScheme.getPathSegments();
            if (pathSegments.size() != 1) {
                throw new RawResourceDataSource$RawResourceDataSourceException("rawresource:// URI must have exactly one path element, found " + pathSegments.size(), null, 2000);
            }
            try {
                i = Integer.parseInt(pathSegments.get(0));
            } catch (NumberFormatException unused) {
                throw new RawResourceDataSource$RawResourceDataSourceException("Resource identifier must be an integer.", null, 1004);
            }
        } else {
            if (!TextUtils.equals("android.resource", uriNormalizeScheme.getScheme())) {
                throw new RawResourceDataSource$RawResourceDataSourceException("Unsupported URI scheme (" + uriNormalizeScheme.getScheme() + "). Only android.resource is supported.", null, 1004);
            }
            String path = uriNormalizeScheme.getPath();
            path.getClass();
            if (path.startsWith("/")) {
                path = path.substring(1);
            }
            String packageName = TextUtils.isEmpty(uriNormalizeScheme.getHost()) ? context.getPackageName() : uriNormalizeScheme.getHost();
            if (packageName.equals(context.getPackageName())) {
                resourcesForApplication = context.getResources();
            } else {
                try {
                    resourcesForApplication = context.getPackageManager().getResourcesForApplication(packageName);
                } catch (PackageManager.NameNotFoundException e) {
                    throw new RawResourceDataSource$RawResourceDataSourceException("Package in android.resource:// URI not found. Check http://g.co/dev/packagevisibility.", e, 2005);
                }
            }
            if (path.matches("\\d+")) {
                try {
                    identifier = Integer.parseInt(path);
                } catch (NumberFormatException unused2) {
                    throw new RawResourceDataSource$RawResourceDataSourceException("Resource identifier must be an integer.", null, 1004);
                }
            } else {
                identifier = resourcesForApplication.getIdentifier(AbstractC3653.m8223(packageName, ":", path), "raw", null);
                if (identifier == 0) {
                    throw new RawResourceDataSource$RawResourceDataSourceException("Resource not found.", null, 2005);
                }
            }
            i = identifier;
            resources = resourcesForApplication;
        }
        try {
            AssetFileDescriptor assetFileDescriptorOpenRawResourceFd = resources.openRawResourceFd(i);
            if (assetFileDescriptorOpenRawResourceFd == null) {
                throw new RawResourceDataSource$RawResourceDataSourceException("Resource is compressed: " + uriNormalizeScheme, null, 2000);
            }
            this.f12732 = assetFileDescriptorOpenRawResourceFd;
            long length = assetFileDescriptorOpenRawResourceFd.getLength();
            FileInputStream fileInputStream = new FileInputStream(this.f12732.getFileDescriptor());
            this.f12733 = fileInputStream;
            try {
                if (length != -1 && j2 > length) {
                    throw new RawResourceDataSource$RawResourceDataSourceException(null, null, 2008);
                }
                long startOffset = this.f12732.getStartOffset();
                long jSkip = fileInputStream.skip(startOffset + j2) - startOffset;
                if (jSkip != j2) {
                    throw new RawResourceDataSource$RawResourceDataSourceException(null, null, 2008);
                }
                if (length == -1) {
                    FileChannel channel = fileInputStream.getChannel();
                    if (channel.size() == 0) {
                        this.f12737 = -1L;
                    } else {
                        long size = channel.size() - channel.position();
                        this.f12737 = size;
                        if (size < 0) {
                            throw new RawResourceDataSource$RawResourceDataSourceException(null, null, 2008);
                        }
                    }
                } else {
                    long j3 = length - jSkip;
                    this.f12737 = j3;
                    if (j3 < 0) {
                        throw new DataSourceException(2008);
                    }
                }
                if (j != -1) {
                    long j4 = this.f12737;
                    this.f12737 = j4 == -1 ? j : Math.min(j4, j);
                }
                this.f12734 = true;
                m2947(c0084);
                return j != -1 ? j : this.f12737;
            } catch (RawResourceDataSource$RawResourceDataSourceException e2) {
                throw e2;
            } catch (IOException e3) {
                throw new RawResourceDataSource$RawResourceDataSourceException(null, e3, 2000);
            }
        } catch (Resources.NotFoundException e4) {
            throw new RawResourceDataSource$RawResourceDataSourceException(null, e4, 2005);
        }
    }
}
