 Chromium Code Reviews
 Chromium Code Reviews Issue 966743003:
  [Cronet] Implement getOutputStream in CronetHttpURLConnection  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@chunked_support
    
  
    Issue 966743003:
  [Cronet] Implement getOutputStream in CronetHttpURLConnection  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@chunked_support| 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..48e85016914e7a4b67fd322c573bae5f973201a5 | 
| --- /dev/null | 
| +++ b/components/cronet/android/java/src/org/chromium/net/urlconnection/CronetOutputStream.java | 
| @@ -0,0 +1,165 @@ | 
| +// 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.base.VisibleForTesting; | 
| +import org.chromium.net.UploadDataProvider; | 
| +import org.chromium.net.UploadDataSink; | 
| + | 
| +import java.io.IOException; | 
| +import java.io.OutputStream; | 
| +import java.net.ProtocolException; | 
| +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 the entire request body in memory. | 
| + * Note that {@link #write} should only be called from the thread on which the | 
| + * {@link #mConnection} is created. | 
| 
mmenke
2015/03/25 18:34:57
This should mention "fixedStreamingMode" or "fixed
 
xunjieli
2015/03/27 17:46:45
Done.
 | 
| + */ | 
| +final class CronetOutputStream extends OutputStream implements UploadDataProvider { | 
| 
mmenke
2015/03/25 18:34:57
Maybe rename to something like CronetFixedModeOutp
 
xunjieli
2015/03/27 17:46:45
Done.
 | 
| + // CronetOutputStream buffers up to this value and wait for UploadDataStream | 
| + // to consume the data. This field is non-final, so it can be changed for tests. | 
| + // TODO(xunjieli): figure out whether this default value should be changed. | 
| + @VisibleForTesting | 
| + private static int sDefaultBufferLength = 2048; | 
| + private final CronetHttpURLConnection mConnection; | 
| + private final long mContentLength; | 
| + private final ByteBuffer mBuffer; | 
| + private long mBytesReceived; | 
| + | 
| + /** | 
| + * Package 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 void write(int oneByte) throws IOException { | 
| + checkNotExceedContentLength(1); | 
| + while (mBuffer.position() == mBuffer.limit()) { | 
| + // Wait until buffer is consumed. | 
| + mConnection.waitForRead(); | 
| + } | 
| + mBuffer.put((byte) oneByte); | 
| + mBytesReceived++; | 
| + if (mBytesReceived == mContentLength) { | 
| + // Entire post data has been received. Now wait for network stack to | 
| + // read it. | 
| + mConnection.waitForRead(); | 
| + } | 
| + } | 
| + | 
| + @Override | 
| + public void write(byte[] buffer, int offset, int count) throws IOException { | 
| + if (buffer.length - offset < count || offset < 0 || count < 0) { | 
| + throw new IndexOutOfBoundsException(); | 
| + } | 
| + checkNotExceedContentLength(count); | 
| + if (count == 0) { | 
| + return; | 
| + } | 
| + int toSend = count; | 
| + while (toSend > 0) { | 
| + if (mBuffer.position() == mBuffer.limit()) { | 
| + // Wait until buffer is consumed. | 
| + mConnection.waitForRead(); | 
| + } | 
| + int sent = Math.min(toSend, mBuffer.limit() - mBuffer.position()); | 
| + mBuffer.put(buffer, offset + count - toSend, sent); | 
| + toSend -= sent; | 
| + } | 
| + mBytesReceived += count; | 
| + if (mBytesReceived == mContentLength) { | 
| + // Entire post data has been received. Now wait for network stack to | 
| + // read it. | 
| + mConnection.waitForRead(); | 
| + } | 
| + } | 
| + | 
| + // TODO(xunjieli): implement close(). | 
| + | 
| + /** | 
| + * Throws {@link java.net.ProtocolException} if adding {@code numBytes} will | 
| + * exceed content length. | 
| + */ | 
| + private void checkNotExceedContentLength(int numBytes) throws ProtocolException { | 
| + if (mContentLength != -1 && mBytesReceived + numBytes > mContentLength) { | 
| + throw new ProtocolException("expected " | 
| + + (mContentLength - mBytesReceived) + " bytes but received " | 
| + + numBytes); | 
| + } | 
| + } | 
| + | 
| + // Below are UploadDataProvider implementations. Only intended to be used | 
| + // within Cronet. | 
| + | 
| + @Override | 
| + public long getLength() { | 
| + return mContentLength; | 
| + } | 
| + | 
| + @Override | 
| + public void read(final UploadDataSink uploadDataSink, final ByteBuffer byteBuffer) { | 
| + Runnable readTask = new Runnable() { | 
| 
mmenke
2015/03/25 18:34:57
Can't we just inline this instead of making it a t
 
xunjieli
2015/03/27 17:46:45
Done. I ran into problem with loop() being invoked
 | 
| + @Override | 
| + public void run() { | 
| + if (byteBuffer.remaining() < mBuffer.position()) { | 
| + // byteBuffer does not have enough capacity, so only put a portion | 
| + // of mBuffer in it. | 
| + mBuffer.position(byteBuffer.remaining()); | 
| + byteBuffer.put(mBuffer.array(), 0, byteBuffer.remaining()); | 
| + // Move remaining buffer to the head of the buffer for use in the | 
| + // next read call. | 
| + mBuffer.compact(); | 
| + } else { | 
| + // byteBuffer has enough capacity to hold content in mBuffer. | 
| + mBuffer.flip(); | 
| + byteBuffer.put(mBuffer); | 
| + // Reuse this buffer. | 
| + mBuffer.clear(); | 
| + } | 
| + uploadDataSink.onReadSucceeded(false); | 
| + } | 
| + }; | 
| + if (mBuffer.position() == 0) { | 
| + // Exit the message loop, so consumer can write more data. | 
| + if (mBytesReceived < mContentLength) { | 
| + mConnection.postponeRead(readTask); | 
| + } | 
| + } else { | 
| + readTask.run(); | 
| + } | 
| + } | 
| + | 
| + @Override | 
| + public void rewind(UploadDataSink uploadDataSink) { | 
| + uploadDataSink.onRewindError(null); | 
| 
mmenke
2015/03/25 18:34:57
Passing a null exception along seems like a bad id
 
xunjieli
2015/03/27 17:46:45
Done.
 | 
| + } | 
| + | 
| + /** | 
| + * Sets the default buffer length for use in tests. | 
| + */ | 
| + @VisibleForTesting | 
| + static void setDefaultBufferLengthForTesting(int length) { | 
| + sDefaultBufferLength = length; | 
| + } | 
| +} |