Commit 610153f4 authored by clm's avatar clm Committed by Commit bot

Flush chunks on each upload read.

The way CronetJavaEngine (which uses JavaUrlRequest) is implemented in regards to chunked uploads:
1) Is inconsistent with it's native counterpart which almost immediately writes the obtained data to the wire.
2) Makes it impossible to slowly stream data (like during a voice search) in a controlled manner. No data is sent other than at JavaUrlRequest.DEFAULT_CHUNK_LENGTH (8192) intervals.

BUG=b/31592204

Review-Url: https://codereview.chromium.org/2356923002
Cr-Commit-Position: refs/heads/master@{#419869}
parent f68c52f4
......@@ -11,6 +11,7 @@ import android.util.Log;
import java.io.Closeable;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
......@@ -260,6 +261,7 @@ final class JavaUrlRequest implements UrlRequest {
final Executor mExecutor;
final HttpURLConnection mUrlConnection;
WritableByteChannel mOutputChannel;
OutputStream mUrlConnectionOutputStream;
final UploadDataProvider mUploadProvider;
ByteBuffer mBuffer;
/** This holds the total bytes to send (the content-length). -1 if unknown. */
......@@ -303,6 +305,11 @@ final class JavaUrlRequest implements UrlRequest {
while (mBuffer.hasRemaining()) {
mWrittenBytes += mOutputChannel.write(mBuffer);
}
// Forces a chunk to be sent, rather than buffering to the DEFAULT_CHUNK_LENGTH.
// This allows clients to trickle-upload bytes as they become available without
// introducing latency due to buffering.
mUrlConnectionOutputStream.flush();
if (mWrittenBytes < mTotalBytes || (mTotalBytes == -1 && !finalChunk)) {
mBuffer.clear();
mSinkState.set(SinkState.AWAITING_READ_RESULT);
......@@ -351,7 +358,8 @@ final class JavaUrlRequest implements UrlRequest {
mAdditionalStatusDetails = Status.CONNECTING;
mUrlConnection.connect();
mAdditionalStatusDetails = Status.SENDING_REQUEST;
mOutputChannel = Channels.newChannel(mUrlConnection.getOutputStream());
mUrlConnectionOutputStream = mUrlConnection.getOutputStream();
mOutputChannel = Channels.newChannel(mUrlConnectionOutputStream);
}
mSinkState.set(SinkState.AWAITING_READ_RESULT);
executeOnUploadExecutor(new CheckedRunnable() {
......
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 to comment