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