Chromium Code Reviews| Index: components/cronet/android/java/src/org/chromium/net/urlconnection/CronetFixedModeOutputStream.java |
| diff --git a/components/cronet/android/java/src/org/chromium/net/urlconnection/CronetFixedModeOutputStream.java b/components/cronet/android/java/src/org/chromium/net/urlconnection/CronetFixedModeOutputStream.java |
| index f6e005335ae4104e6bf1b8444324ce89999908fb..9f702f58944ee5699b81369c3289990850a8a819 100644 |
| --- a/components/cronet/android/java/src/org/chromium/net/urlconnection/CronetFixedModeOutputStream.java |
| +++ b/components/cronet/android/java/src/org/chromium/net/urlconnection/CronetFixedModeOutputStream.java |
| @@ -60,13 +60,15 @@ final class CronetFixedModeOutputStream extends CronetOutputStream { |
| @Override |
| public void write(int oneByte) throws IOException { |
| checkNotExceedContentLength(1); |
| - while (mBuffer.position() == mBuffer.limit()) { |
|
xunjieli
2016/06/10 00:10:48
I can't remember why I put "while" here. I will th
pauljensen
2016/06/10 13:27:39
instead of "position == limit" we should say hasRe
pauljensen
2016/06/10 13:27:39
Actually I like "while" more than "if" because it
xunjieli
2016/06/10 14:05:00
Done.
|
| + if (mBuffer.position() == mBuffer.limit()) { |
| + mBuffer.flip(); |
| // Wait until buffer is consumed. |
| mMessageLoop.loop(); |
| } |
|
pauljensen
2016/06/10 13:27:39
We should move this loop into a separate function
xunjieli
2016/06/10 14:05:00
Done.
|
| mBuffer.put((byte) oneByte); |
| mBytesWritten++; |
| if (mBytesWritten == mContentLength) { |
| + mBuffer.flip(); |
| // Entire post data has been received. Now wait for network stack to |
| // read it. |
| mMessageLoop.loop(); |
| @@ -85,6 +87,7 @@ final class CronetFixedModeOutputStream extends CronetOutputStream { |
| int toSend = count; |
| while (toSend > 0) { |
| if (mBuffer.position() == mBuffer.limit()) { |
| + mBuffer.flip(); |
| // Wait until buffer is consumed. |
| mMessageLoop.loop(); |
| } |
|
pauljensen
2016/06/10 13:27:39
This function seems like it could really use a sho
xunjieli
2016/06/10 14:05:00
Let's keep this as it is now. Swapping it for mBuf
|
| @@ -94,6 +97,7 @@ final class CronetFixedModeOutputStream extends CronetOutputStream { |
| } |
| mBytesWritten += count; |
| if (mBytesWritten == mContentLength) { |
| + mBuffer.flip(); |
| // Entire post data has been received. Now wait for network stack to |
| // read it. |
| mMessageLoop.loop(); |
| @@ -141,25 +145,20 @@ final class CronetFixedModeOutputStream extends CronetOutputStream { |
| @Override |
| public void read(final UploadDataSink uploadDataSink, final ByteBuffer byteBuffer) { |
| - final int availableSpace = byteBuffer.remaining(); |
| - if (availableSpace < mBuffer.position()) { |
| - // byteBuffer does not have enough capacity, so only put a portion |
| - // of mBuffer in it. |
| - byteBuffer.put(mBuffer.array(), 0, availableSpace); |
| - mBuffer.position(availableSpace); |
| - // 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 the content of mBuffer. |
| - mBuffer.flip(); |
| + if (byteBuffer.remaining() >= mBuffer.remaining()) { |
| byteBuffer.put(mBuffer); |
| // Reuse this buffer. |
| mBuffer.clear(); |
| + uploadDataSink.onReadSucceeded(false); |
| // Quit message loop so embedder can write more data. |
| mMessageLoop.quit(); |
| + } else { |
| + int oldLimit = mBuffer.limit(); |
| + mBuffer.limit(mBuffer.position() + byteBuffer.remaining()); |
| + byteBuffer.put(mBuffer); |
| + mBuffer.limit(oldLimit); |
| + uploadDataSink.onReadSucceeded(false); |
| } |
| - uploadDataSink.onReadSucceeded(false); |
| } |
| @Override |