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 e1f2077 commit 8308b74
Showing 1 changed file with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public ContentDataSourceException(IOException cause) {
private final ContentResolver resolver;
private final TransferListener listener;

private AssetFileDescriptor assetFileDescriptor;
private InputStream inputStream;
private String uriString;
private long bytesRemaining;
Expand All @@ -69,8 +70,8 @@ public ContentDataSource(Context context, TransferListener listener) {
public long open(DataSpec dataSpec) throws ContentDataSourceException {
try {
uriString = dataSpec.uri.toString();
AssetFileDescriptor assetFd = resolver.openAssetFileDescriptor(dataSpec.uri, "r");
inputStream = new FileInputStream(assetFd.getFileDescriptor());
assetFileDescriptor = resolver.openAssetFileDescriptor(dataSpec.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 String getUri() {
@Override
public void close() throws ContentDataSourceException {
uriString = 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

0 comments on commit 8308b74

Please sign in to comment.