Commit 55ce085a by tonihei Committed by Oliver Woodman

Add transferInitializing to BaseDataSource.

This allows implementations to notify when the transfer is about to be started.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=203102117
parent 985160a4
...@@ -301,6 +301,7 @@ public class CronetDataSource extends BaseDataSource implements HttpDataSource { ...@@ -301,6 +301,7 @@ public class CronetDataSource extends BaseDataSource implements HttpDataSource {
} }
currentUrlRequest.start(); currentUrlRequest.start();
transferInitializing(dataSpec);
try { try {
boolean connectionOpened = blockUntilConnectTimeout(); boolean connectionOpened = blockUntilConnectTimeout();
if (exception != null) { if (exception != null) {
......
...@@ -165,6 +165,7 @@ public class OkHttpDataSource extends BaseDataSource implements HttpDataSource { ...@@ -165,6 +165,7 @@ public class OkHttpDataSource extends BaseDataSource implements HttpDataSource {
this.dataSpec = dataSpec; this.dataSpec = dataSpec;
this.bytesRead = 0; this.bytesRead = 0;
this.bytesSkipped = 0; this.bytesSkipped = 0;
transferInitializing(dataSpec);
Request request = makeRequest(dataSpec); Request request = makeRequest(dataSpec);
try { try {
response = callFactory.newCall(request).execute(); response = callFactory.newCall(request).execute();
......
...@@ -51,6 +51,7 @@ public final class RtmpDataSource extends BaseDataSource { ...@@ -51,6 +51,7 @@ public final class RtmpDataSource extends BaseDataSource {
@Override @Override
public long open(DataSpec dataSpec) throws RtmpIOException { public long open(DataSpec dataSpec) throws RtmpIOException {
transferInitializing(dataSpec);
rtmpClient = new RtmpClient(); rtmpClient = new RtmpClient();
rtmpClient.open(dataSpec.uri.toString(), false); rtmpClient.open(dataSpec.uri.toString(), false);
......
...@@ -74,6 +74,7 @@ public final class AssetDataSource extends BaseDataSource { ...@@ -74,6 +74,7 @@ public final class AssetDataSource extends BaseDataSource {
} else if (path.startsWith("/")) { } else if (path.startsWith("/")) {
path = path.substring(1); path = path.substring(1);
} }
transferInitializing(dataSpec);
inputStream = assetManager.open(path, AssetManager.ACCESS_RANDOM); inputStream = assetManager.open(path, AssetManager.ACCESS_RANDOM);
long skipped = inputStream.skip(dataSpec.position); long skipped = inputStream.skip(dataSpec.position);
if (skipped < dataSpec.position) { if (skipped < dataSpec.position) {
......
...@@ -20,8 +20,9 @@ import java.util.ArrayList; ...@@ -20,8 +20,9 @@ import java.util.ArrayList;
/** /**
* Base {@link DataSource} implementation to keep a list of {@link TransferListener}s. * Base {@link DataSource} implementation to keep a list of {@link TransferListener}s.
* *
* <p>Subclasses must call {@link #transferStarted(DataSpec)}, {@link #bytesTransferred(int)}, and * <p>Subclasses must call {@link #transferInitializing(DataSpec)}, {@link
* {@link #transferEnded()} to inform listeners of data transfers. * #transferStarted(DataSpec)}, {@link #bytesTransferred(int)}, and {@link #transferEnded()} to
* inform listeners of data transfers.
*/ */
public abstract class BaseDataSource implements DataSource { public abstract class BaseDataSource implements DataSource {
...@@ -44,6 +45,15 @@ public abstract class BaseDataSource implements DataSource { ...@@ -44,6 +45,15 @@ public abstract class BaseDataSource implements DataSource {
} }
/** /**
* Notifies listeners that data transfer for the specified {@link DataSpec} is being initialized.
*
* @param dataSpec {@link DataSpec} describing the data for initializing transfer.
*/
protected final void transferInitializing(DataSpec dataSpec) {
// TODO: notify listeners.
}
/**
* Notifies listeners that data transfer for the specified {@link DataSpec} started. * Notifies listeners that data transfer for the specified {@link DataSpec} started.
* *
* @param dataSpec {@link DataSpec} describing the data being transferred. * @param dataSpec {@link DataSpec} describing the data being transferred.
......
...@@ -44,6 +44,7 @@ public final class ByteArrayDataSource extends BaseDataSource { ...@@ -44,6 +44,7 @@ public final class ByteArrayDataSource extends BaseDataSource {
@Override @Override
public long open(DataSpec dataSpec) throws IOException { public long open(DataSpec dataSpec) throws IOException {
uri = dataSpec.uri; uri = dataSpec.uri;
transferInitializing(dataSpec);
readPosition = (int) dataSpec.position; readPosition = (int) dataSpec.position;
bytesRemaining = (int) ((dataSpec.length == C.LENGTH_UNSET) bytesRemaining = (int) ((dataSpec.length == C.LENGTH_UNSET)
? (data.length - dataSpec.position) : dataSpec.length); ? (data.length - dataSpec.position) : dataSpec.length);
......
...@@ -73,6 +73,7 @@ public final class ContentDataSource extends BaseDataSource { ...@@ -73,6 +73,7 @@ public final class ContentDataSource extends BaseDataSource {
public long open(DataSpec dataSpec) throws ContentDataSourceException { public long open(DataSpec dataSpec) throws ContentDataSourceException {
try { try {
uri = dataSpec.uri; uri = dataSpec.uri;
transferInitializing(dataSpec);
assetFileDescriptor = resolver.openAssetFileDescriptor(uri, "r"); assetFileDescriptor = resolver.openAssetFileDescriptor(uri, "r");
if (assetFileDescriptor == null) { if (assetFileDescriptor == null) {
throw new FileNotFoundException("Could not open file descriptor for: " + uri); throw new FileNotFoundException("Could not open file descriptor for: " + uri);
......
...@@ -39,6 +39,7 @@ public final class DataSchemeDataSource extends BaseDataSource { ...@@ -39,6 +39,7 @@ public final class DataSchemeDataSource extends BaseDataSource {
@Override @Override
public long open(DataSpec dataSpec) throws IOException { public long open(DataSpec dataSpec) throws IOException {
transferInitializing(dataSpec);
this.dataSpec = dataSpec; this.dataSpec = dataSpec;
Uri uri = dataSpec.uri; Uri uri = dataSpec.uri;
String scheme = uri.getScheme(); String scheme = uri.getScheme();
......
...@@ -201,6 +201,7 @@ public class DefaultHttpDataSource extends BaseDataSource implements HttpDataSou ...@@ -201,6 +201,7 @@ public class DefaultHttpDataSource extends BaseDataSource implements HttpDataSou
this.dataSpec = dataSpec; this.dataSpec = dataSpec;
this.bytesRead = 0; this.bytesRead = 0;
this.bytesSkipped = 0; this.bytesSkipped = 0;
transferInitializing(dataSpec);
try { try {
connection = makeConnection(dataSpec); connection = makeConnection(dataSpec);
} catch (IOException e) { } catch (IOException e) {
......
...@@ -57,6 +57,7 @@ public final class FileDataSource extends BaseDataSource { ...@@ -57,6 +57,7 @@ public final class FileDataSource extends BaseDataSource {
public long open(DataSpec dataSpec) throws FileDataSourceException { public long open(DataSpec dataSpec) throws FileDataSourceException {
try { try {
uri = dataSpec.uri; uri = dataSpec.uri;
transferInitializing(dataSpec);
file = new RandomAccessFile(dataSpec.uri.getPath(), "r"); file = new RandomAccessFile(dataSpec.uri.getPath(), "r");
file.seek(dataSpec.position); file.seek(dataSpec.position);
bytesRemaining = dataSpec.length == C.LENGTH_UNSET ? file.length() - dataSpec.position bytesRemaining = dataSpec.length == C.LENGTH_UNSET ? file.length() - dataSpec.position
......
...@@ -105,6 +105,7 @@ public final class RawResourceDataSource extends BaseDataSource { ...@@ -105,6 +105,7 @@ public final class RawResourceDataSource extends BaseDataSource {
throw new RawResourceDataSourceException("Resource identifier must be an integer."); throw new RawResourceDataSourceException("Resource identifier must be an integer.");
} }
transferInitializing(dataSpec);
assetFileDescriptor = resources.openRawResourceFd(resourceId); assetFileDescriptor = resources.openRawResourceFd(resourceId);
inputStream = new FileInputStream(assetFileDescriptor.getFileDescriptor()); inputStream = new FileInputStream(assetFileDescriptor.getFileDescriptor());
inputStream.skip(assetFileDescriptor.getStartOffset()); inputStream.skip(assetFileDescriptor.getStartOffset());
......
...@@ -100,7 +100,7 @@ public final class UdpDataSource extends BaseDataSource { ...@@ -100,7 +100,7 @@ public final class UdpDataSource extends BaseDataSource {
uri = dataSpec.uri; uri = dataSpec.uri;
String host = uri.getHost(); String host = uri.getHost();
int port = uri.getPort(); int port = uri.getPort();
transferInitializing(dataSpec);
try { try {
address = InetAddress.getByName(host); address = InetAddress.getByName(host);
socketAddress = new InetSocketAddress(address, port); socketAddress = new InetSocketAddress(address, port);
......
...@@ -104,10 +104,12 @@ public class FakeDataSource extends BaseDataSource { ...@@ -104,10 +104,12 @@ public class FakeDataSource extends BaseDataSource {
public final long open(DataSpec dataSpec) throws IOException { public final long open(DataSpec dataSpec) throws IOException {
Assertions.checkState(!openCalled); Assertions.checkState(!openCalled);
openCalled = true; openCalled = true;
// DataSpec requires a matching close call even if open fails. // DataSpec requires a matching close call even if open fails.
uri = dataSpec.uri; uri = dataSpec.uri;
openedDataSpecs.add(dataSpec); openedDataSpecs.add(dataSpec);
transferInitializing(dataSpec);
fakeData = fakeDataSet.getData(uri.toString()); fakeData = fakeDataSet.getData(uri.toString());
if (fakeData == null) { if (fakeData == null) {
throw new IOException("Data not found: " + dataSpec.uri); throw new IOException("Data not found: " + dataSpec.uri);
......
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