Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.net.urlconnection; | |
| 6 | |
| 7 import org.chromium.net.UploadDataProvider; | |
| 8 import org.chromium.net.UploadDataSink; | |
| 9 | |
| 10 import java.io.ByteArrayOutputStream; | |
| 11 import java.io.IOException; | |
| 12 import java.io.OutputStream; | |
| 13 import java.nio.ByteBuffer; | |
| 14 | |
| 15 /** | |
| 16 * An implementation of {@link java.io.OutputStream} that buffers entire request | |
| 17 * body in memory. This is used when neither | |
| 18 * {@link CronetHttpURLConnection#setFixedLengthStreamingMode} | |
| 19 * or {@link CronetHttpURLConnection#setChunkedStreamingMode} is set. | |
| 20 */ | |
| 21 final class CronetBufferedOutputStream extends OutputStream | |
| 22 implements UploadDataProvider { | |
| 23 private final int mInitialContentLength; | |
| 24 private final CronetHttpURLConnection mConnection; | |
|
pauljensen
2015/03/04 16:44:10
I think this is dead if you agree with my logic on
xunjieli
2015/03/05 17:43:10
Sorry, getOutputStream() is not organized properly
| |
| 25 // Internal buffer that is used to buffer the request body. | |
| 26 private final ByteArrayOutputStream mBuffer; | |
| 27 // Number of bytes consumed by the native UploadDataStream. | |
| 28 private int mBytesConsumed; | |
| 29 | |
| 30 /** | |
| 31 * Packaged protected constructor. | |
| 32 * @param connection The CronetHttpURLConnection object. | |
| 33 * @param contentLength The content length of the request body. If content | |
| 34 * length cannot be determined in advance, use -1. | |
| 35 */ | |
| 36 CronetBufferedOutputStream(final CronetHttpURLConnection connection, | |
| 37 final long contentLength) { | |
| 38 if (connection == null) { | |
| 39 throw new NullPointerException(); | |
| 40 } | |
| 41 | |
| 42 if (contentLength > Integer.MAX_VALUE) { | |
| 43 throw new IllegalStateException("Use setFixedLengthStreamingMode()" | |
| 44 + " or setChunkedStreamingMode() for requests larger than 2GB.") ; | |
| 45 } | |
| 46 mConnection = connection; | |
| 47 mInitialContentLength = (int) contentLength; | |
| 48 if (mInitialContentLength == -1) { | |
| 49 // Bufferring without knowing content-length. | |
| 50 mBuffer = new ByteArrayOutputStream(); | |
| 51 } else { | |
| 52 mBuffer = new ByteArrayOutputStream(mInitialContentLength); | |
| 53 } | |
| 54 mBytesConsumed = 0; | |
| 55 } | |
| 56 | |
| 57 @Override | |
| 58 public void write(int oneByte) throws IOException { | |
| 59 mBuffer.write((byte) oneByte); | |
| 60 if (mBuffer.size() == mInitialContentLength) { | |
| 61 // Entire post data has been received. Now start the request. | |
| 62 mConnection.connect(); | |
|
pauljensen
2015/03/04 16:44:10
Ditto with line 74.
xunjieli
2015/03/05 17:43:10
above.
| |
| 63 } | |
| 64 } | |
| 65 | |
| 66 @Override | |
| 67 public void write(byte[] buffer, int offset, int count) throws IOException { | |
| 68 mBuffer.write(buffer, offset, count); | |
| 69 if (mInitialContentLength != -1 && mBuffer.size() > mInitialContentLengt h) { | |
| 70 throw new IllegalStateException("Writing out of bound."); | |
| 71 } | |
| 72 if (mBuffer.size() == mInitialContentLength) { | |
| 73 // Entire post data has been received. Now start the request. | |
| 74 mConnection.connect(); | |
|
pauljensen
2015/03/04 16:44:10
If getOutputStream() starts the request, and we kn
xunjieli
2015/03/05 17:43:10
above.
| |
| 75 } | |
| 76 } | |
| 77 | |
| 78 @Override | |
| 79 public long getLength() { | |
| 80 // This method is supposed to be called just before starting the request . | |
| 81 // If content length is not initially passed in, the number of bytes | |
| 82 // written will be used as the content length. | |
| 83 if (mInitialContentLength < 0) { | |
| 84 return mBuffer.size(); | |
|
pauljensen
2015/03/04 16:44:10
If getOutputStream() starts the request, which AFA
xunjieli
2015/03/05 17:43:10
above.
| |
| 85 } | |
| 86 return mInitialContentLength; | |
| 87 } | |
| 88 | |
| 89 @Override | |
| 90 public void read(UploadDataSink uploadDataSink, ByteBuffer byteBuffer) { | |
| 91 int toConsume = Math.min(byteBuffer.remaining(), | |
| 92 mBuffer.size() - mBytesConsumed); | |
| 93 if (toConsume > 0) { | |
| 94 byteBuffer.put(mBuffer.toByteArray(), mBytesConsumed, toConsume); | |
| 95 } | |
| 96 mBytesConsumed += toConsume; | |
| 97 uploadDataSink.onReadSucceeded(false); | |
| 98 } | |
| 99 | |
| 100 @Override | |
| 101 public void rewind(UploadDataSink uploadDataSink) { | |
| 102 mBytesConsumed = 0; | |
| 103 uploadDataSink.onRewindSucceeded(); | |
| 104 } | |
| 105 } | |
| OLD | NEW |