package com.google.android.exoplayer2.upstream;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.Resources;
import android.net.Uri;
import android.text.TextUtils;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import p000.p001.p002.p003.p004.C0567;
import p138.p176.p177.p190.p238.AbstractC4092;
import p138.p176.p177.p190.p238.C4098;
import p138.p176.p177.p190.p239.C4150;

/* JADX INFO: loaded from: classes.dex */
public final class RawResourceDataSource extends AbstractC4092 {

    /* JADX INFO: renamed from: ނ, reason: contains not printable characters */
    public final Resources f1998;

    /* JADX INFO: renamed from: ރ, reason: contains not printable characters */
    public Uri f1999;

    /* JADX INFO: renamed from: ބ, reason: contains not printable characters */
    public AssetFileDescriptor f2000;

    /* JADX INFO: renamed from: ޅ, reason: contains not printable characters */
    public InputStream f2001;

    /* JADX INFO: renamed from: ކ, reason: contains not printable characters */
    public long f2002;

    /* JADX INFO: renamed from: އ, reason: contains not printable characters */
    public boolean f2003;

    public static class RawResourceDataSourceException extends IOException {
        public RawResourceDataSourceException(IOException iOException) {
            super(iOException);
        }

        public RawResourceDataSourceException(String str) {
            super(str);
        }
    }

    public RawResourceDataSource(Context context) {
        super(false);
        this.f1998 = context.getResources();
    }

    @Override // p138.p176.p177.p190.p238.InterfaceC4095
    public void close() {
        this.f1999 = null;
        try {
            try {
                if (this.f2001 != null) {
                    this.f2001.close();
                }
                this.f2001 = null;
                try {
                    try {
                        if (this.f2000 != null) {
                            this.f2000.close();
                        }
                    } catch (IOException e) {
                        throw new RawResourceDataSourceException(e);
                    }
                } finally {
                    this.f2000 = null;
                    if (this.f2003) {
                        this.f2003 = false;
                        m6581();
                    }
                }
            } catch (IOException e2) {
                throw new RawResourceDataSourceException(e2);
            }
        } catch (Throwable th) {
            this.f2001 = null;
            try {
                try {
                    if (this.f2000 != null) {
                        this.f2000.close();
                    }
                    this.f2000 = null;
                    if (this.f2003) {
                        this.f2003 = false;
                        m6581();
                    }
                    throw th;
                } catch (IOException e3) {
                    throw new RawResourceDataSourceException(e3);
                }
            } finally {
                this.f2000 = null;
                if (this.f2003) {
                    this.f2003 = false;
                    m6581();
                }
            }
        }
    }

    @Override // p138.p176.p177.p190.p238.InterfaceC4095
    /* JADX INFO: renamed from: ֏ */
    public int mo1282(byte[] bArr, int i, int i2) throws RawResourceDataSourceException {
        if (i2 == 0) {
            return 0;
        }
        long j = this.f2002;
        if (j == 0) {
            return -1;
        }
        if (j != -1) {
            try {
                i2 = (int) Math.min(j, i2);
            } catch (IOException e) {
                throw new RawResourceDataSourceException(e);
            }
        }
        InputStream inputStream = this.f2001;
        C4150.m6755(inputStream);
        int i3 = inputStream.read(bArr, i, i2);
        if (i3 == -1) {
            if (this.f2002 == -1) {
                return -1;
            }
            throw new RawResourceDataSourceException(new EOFException());
        }
        long j2 = this.f2002;
        if (j2 != -1) {
            this.f2002 = j2 - ((long) i3);
        }
        m6579(i3);
        return i3;
    }

    @Override // p138.p176.p177.p190.p238.InterfaceC4095
    /* JADX INFO: renamed from: ֏ */
    public long mo1283(C4098 c4098) throws RawResourceDataSourceException {
        try {
            Uri uri = c4098.f13076;
            this.f1999 = uri;
            if (!TextUtils.equals("rawresource", uri.getScheme())) {
                throw new RawResourceDataSourceException("URI must use scheme rawresource");
            }
            try {
                String lastPathSegment = uri.getLastPathSegment();
                C0567.m1856(lastPathSegment);
                int i = Integer.parseInt(lastPathSegment);
                m6580(c4098);
                AssetFileDescriptor assetFileDescriptorOpenRawResourceFd = this.f1998.openRawResourceFd(i);
                this.f2000 = assetFileDescriptorOpenRawResourceFd;
                if (assetFileDescriptorOpenRawResourceFd == null) {
                    throw new RawResourceDataSourceException("Resource is compressed: " + uri);
                }
                FileInputStream fileInputStream = new FileInputStream(assetFileDescriptorOpenRawResourceFd.getFileDescriptor());
                this.f2001 = fileInputStream;
                fileInputStream.skip(assetFileDescriptorOpenRawResourceFd.getStartOffset());
                if (fileInputStream.skip(c4098.f13081) < c4098.f13081) {
                    throw new EOFException();
                }
                long j = -1;
                if (c4098.f13082 != -1) {
                    this.f2002 = c4098.f13082;
                } else {
                    long length = assetFileDescriptorOpenRawResourceFd.getLength();
                    if (length != -1) {
                        j = length - c4098.f13081;
                    }
                    this.f2002 = j;
                }
                this.f2003 = true;
                m6582(c4098);
                return this.f2002;
            } catch (NumberFormatException unused) {
                throw new RawResourceDataSourceException("Resource identifier must be an integer.");
            }
        } catch (IOException e) {
            throw new RawResourceDataSourceException(e);
        }
    }

    @Override // p138.p176.p177.p190.p238.InterfaceC4095
    /* JADX INFO: renamed from: ؠ */
    public Uri mo1284() {
        return this.f1999;
    }
}
