| 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.net.UploadDataProvider; | 7 import org.chromium.net.UploadDataProvider; |
| 8 import org.chromium.net.UploadDataSink; | 8 import org.chromium.net.UploadDataSink; |
| 9 | 9 |
| 10 import java.io.IOException; | 10 import java.io.IOException; |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 | 139 |
| 140 private class UploadDataProviderImpl extends UploadDataProvider { | 140 private class UploadDataProviderImpl extends UploadDataProvider { |
| 141 @Override | 141 @Override |
| 142 public long getLength() { | 142 public long getLength() { |
| 143 // This method is supposed to be called just before starting the req
uest. | 143 // This method is supposed to be called just before starting the req
uest. |
| 144 // If content length is not initially passed in, the number of bytes | 144 // If content length is not initially passed in, the number of bytes |
| 145 // written will be used as the content length. | 145 // written will be used as the content length. |
| 146 // TODO(xunjieli): Think of a less fragile way, since getLength() ca
n be | 146 // TODO(xunjieli): Think of a less fragile way, since getLength() ca
n be |
| 147 // potentially called in other places in the future. | 147 // potentially called in other places in the future. |
| 148 if (mInitialContentLength == -1) { | 148 if (mInitialContentLength == -1) { |
| 149 return mBuffer.position(); | 149 // Account for the fact that setConnected() flip()s mBuffer. |
| 150 return mConnected ? mBuffer.limit() : mBuffer.position(); |
| 150 } | 151 } |
| 151 return mInitialContentLength; | 152 return mInitialContentLength; |
| 152 } | 153 } |
| 153 | 154 |
| 154 @Override | 155 @Override |
| 155 public void read(UploadDataSink uploadDataSink, ByteBuffer byteBuffer) { | 156 public void read(UploadDataSink uploadDataSink, ByteBuffer byteBuffer) { |
| 156 final int availableSpace = byteBuffer.remaining(); | 157 final int availableSpace = byteBuffer.remaining(); |
| 157 if (availableSpace < mBuffer.remaining()) { | 158 if (availableSpace < mBuffer.remaining()) { |
| 158 byteBuffer.put(mBuffer.array(), mBuffer.position(), availableSpa
ce); | 159 byteBuffer.put(mBuffer.array(), mBuffer.position(), availableSpa
ce); |
| 159 mBuffer.position(mBuffer.position() + availableSpace); | 160 mBuffer.position(mBuffer.position() + availableSpace); |
| 160 } else { | 161 } else { |
| 161 byteBuffer.put(mBuffer); | 162 byteBuffer.put(mBuffer); |
| 162 } | 163 } |
| 163 uploadDataSink.onReadSucceeded(false); | 164 uploadDataSink.onReadSucceeded(false); |
| 164 } | 165 } |
| 165 | 166 |
| 166 @Override | 167 @Override |
| 167 public void rewind(UploadDataSink uploadDataSink) { | 168 public void rewind(UploadDataSink uploadDataSink) { |
| 168 mBuffer.position(0); | 169 mBuffer.position(0); |
| 169 uploadDataSink.onRewindSucceeded(); | 170 uploadDataSink.onRewindSucceeded(); |
| 170 } | 171 } |
| 171 } | 172 } |
| 172 } | 173 } |
| OLD | NEW |