Skip to content

Commit

Permalink
Fix ContentDataSource handling of AssetFileDescriptor
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ojw28 committed Aug 31, 2016
1 parent 5f1a2c7 commit ef7dd69
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,18 @@ public Uri getUri() {
@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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public ContentDataSourceException(IOException cause) {
private final TransferListener<? super ContentDataSource> listener;

private Uri uri;
private AssetFileDescriptor assetFileDescriptor;
private InputStream inputStream;
private long bytesRemaining;
private boolean opened;
Expand All @@ -69,8 +70,8 @@ public ContentDataSource(Context context, TransferListener<? super ContentDataSo
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
Expand Down Expand Up @@ -135,13 +136,22 @@ public Uri getUri() {
@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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,18 @@ public Uri getUri() {
@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);
}
}
}
Expand Down

0 comments on commit ef7dd69

Please sign in to comment.