Commit 94c7ee72 by olly Committed by Oliver Woodman

Cronet - Skip if server doesn't support range requests

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=135819142
parent 996fe47f
...@@ -103,6 +103,7 @@ public class CronetDataSource extends UrlRequest.Callback implements HttpDataSou ...@@ -103,6 +103,7 @@ public class CronetDataSource extends UrlRequest.Callback implements HttpDataSou
// Accessed by the calling thread only. // Accessed by the calling thread only.
private boolean opened; private boolean opened;
private long bytesToSkip;
private long bytesRemaining; private long bytesRemaining;
// Written from the calling thread only. currentUrlRequest.start() calls ensure writes are visible // Written from the calling thread only. currentUrlRequest.start() calls ensure writes are visible
...@@ -242,9 +243,10 @@ public class CronetDataSource extends UrlRequest.Callback implements HttpDataSou ...@@ -242,9 +243,10 @@ public class CronetDataSource extends UrlRequest.Callback implements HttpDataSou
} }
} }
// TODO: Handle the case where we requested a range starting from a non-zero position and // If we requested a range starting from a non-zero position and received a 200 rather than a
// received a 200 rather than a 206. This occurs if the server does not support partial // 206, then the server does not support partial requests. We'll need to manually skip to the
// requests, and requires that the source skips to the requested position. // requested position.
bytesToSkip = responseCode == 200 && dataSpec.position != 0 ? dataSpec.position : 0;
// Calculate the content length. // Calculate the content length.
if (!getIsCompressed(responseInfo)) { if (!getIsCompressed(responseInfo)) {
...@@ -281,7 +283,7 @@ public class CronetDataSource extends UrlRequest.Callback implements HttpDataSou ...@@ -281,7 +283,7 @@ public class CronetDataSource extends UrlRequest.Callback implements HttpDataSou
readBuffer = ByteBuffer.allocateDirect(READ_BUFFER_SIZE_BYTES); readBuffer = ByteBuffer.allocateDirect(READ_BUFFER_SIZE_BYTES);
readBuffer.limit(0); readBuffer.limit(0);
} }
if (!readBuffer.hasRemaining()) { while (!readBuffer.hasRemaining()) {
// Fill readBuffer with more data from Cronet. // Fill readBuffer with more data from Cronet.
operation.close(); operation.close();
readBuffer.clear(); readBuffer.clear();
...@@ -301,6 +303,12 @@ public class CronetDataSource extends UrlRequest.Callback implements HttpDataSou ...@@ -301,6 +303,12 @@ public class CronetDataSource extends UrlRequest.Callback implements HttpDataSou
} else { } else {
// The operation didn't time out, fail or finish, and therefore data must have been read. // The operation didn't time out, fail or finish, and therefore data must have been read.
readBuffer.flip(); readBuffer.flip();
Assertions.checkState(readBuffer.hasRemaining());
if (bytesToSkip > 0) {
int bytesSkipped = (int) Math.min(readBuffer.remaining(), bytesToSkip);
readBuffer.position(readBuffer.position() + bytesSkipped);
bytesToSkip -= bytesSkipped;
}
} }
} }
......
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