package com.mbridge.msdk.playercommon.exoplayer2.upstream; import android.content.Context; import android.content.res.AssetManager; import android.net.Uri; import java.io.EOFException; import java.io.IOException; import java.io.InputStream; /* loaded from: classes4.dex */ public final class AssetDataSource implements DataSource { private final AssetManager assetManager; private long bytesRemaining; private InputStream inputStream; private final TransferListener listener; private boolean opened; private Uri uri; @Override // com.mbridge.msdk.playercommon.exoplayer2.upstream.DataSource public final Uri getUri() { return this.uri; } public static final class AssetDataSourceException extends IOException { public AssetDataSourceException(IOException iOException) { super(iOException); } } public AssetDataSource(Context context) { this(context, null); } public AssetDataSource(Context context, TransferListener transferListener) { this.assetManager = context.getAssets(); this.listener = transferListener; } @Override // com.mbridge.msdk.playercommon.exoplayer2.upstream.DataSource public final long open(DataSpec dataSpec) throws AssetDataSourceException { try { Uri uri = dataSpec.uri; this.uri = uri; String path = uri.getPath(); if (path.startsWith("/android_asset/")) { path = path.substring(15); } else if (path.startsWith("/")) { path = path.substring(1); } InputStream open = this.assetManager.open(path, 1); this.inputStream = open; if (open.skip(dataSpec.position) < dataSpec.position) { throw new EOFException(); } long j = dataSpec.length; if (j != -1) { this.bytesRemaining = j; } else { long available = this.inputStream.available(); this.bytesRemaining = available; if (available == 2147483647L) { this.bytesRemaining = -1L; } } this.opened = true; TransferListener transferListener = this.listener; if (transferListener != null) { transferListener.onTransferStart(this, dataSpec); } return this.bytesRemaining; } catch (IOException e) { throw new AssetDataSourceException(e); } } @Override // com.mbridge.msdk.playercommon.exoplayer2.upstream.DataSource public final int read(byte[] bArr, int i, int i2) throws AssetDataSourceException { 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 AssetDataSourceException(e); } } int read = this.inputStream.read(bArr, i, i2); if (read == -1) { if (this.bytesRemaining == -1) { return -1; } throw new AssetDataSourceException(new EOFException()); } long j2 = this.bytesRemaining; if (j2 != -1) { this.bytesRemaining = j2 - read; } TransferListener transferListener = this.listener; if (transferListener != null) { transferListener.onBytesTransferred(this, read); } return read; } @Override // com.mbridge.msdk.playercommon.exoplayer2.upstream.DataSource public final void close() throws AssetDataSourceException { this.uri = null; try { try { InputStream inputStream = this.inputStream; if (inputStream != null) { inputStream.close(); } } catch (IOException e) { throw new AssetDataSourceException(e); } } finally { this.inputStream = null; if (this.opened) { this.opened = false; TransferListener transferListener = this.listener; if (transferListener != null) { transferListener.onTransferEnd(this); } } } } }