Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.net.urlconnection; | 5 package org.chromium.net.urlconnection; |
| 6 | 6 |
| 7 import org.chromium.base.VisibleForTesting; | 7 import org.chromium.base.VisibleForTesting; |
| 8 import org.chromium.net.UploadDataProvider; | 8 import org.chromium.net.UploadDataProvider; |
| 9 import org.chromium.net.UploadDataSink; | 9 import org.chromium.net.UploadDataSink; |
| 10 | 10 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 53 int bufferSize = (int) Math.min(mContentLength, sDefaultBufferLength); | 53 int bufferSize = (int) Math.min(mContentLength, sDefaultBufferLength); |
| 54 mBuffer = ByteBuffer.allocate(bufferSize); | 54 mBuffer = ByteBuffer.allocate(bufferSize); |
| 55 mConnection = connection; | 55 mConnection = connection; |
| 56 mMessageLoop = messageLoop; | 56 mMessageLoop = messageLoop; |
| 57 mBytesWritten = 0; | 57 mBytesWritten = 0; |
| 58 } | 58 } |
| 59 | 59 |
| 60 @Override | 60 @Override |
| 61 public void write(int oneByte) throws IOException { | 61 public void write(int oneByte) throws IOException { |
| 62 checkNotExceedContentLength(1); | 62 checkNotExceedContentLength(1); |
| 63 while (mBuffer.position() == mBuffer.limit()) { | 63 if (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.
| |
| 64 mBuffer.flip(); | |
| 64 // Wait until buffer is consumed. | 65 // Wait until buffer is consumed. |
| 65 mMessageLoop.loop(); | 66 mMessageLoop.loop(); |
| 66 } | 67 } |
|
pauljensen
2016/06/10 13:27:39
We should move this loop into a separate function
xunjieli
2016/06/10 14:05:00
Done.
| |
| 67 mBuffer.put((byte) oneByte); | 68 mBuffer.put((byte) oneByte); |
| 68 mBytesWritten++; | 69 mBytesWritten++; |
| 69 if (mBytesWritten == mContentLength) { | 70 if (mBytesWritten == mContentLength) { |
| 71 mBuffer.flip(); | |
| 70 // Entire post data has been received. Now wait for network stack to | 72 // Entire post data has been received. Now wait for network stack to |
| 71 // read it. | 73 // read it. |
| 72 mMessageLoop.loop(); | 74 mMessageLoop.loop(); |
| 73 } | 75 } |
| 74 } | 76 } |
| 75 | 77 |
| 76 @Override | 78 @Override |
| 77 public void write(byte[] buffer, int offset, int count) throws IOException { | 79 public void write(byte[] buffer, int offset, int count) throws IOException { |
| 78 if (buffer.length - offset < count || offset < 0 || count < 0) { | 80 if (buffer.length - offset < count || offset < 0 || count < 0) { |
| 79 throw new IndexOutOfBoundsException(); | 81 throw new IndexOutOfBoundsException(); |
| 80 } | 82 } |
| 81 checkNotExceedContentLength(count); | 83 checkNotExceedContentLength(count); |
| 82 if (count == 0) { | 84 if (count == 0) { |
| 83 return; | 85 return; |
| 84 } | 86 } |
| 85 int toSend = count; | 87 int toSend = count; |
| 86 while (toSend > 0) { | 88 while (toSend > 0) { |
| 87 if (mBuffer.position() == mBuffer.limit()) { | 89 if (mBuffer.position() == mBuffer.limit()) { |
| 90 mBuffer.flip(); | |
| 88 // Wait until buffer is consumed. | 91 // Wait until buffer is consumed. |
| 89 mMessageLoop.loop(); | 92 mMessageLoop.loop(); |
| 90 } | 93 } |
|
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
| |
| 91 int sent = Math.min(toSend, mBuffer.limit() - mBuffer.position()); | 94 int sent = Math.min(toSend, mBuffer.limit() - mBuffer.position()); |
|
pauljensen
2016/06/10 13:27:39
limit-position should be remaining()
xunjieli
2016/06/10 14:05:00
Done.
| |
| 92 mBuffer.put(buffer, offset + count - toSend, sent); | 95 mBuffer.put(buffer, offset + count - toSend, sent); |
| 93 toSend -= sent; | 96 toSend -= sent; |
| 94 } | 97 } |
| 95 mBytesWritten += count; | 98 mBytesWritten += count; |
| 96 if (mBytesWritten == mContentLength) { | 99 if (mBytesWritten == mContentLength) { |
| 100 mBuffer.flip(); | |
| 97 // Entire post data has been received. Now wait for network stack to | 101 // Entire post data has been received. Now wait for network stack to |
| 98 // read it. | 102 // read it. |
| 99 mMessageLoop.loop(); | 103 mMessageLoop.loop(); |
| 100 } | 104 } |
| 101 } | 105 } |
| 102 | 106 |
| 103 /** | 107 /** |
| 104 * Throws {@link java.net.ProtocolException} if adding {@code numBytes} will | 108 * Throws {@link java.net.ProtocolException} if adding {@code numBytes} will |
| 105 * exceed content length. | 109 * exceed content length. |
| 106 */ | 110 */ |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 134 } | 138 } |
| 135 | 139 |
| 136 private class UploadDataProviderImpl extends UploadDataProvider { | 140 private class UploadDataProviderImpl extends UploadDataProvider { |
| 137 @Override | 141 @Override |
| 138 public long getLength() { | 142 public long getLength() { |
| 139 return mContentLength; | 143 return mContentLength; |
| 140 } | 144 } |
| 141 | 145 |
| 142 @Override | 146 @Override |
| 143 public void read(final UploadDataSink uploadDataSink, final ByteBuffer b yteBuffer) { | 147 public void read(final UploadDataSink uploadDataSink, final ByteBuffer b yteBuffer) { |
| 144 final int availableSpace = byteBuffer.remaining(); | 148 if (byteBuffer.remaining() >= mBuffer.remaining()) { |
| 145 if (availableSpace < mBuffer.position()) { | |
| 146 // byteBuffer does not have enough capacity, so only put a porti on | |
| 147 // of mBuffer in it. | |
| 148 byteBuffer.put(mBuffer.array(), 0, availableSpace); | |
| 149 mBuffer.position(availableSpace); | |
| 150 // Move remaining buffer to the head of the buffer for use in th e | |
| 151 // next read call. | |
| 152 mBuffer.compact(); | |
| 153 } else { | |
| 154 // byteBuffer has enough capacity to hold the content of mBuffer . | |
| 155 mBuffer.flip(); | |
| 156 byteBuffer.put(mBuffer); | 149 byteBuffer.put(mBuffer); |
| 157 // Reuse this buffer. | 150 // Reuse this buffer. |
| 158 mBuffer.clear(); | 151 mBuffer.clear(); |
| 152 uploadDataSink.onReadSucceeded(false); | |
| 159 // Quit message loop so embedder can write more data. | 153 // Quit message loop so embedder can write more data. |
| 160 mMessageLoop.quit(); | 154 mMessageLoop.quit(); |
| 155 } else { | |
| 156 int oldLimit = mBuffer.limit(); | |
| 157 mBuffer.limit(mBuffer.position() + byteBuffer.remaining()); | |
| 158 byteBuffer.put(mBuffer); | |
| 159 mBuffer.limit(oldLimit); | |
| 160 uploadDataSink.onReadSucceeded(false); | |
| 161 } | 161 } |
| 162 uploadDataSink.onReadSucceeded(false); | |
| 163 } | 162 } |
| 164 | 163 |
| 165 @Override | 164 @Override |
| 166 public void rewind(UploadDataSink uploadDataSink) { | 165 public void rewind(UploadDataSink uploadDataSink) { |
| 167 uploadDataSink.onRewindError( | 166 uploadDataSink.onRewindError( |
| 168 new HttpRetryException("Cannot retry streamed Http body", -1 )); | 167 new HttpRetryException("Cannot retry streamed Http body", -1 )); |
| 169 } | 168 } |
| 170 } | 169 } |
| 171 | 170 |
| 172 /** | 171 /** |
| 173 * Sets the default buffer length for use in tests. | 172 * Sets the default buffer length for use in tests. |
| 174 */ | 173 */ |
| 175 @VisibleForTesting | 174 @VisibleForTesting |
| 176 static void setDefaultBufferLengthForTesting(int length) { | 175 static void setDefaultBufferLengthForTesting(int length) { |
| 177 sDefaultBufferLength = length; | 176 sDefaultBufferLength = length; |
| 178 } | 177 } |
| 179 } | 178 } |
| OLD | NEW |