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 @Override
public void close() throws AssetDataSourceException { public void close() throws AssetDataSourceException {
uri = null; uri = null;
if (inputStream != null) { try {
try { if (inputStream != null) {
inputStream.close(); inputStream.close();
} catch (IOException e) { }
throw new AssetDataSourceException(e); } catch (IOException e) {
} finally { throw new AssetDataSourceException(e);
inputStream = null; } finally {
if (opened) { inputStream = null;
opened = false; if (opened) {
if (listener != null) { opened = false;
listener.onTransferEnd(this); 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 final TransferListener<? super ContentDataSource> listener;
private Uri uri; private Uri uri;
private AssetFileDescriptor assetFileDescriptor;
private InputStream inputStream; private InputStream inputStream;
private long bytesRemaining; private long bytesRemaining;
private boolean opened; private boolean opened;
@ -69,8 +70,8 @@ public final class ContentDataSource implements DataSource {
public long open(DataSpec dataSpec) throws ContentDataSourceException { public long open(DataSpec dataSpec) throws ContentDataSourceException {
try { try {
uri = dataSpec.uri; uri = dataSpec.uri;
AssetFileDescriptor assetFd = resolver.openAssetFileDescriptor(uri, "r"); assetFileDescriptor = resolver.openAssetFileDescriptor(uri, "r");
inputStream = new FileInputStream(assetFd.getFileDescriptor()); inputStream = new FileInputStream(assetFileDescriptor.getFileDescriptor());
long skipped = inputStream.skip(dataSpec.position); long skipped = inputStream.skip(dataSpec.position);
if (skipped < 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 // 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 @Override
public void close() throws ContentDataSourceException { public void close() throws ContentDataSourceException {
uri = null; uri = null;
if (inputStream != null) { try {
try { if (inputStream != null) {
inputStream.close(); inputStream.close();
}
} catch (IOException e) {
throw new ContentDataSourceException(e);
} finally {
inputStream = null;
try {
if (assetFileDescriptor != null) {
assetFileDescriptor.close();
}
} catch (IOException e) { } catch (IOException e) {
throw new ContentDataSourceException(e); throw new ContentDataSourceException(e);
} finally { } finally {
inputStream = null; assetFileDescriptor = null;
if (opened) { if (opened) {
opened = false; opened = false;
if (listener != null) { if (listener != null) {

View File

@ -109,18 +109,18 @@ public final class FileDataSource implements DataSource {
@Override @Override
public void close() throws FileDataSourceException { public void close() throws FileDataSourceException {
uri = null; uri = null;
if (file != null) { try {
try { if (file != null) {
file.close(); file.close();
} catch (IOException e) { }
throw new FileDataSourceException(e); } catch (IOException e) {
} finally { throw new FileDataSourceException(e);
file = null; } finally {
if (opened) { file = null;
opened = false; if (opened) {
if (listener != null) { opened = false;
listener.onTransferEnd(this); if (listener != null) {
} listener.onTransferEnd(this);
} }
} }
} }