Chromium Code Reviews| 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..6abb1a37db837a380136a2f77638894e2b763b4a |
| --- /dev/null |
| +++ b/components/cronet/android/java/src/org/chromium/net/urlconnection/CronetOutputStream.java |
| @@ -0,0 +1,90 @@ |
| +// 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. |
| + */ |
| +final class CronetOutputStream extends OutputStream implements UploadDataProvider { |
| + // CronetUploadStream buffers up to this value and wait for UploadDataStream |
|
pauljensen
2015/03/04 16:44:11
What is CronetUploadStream? Do you mean CronetUpl
xunjieli
2015/03/05 17:43:11
Done. Sorry, it should be CronetOutputStream.
|
| + // to consume the data. |
| + 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); |
| + // 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(); |
| + } |
| + } |
| + |
| + static void setDefaultBufferLengthForTesting(int bufferLength) { |
| + sDefaultBufferLength = bufferLength; |
| + } |
| +} |