Tidy up AssetDataSource.

This commit is contained in:
Oliver Woodman 2015-05-01 20:23:02 +01:00
parent 053e5b9f1c
commit 5ea7424ee3

View file

@ -15,16 +15,14 @@
*/ */
package com.google.android.exoplayer.upstream; package com.google.android.exoplayer.upstream;
import com.google.android.exoplayer.C;
import android.content.res.AssetManager;
import java.io.EOFException; import java.io.EOFException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import android.content.res.AssetManager;
import android.util.Log;
import com.google.android.exoplayer.C;
import com.google.android.exoplayer.upstream.FileDataSource.FileDataSourceException;
/** /**
* A local asset {@link DataSource}. * A local asset {@link DataSource}.
*/ */
@ -41,10 +39,10 @@ public final class AssetDataSource implements DataSource {
} }
private final AssetManager assetManager;
private final TransferListener listener; private final TransferListener listener;
private InputStream assetInputStream; private InputStream assetInputStream;
private AssetManager assetManager;
private long bytesRemaining; private long bytesRemaining;
private boolean opened; private boolean opened;
@ -52,7 +50,7 @@ public final class AssetDataSource implements DataSource {
* Constructs a new {@link DataSource} that retrieves data from a local asset. * Constructs a new {@link DataSource} that retrieves data from a local asset.
*/ */
public AssetDataSource(AssetManager assetManager) { public AssetDataSource(AssetManager assetManager) {
this(assetManager, null); this(assetManager, null);
} }
/** /**
@ -69,7 +67,8 @@ public final class AssetDataSource implements DataSource {
public long open(DataSpec dataSpec) throws AssetDataSourceException { public long open(DataSpec dataSpec) throws AssetDataSourceException {
try { try {
// Lose the '/' prefix in the path or else AssetManager won't find our file // Lose the '/' prefix in the path or else AssetManager won't find our file
assetInputStream = assetManager.open(dataSpec.uri.getPath().substring(1), AssetManager.ACCESS_RANDOM); assetInputStream = assetManager.open(dataSpec.uri.getPath().substring(1),
AssetManager.ACCESS_RANDOM);
assetInputStream.skip(dataSpec.position); assetInputStream.skip(dataSpec.position);
bytesRemaining = dataSpec.length == C.LENGTH_UNBOUNDED ? assetInputStream.available() bytesRemaining = dataSpec.length == C.LENGTH_UNBOUNDED ? assetInputStream.available()
: dataSpec.length; : dataSpec.length;
@ -94,7 +93,8 @@ public final class AssetDataSource implements DataSource {
} else { } else {
int bytesRead = 0; int bytesRead = 0;
try { try {
bytesRead = assetInputStream.read(buffer, offset, (int) Math.min(bytesRemaining, readLength)); bytesRead = assetInputStream.read(buffer, offset,
(int) Math.min(bytesRemaining, readLength));
} catch (IOException e) { } catch (IOException e) {
throw new AssetDataSourceException(e); throw new AssetDataSourceException(e);
} }
@ -114,12 +114,11 @@ public final class AssetDataSource implements DataSource {
public void close() throws AssetDataSourceException { public void close() throws AssetDataSourceException {
if (assetInputStream != null) { if (assetInputStream != null) {
try { try {
assetInputStream.close(); assetInputStream.close();
} catch (IOException e) { } catch (IOException e) {
throw new AssetDataSourceException(e); throw new AssetDataSourceException(e);
} finally { } finally {
assetInputStream = null; assetInputStream = null;
if (opened) { if (opened) {
opened = false; opened = false;
if (listener != null) { if (listener != null) {