diff --git a/library/src/main/java/com/google/android/exoplayer2/upstream/AssetDataSource.java b/library/src/main/java/com/google/android/exoplayer2/upstream/AssetDataSource.java index ff7ea42d4d..1a685ca1af 100644 --- a/library/src/main/java/com/google/android/exoplayer2/upstream/AssetDataSource.java +++ b/library/src/main/java/com/google/android/exoplayer2/upstream/AssetDataSource.java @@ -137,18 +137,18 @@ public final class AssetDataSource implements DataSource { @Override public void close() throws AssetDataSourceException { uri = null; - if (inputStream != null) { - try { + try { + if (inputStream != null) { inputStream.close(); - } catch (IOException e) { - throw new AssetDataSourceException(e); - } finally { - inputStream = null; - if (opened) { - opened = false; - if (listener != null) { - listener.onTransferEnd(this); - } + } + } catch (IOException e) { + throw new AssetDataSourceException(e); + } finally { + inputStream = null; + if (opened) { + opened = false; + if (listener != null) { + listener.onTransferEnd(this); } } } diff --git a/library/src/main/java/com/google/android/exoplayer2/upstream/ContentDataSource.java b/library/src/main/java/com/google/android/exoplayer2/upstream/ContentDataSource.java index 838a7970dc..2a0ad01489 100644 --- a/library/src/main/java/com/google/android/exoplayer2/upstream/ContentDataSource.java +++ b/library/src/main/java/com/google/android/exoplayer2/upstream/ContentDataSource.java @@ -45,6 +45,7 @@ public final class ContentDataSource implements DataSource { private final TransferListener listener; private Uri uri; + private AssetFileDescriptor assetFileDescriptor; private InputStream inputStream; private long bytesRemaining; private boolean opened; @@ -69,8 +70,8 @@ public final class ContentDataSource implements DataSource { public long open(DataSpec dataSpec) throws ContentDataSourceException { try { uri = dataSpec.uri; - AssetFileDescriptor assetFd = resolver.openAssetFileDescriptor(uri, "r"); - inputStream = new FileInputStream(assetFd.getFileDescriptor()); + assetFileDescriptor = resolver.openAssetFileDescriptor(uri, "r"); + inputStream = new FileInputStream(assetFileDescriptor.getFileDescriptor()); long skipped = inputStream.skip(dataSpec.position); if (skipped < dataSpec.position) { // We expect the skip to be satisfied in full. If it isn't then we're probably trying to @@ -135,13 +136,22 @@ public final class ContentDataSource implements DataSource { @Override public void close() throws ContentDataSourceException { uri = null; - if (inputStream != null) { - try { + try { + if (inputStream != null) { inputStream.close(); + } + } catch (IOException e) { + throw new ContentDataSourceException(e); + } finally { + inputStream = null; + try { + if (assetFileDescriptor != null) { + assetFileDescriptor.close(); + } } catch (IOException e) { throw new ContentDataSourceException(e); } finally { - inputStream = null; + assetFileDescriptor = null; if (opened) { opened = false; if (listener != null) { diff --git a/library/src/main/java/com/google/android/exoplayer2/upstream/FileDataSource.java b/library/src/main/java/com/google/android/exoplayer2/upstream/FileDataSource.java index bed018d76c..d211cfacbd 100644 --- a/library/src/main/java/com/google/android/exoplayer2/upstream/FileDataSource.java +++ b/library/src/main/java/com/google/android/exoplayer2/upstream/FileDataSource.java @@ -109,18 +109,18 @@ public final class FileDataSource implements DataSource { @Override public void close() throws FileDataSourceException { uri = null; - if (file != null) { - try { + try { + if (file != null) { file.close(); - } catch (IOException e) { - throw new FileDataSourceException(e); - } finally { - file = null; - if (opened) { - opened = false; - if (listener != null) { - listener.onTransferEnd(this); - } + } + } catch (IOException e) { + throw new FileDataSourceException(e); + } finally { + file = null; + if (opened) { + opened = false; + if (listener != null) { + listener.onTransferEnd(this); } } }