package androidx.media3.datasource; import android.content.Context; import android.content.res.AssetFileDescriptor; import android.content.res.Resources; import android.net.Uri; import android.text.TextUtils; import androidx.media3.common.util.Assertions; import androidx.media3.common.util.Util; import java.io.EOFException; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.channels.FileChannel; /* loaded from: classes2.dex */ public final class RawResourceDataSource extends BaseDataSource { public static final String RAW_RESOURCE_SCHEME = "rawresource"; private AssetFileDescriptor assetFileDescriptor; private long bytesRemaining; private InputStream inputStream; private boolean opened; private final String packageName; private final Resources resources; private Uri uri; @Override // androidx.media3.datasource.DataSource public Uri getUri() { return this.uri; } /* loaded from: classes2.dex */ public static class RawResourceDataSourceException extends DataSourceException { @Deprecated public RawResourceDataSourceException(String str) { super(str, null, 2000); } @Deprecated public RawResourceDataSourceException(Throwable th) { super(th, 2000); } public RawResourceDataSourceException(String str, Throwable th, int i) { super(str, th, i); } } public static Uri buildRawResourceUri(int i) { return Uri.parse("rawresource:///" + i); } public RawResourceDataSource(Context context) { super(false); this.resources = context.getResources(); this.packageName = context.getPackageName(); } @Override // androidx.media3.datasource.DataSource public long open(DataSpec dataSpec) throws RawResourceDataSourceException { int parseInt; Uri normalizeScheme = dataSpec.uri.normalizeScheme(); this.uri = normalizeScheme; if (TextUtils.equals("rawresource", normalizeScheme.getScheme()) || (TextUtils.equals("android.resource", normalizeScheme.getScheme()) && normalizeScheme.getPathSegments().size() == 1 && ((String) Assertions.checkNotNull(normalizeScheme.getLastPathSegment())).matches("\\d+"))) { try { parseInt = Integer.parseInt((String) Assertions.checkNotNull(normalizeScheme.getLastPathSegment())); } catch (NumberFormatException unused) { throw new RawResourceDataSourceException("Resource identifier must be an integer.", null, 1004); } } else if (TextUtils.equals("android.resource", normalizeScheme.getScheme())) { String str = (String) Assertions.checkNotNull(normalizeScheme.getPath()); if (str.startsWith("/")) { str = str.substring(1); } String host = normalizeScheme.getHost(); parseInt = this.resources.getIdentifier((TextUtils.isEmpty(host) ? "" : host + ":") + str, "raw", this.packageName); if (parseInt == 0) { throw new RawResourceDataSourceException("Resource not found.", null, 2005); } } else { throw new RawResourceDataSourceException("Unsupported URI scheme (" + normalizeScheme.getScheme() + "). Only rawresource and android.resource are supported.", null, 1004); } transferInitializing(dataSpec); try { AssetFileDescriptor openRawResourceFd = this.resources.openRawResourceFd(parseInt); this.assetFileDescriptor = openRawResourceFd; if (openRawResourceFd == null) { throw new RawResourceDataSourceException("Resource is compressed: " + normalizeScheme, null, 2000); } long length = openRawResourceFd.getLength(); FileInputStream fileInputStream = new FileInputStream(openRawResourceFd.getFileDescriptor()); this.inputStream = fileInputStream; if (length != -1) { try { if (dataSpec.position > length) { throw new RawResourceDataSourceException(null, null, 2008); } } catch (RawResourceDataSourceException e) { throw e; } catch (IOException e2) { throw new RawResourceDataSourceException(null, e2, 2000); } } long startOffset = openRawResourceFd.getStartOffset(); long skip = fileInputStream.skip(dataSpec.position + startOffset) - startOffset; if (skip != dataSpec.position) { throw new RawResourceDataSourceException(null, null, 2008); } if (length == -1) { FileChannel channel = fileInputStream.getChannel(); if (channel.size() == 0) { this.bytesRemaining = -1L; } else { long size = channel.size() - channel.position(); this.bytesRemaining = size; if (size < 0) { throw new RawResourceDataSourceException(null, null, 2008); } } } else { long j = length - skip; this.bytesRemaining = j; if (j < 0) { throw new DataSourceException(2008); } } if (dataSpec.length != -1) { long j2 = this.bytesRemaining; this.bytesRemaining = j2 == -1 ? dataSpec.length : Math.min(j2, dataSpec.length); } this.opened = true; transferStarted(dataSpec); return dataSpec.length != -1 ? dataSpec.length : this.bytesRemaining; } catch (Resources.NotFoundException e3) { throw new RawResourceDataSourceException(null, e3, 2005); } } @Override // androidx.media3.common.DataReader public int read(byte[] bArr, int i, int i2) throws RawResourceDataSourceException { if (i2 == 0) { return 0; } long j = this.bytesRemaining; if (j == 0) { return -1; } if (j != -1) { try { i2 = (int) Math.min(j, i2); } catch (IOException e) { throw new RawResourceDataSourceException(null, e, 2000); } } int read = ((InputStream) Util.castNonNull(this.inputStream)).read(bArr, i, i2); if (read == -1) { if (this.bytesRemaining == -1) { return -1; } throw new RawResourceDataSourceException("End of stream reached having not read sufficient data.", new EOFException(), 2000); } long j2 = this.bytesRemaining; if (j2 != -1) { this.bytesRemaining = j2 - read; } bytesTransferred(read); return read; } @Override // androidx.media3.datasource.DataSource public void close() throws RawResourceDataSourceException { this.uri = null; try { try { InputStream inputStream = this.inputStream; if (inputStream != null) { inputStream.close(); } this.inputStream = null; try { try { AssetFileDescriptor assetFileDescriptor = this.assetFileDescriptor; if (assetFileDescriptor != null) { assetFileDescriptor.close(); } } finally { this.assetFileDescriptor = null; if (this.opened) { this.opened = false; transferEnded(); } } } catch (IOException e) { throw new RawResourceDataSourceException(null, e, 2000); } } catch (IOException e2) { throw new RawResourceDataSourceException(null, e2, 2000); } } catch (Throwable th) { this.inputStream = null; try { try { AssetFileDescriptor assetFileDescriptor2 = this.assetFileDescriptor; if (assetFileDescriptor2 != null) { assetFileDescriptor2.close(); } this.assetFileDescriptor = null; if (this.opened) { this.opened = false; transferEnded(); } throw th; } catch (IOException e3) { throw new RawResourceDataSourceException(null, e3, 2000); } } finally { this.assetFileDescriptor = null; if (this.opened) { this.opened = false; transferEnded(); } } } } }