Commit bf0d398b by olly Committed by Oliver Woodman

Fix RawResourceDataSource remaining length

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=133120449
parent e4cc2d6a
...@@ -20,7 +20,6 @@ import android.content.res.AssetFileDescriptor; ...@@ -20,7 +20,6 @@ import android.content.res.AssetFileDescriptor;
import android.content.res.Resources; import android.content.res.Resources;
import android.net.Uri; import android.net.Uri;
import android.text.TextUtils; import android.text.TextUtils;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import java.io.EOFException; import java.io.EOFException;
import java.io.FileInputStream; import java.io.FileInputStream;
...@@ -30,8 +29,9 @@ import java.io.InputStream; ...@@ -30,8 +29,9 @@ import java.io.InputStream;
/** /**
* A {@link DataSource} for reading a raw resource inside the APK. * A {@link DataSource} for reading a raw resource inside the APK.
* <p> * <p>
* URIs supported by this source are of the form {@code rawresource:///resourceId}, where resourceId * URIs supported by this source are of the form {@code rawresource:///rawResourceId}, where
* is the integer identifier of a raw resource. * rawResourceId is the integer identifier of a raw resource. {@link #buildRawResourceUri(int)} can
* be used to build {@link Uri}s in this format.
*/ */
public final class RawResourceDataSource implements DataSource { public final class RawResourceDataSource implements DataSource {
...@@ -49,9 +49,14 @@ public final class RawResourceDataSource implements DataSource { ...@@ -49,9 +49,14 @@ public final class RawResourceDataSource implements DataSource {
} }
/** /**
* The URI scheme used to identify raw resources. URIs used with this class must use this scheme. * Builds a {@link Uri} for the specified raw resource identifier.
*
* @param rawResourceId A raw resource identifier (i.e. a constant defined in {@code R.raw}).
* @return The corresponding {@link Uri}.
*/ */
public static final String RAW_RESOURCE_SCHEME = "rawresource"; public static final Uri buildRawResourceUri(int rawResourceId) {
return Uri.parse("rawresource:///" + rawResourceId);
}
private final Resources resources; private final Resources resources;
private final TransferListener<? super RawResourceDataSource> listener; private final TransferListener<? super RawResourceDataSource> listener;
...@@ -106,13 +111,10 @@ public final class RawResourceDataSource implements DataSource { ...@@ -106,13 +111,10 @@ public final class RawResourceDataSource implements DataSource {
if (dataSpec.length != C.LENGTH_UNSET) { if (dataSpec.length != C.LENGTH_UNSET) {
bytesRemaining = dataSpec.length; bytesRemaining = dataSpec.length;
} else { } else {
bytesRemaining = inputStream.available(); long assetFileDescriptorLength = assetFileDescriptor.getLength();
if (bytesRemaining == 0) { // If the length is UNKNOWN_LENGTH then the asset extends to the end of the file.
// FileInputStream.available() returns 0 if the remaining length cannot be determined, or bytesRemaining = assetFileDescriptorLength == AssetFileDescriptor.UNKNOWN_LENGTH
// if it's greater than Integer.MAX_VALUE. We don't know the true length in either case, ? C.LENGTH_UNSET : (assetFileDescriptorLength - dataSpec.position);
// so treat as unbounded.
bytesRemaining = C.LENGTH_UNSET;
}
} }
} catch (IOException e) { } catch (IOException e) {
throw new RawResourceDataSourceException(e); throw new RawResourceDataSourceException(e);
...@@ -174,7 +176,7 @@ public final class RawResourceDataSource implements DataSource { ...@@ -174,7 +176,7 @@ public final class RawResourceDataSource implements DataSource {
assetFileDescriptor.close(); assetFileDescriptor.close();
} }
} catch (IOException e) { } catch (IOException e) {
throw new RawResourceDataSourceException(e); throw new RawResourceDataSourceException(e);
} finally { } finally {
assetFileDescriptor = null; assetFileDescriptor = null;
if (opened) { if (opened) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment