Fix ContentDataSource handling of AssetFileDescriptor

Also tweak how the null checks happen in a few DataSource
implementations (should be no-op changes, but allow you
to look at close() and be happy it does the right thing
without having to loop at the open() implementations).

Issue: #1759

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=131172427
This commit is contained in:
olly 2016-08-24 08:05:38 -07:00 committed by Oliver Woodman
parent 5f1a2c71f0
commit ef7dd697b1
3 changed files with 37 additions and 27 deletions

View File

@ -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);
}
}
}

View File

@ -45,6 +45,7 @@ public final class ContentDataSource implements DataSource {
private final TransferListener<? super ContentDataSource> 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) {

View File

@ -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);
}
}
}