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