Let FileDataSource report to a TransferListener.
This commit is contained in:
parent
b8415dba59
commit
f859205438
@ -36,8 +36,27 @@ public final class FileDataSource implements DataSource {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private final TransferListener listener;
|
||||||
|
|
||||||
private RandomAccessFile file;
|
private RandomAccessFile file;
|
||||||
private long bytesRemaining;
|
private long bytesRemaining;
|
||||||
|
private boolean opened;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new {@link DataSource} that retrieves data from a file.
|
||||||
|
*/
|
||||||
|
public FileDataSource() {
|
||||||
|
this(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new {@link DataSource} that retrieves data from a file.
|
||||||
|
*
|
||||||
|
* @param listener An optional listener. Specify {@code null} for no listener.
|
||||||
|
*/
|
||||||
|
public FileDataSource(TransferListener listener) {
|
||||||
|
this.listener = listener;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long open(DataSpec dataSpec) throws FileDataSourceException {
|
public long open(DataSpec dataSpec) throws FileDataSourceException {
|
||||||
@ -46,10 +65,16 @@ public final class FileDataSource implements DataSource {
|
|||||||
file.seek(dataSpec.position);
|
file.seek(dataSpec.position);
|
||||||
bytesRemaining = dataSpec.length == C.LENGTH_UNBOUNDED ? file.length() - dataSpec.position
|
bytesRemaining = dataSpec.length == C.LENGTH_UNBOUNDED ? file.length() - dataSpec.position
|
||||||
: dataSpec.length;
|
: dataSpec.length;
|
||||||
return bytesRemaining;
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new FileDataSourceException(e);
|
throw new FileDataSourceException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
opened = true;
|
||||||
|
if (listener != null) {
|
||||||
|
listener.onTransferStart();
|
||||||
|
}
|
||||||
|
|
||||||
|
return bytesRemaining;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -63,7 +88,14 @@ public final class FileDataSource implements DataSource {
|
|||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new FileDataSourceException(e);
|
throw new FileDataSourceException(e);
|
||||||
}
|
}
|
||||||
bytesRemaining -= bytesRead;
|
|
||||||
|
if (bytesRead > 0) {
|
||||||
|
bytesRemaining -= bytesRead;
|
||||||
|
if (listener != null) {
|
||||||
|
listener.onBytesTransferred(bytesRead);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return bytesRead;
|
return bytesRead;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -75,8 +107,16 @@ public final class FileDataSource implements DataSource {
|
|||||||
file.close();
|
file.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new FileDataSourceException(e);
|
throw new FileDataSourceException(e);
|
||||||
|
} finally {
|
||||||
|
file = null;
|
||||||
|
|
||||||
|
if (opened) {
|
||||||
|
opened = false;
|
||||||
|
if (listener != null) {
|
||||||
|
listener.onTransferEnd();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
file = null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user