Commit b3c6f1ca by olly Committed by Oliver Woodman

Finish cleaning DataSource implementations.

- Enfroce read returns 0 if readLength==0 everywhere.
- Fixes and simplifications for CronetDataSource.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=135138232
parent f8ed4cfd
......@@ -80,7 +80,9 @@ public final class FileDataSource implements DataSource {
@Override
public int read(byte[] buffer, int offset, int readLength) throws FileDataSourceException {
if (bytesRemaining == 0) {
if (readLength == 0) {
return 0;
} else if (bytesRemaining == 0) {
return C.RESULT_END_OF_INPUT;
} else {
int bytesRead;
......
......@@ -129,6 +129,10 @@ public final class UdpDataSource implements DataSource {
@Override
public int read(byte[] buffer, int offset, int readLength) throws UdpDataSourceException {
if (readLength == 0) {
return 0;
}
if (packetRemaining == 0) {
// We've read all of the data from the current packet. Get another.
try {
......@@ -136,7 +140,6 @@ public final class UdpDataSource implements DataSource {
} catch (IOException e) {
throw new UdpDataSourceException(e);
}
packetRemaining = packet.getLength();
if (listener != null) {
listener.onBytesTransferred(this, packetRemaining);
......
......@@ -194,12 +194,15 @@ public final class CacheDataSource implements DataSource {
}
@Override
public int read(byte[] buffer, int offset, int max) throws IOException {
public int read(byte[] buffer, int offset, int readLength) throws IOException {
if (readLength == 0) {
return 0;
}
if (bytesRemaining == 0) {
return C.RESULT_END_OF_INPUT;
}
try {
int bytesRead = currentDataSource.read(buffer, offset, max);
int bytesRead = currentDataSource.read(buffer, offset, readLength);
if (bytesRead >= 0) {
if (currentDataSource == cacheReadDataSource) {
totalCachedBytesRead += bytesRead;
......@@ -218,7 +221,7 @@ public final class CacheDataSource implements DataSource {
closeCurrentSource();
if (bytesRemaining > 0 || bytesRemaining == C.LENGTH_UNSET) {
if (openNextSource(false)) {
return read(buffer, offset, max);
return read(buffer, offset, readLength);
}
}
}
......
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