Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(623)

Unified Diff: components/cronet/android/java/src/org/chromium/net/urlconnection/CronetOutputStream.java

Issue 966743003: [Cronet] Implement getOutputStream in CronetHttpURLConnection (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@chunked_support
Patch Set: Use large data in buffered case too Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/cronet/android/java/src/org/chromium/net/urlconnection/CronetOutputStream.java
diff --git a/components/cronet/android/java/src/org/chromium/net/urlconnection/CronetOutputStream.java b/components/cronet/android/java/src/org/chromium/net/urlconnection/CronetOutputStream.java
new file mode 100644
index 0000000000000000000000000000000000000000..ad6cff47c31e42dd5ab2bf77ae7f72ac9f3f0d0a
--- /dev/null
+++ b/components/cronet/android/java/src/org/chromium/net/urlconnection/CronetOutputStream.java
@@ -0,0 +1,113 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.net.urlconnection;
+
+import org.chromium.net.UploadDataProvider;
+import org.chromium.net.UploadDataSink;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.ByteBuffer;
+
+/**
+ * An implementation of {@link java.io.OutputStream} to send data to a server,
+ * when {@link CronetHttpURLConnection#setFixedLengthStreamingMode} is used.
+ * This implementation does not buffer entire request body in memory.
pauljensen 2015/03/10 17:31:06 What threads do we expect this to be called from?
xunjieli 2015/03/12 21:55:10 I guess I was assuming that writes happen on the t
+ */
+final class CronetOutputStream extends OutputStream implements UploadDataProvider {
+ // CronetOutputStream buffers up to this value and wait for UploadDataStream
+ // to consume the data.
+ // FIXME: confirm this number is what we want to buffer internally.
pauljensen 2015/03/10 17:31:06 can we either resolve this FIXME now or change it
xunjieli 2015/03/12 21:55:10 Done.
+ private static int sDefaultBufferLength = 2048;
+ private final CronetHttpURLConnection mConnection;
+ private final long mContentLength;
+ private final ByteBuffer mBuffer;
+ private long mBytesReceived;
+
+ /**
+ * Packaged protected constructor.
+ * @param connection The CronetHttpURLConnection object.
+ * @param contentLength The content length of the request body. Non-zero for
+ * non-chunked upload.
+ */
+ CronetOutputStream(CronetHttpURLConnection connection, long contentLength) {
+ if (connection == null) {
+ throw new NullPointerException();
+ }
+ if (contentLength < 0) {
+ throw new IllegalArgumentException(
+ "Content length must be larger than 0 for non-chunked upload.");
+ }
+ mContentLength = contentLength;
+ int bufferSize = (int) Math.min(mContentLength, sDefaultBufferLength);
+ mBuffer = ByteBuffer.allocate(bufferSize);
+ mConnection = connection;
+ mBytesReceived = 0;
+ }
+
+ @Override
+ public long getLength() {
+ return mContentLength;
+ }
+
+ @Override
+ public void read(UploadDataSink uploadDataSink, ByteBuffer byteBuffer) {
+ mBuffer.flip();
+ byteBuffer.put(mBuffer);
pauljensen 2015/03/10 17:31:06 how do we know this won't overflow? can we add a
xunjieli 2015/03/12 21:55:10 Sorry, I think there's a risk of overflowing. Don
+ // Reuse this buffer.
+ mBuffer.clear();
+ uploadDataSink.onReadSucceeded(false);
+ // Wait for more data if not enough data is received.
+ if (mBytesReceived < mContentLength) {
+ mConnection.waitForPostData();
+ }
+ }
+
+ @Override
+ public void rewind(UploadDataSink uploadDataSink) {
+ uploadDataSink.onRewindError(null);
+ }
+
+ @Override
+ public void write(int oneByte) throws IOException {
+ if (mBuffer.position() == mBuffer.limit()) {
+ // Wait until buffer is consumed.
+ mConnection.waitForPostDataConsumed();
+ }
+ mBuffer.put((byte) oneByte);
+ mBytesReceived++;
+ if (mBytesReceived == mContentLength) {
+ // Entire post data has been received. Now wait for network stack to
+ // consume it.
+ mConnection.waitForPostDataConsumed();
+ }
+ }
+
+ @Override
+ public void write(byte[] buffer, int offset, int count) throws IOException {
+ if (buffer.length - offset < count || offset < 0 || count < 0) {
+ throw new IndexOutOfBoundsException();
+ }
+ if (count == 0) {
+ return;
+ }
+ int toSent = count;
pauljensen 2015/03/10 17:31:06 how about renaming toSent to toSend
xunjieli 2015/03/12 21:55:10 Done.
+ while (toSent > 0) {
+ if (mBuffer.position() == mBuffer.limit()) {
+ // Wait until buffer is consumed.
+ mConnection.waitForPostDataConsumed();
+ }
+ int sent = Math.min(toSent, mBuffer.limit() - mBuffer.position());
+ mBuffer.put(buffer, offset + count - toSent, sent);
+ toSent -= sent;
+ }
+ mBytesReceived += count;
+ if (mBytesReceived == mContentLength) {
+ // Entire post data has been received. Now wait for network stack to
+ // consume it.
+ mConnection.waitForPostDataConsumed();
+ }
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698