OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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; | 5 package org.chromium.net; |
6 | 6 |
7 import android.os.SystemClock; | 7 import android.os.SystemClock; |
8 import android.support.annotation.Nullable; | 8 import android.support.annotation.Nullable; |
9 import android.util.Log; | 9 import android.util.Log; |
10 | 10 |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 } | 249 } |
250 } | 250 } |
251 | 251 |
252 @Override | 252 @Override |
253 public void read(ByteBuffer buffer) { | 253 public void read(ByteBuffer buffer) { |
254 synchronized (mUrlRequestAdapterLock) { | 254 synchronized (mUrlRequestAdapterLock) { |
255 if (buffer.position() >= buffer.capacity()) { | 255 if (buffer.position() >= buffer.capacity()) { |
256 throw new IllegalArgumentException( | 256 throw new IllegalArgumentException( |
257 "ByteBuffer is already full."); | 257 "ByteBuffer is already full."); |
258 } | 258 } |
| 259 Preconditions.checkDirect(buffer); |
259 | 260 |
260 if (!mWaitingOnRead) { | 261 if (!mWaitingOnRead) { |
261 throw new IllegalStateException("Unexpected read attempt."); | 262 throw new IllegalStateException("Unexpected read attempt."); |
262 } | 263 } |
263 mWaitingOnRead = false; | 264 mWaitingOnRead = false; |
264 mLegacyReadByteBufferAdjustment = true; | 265 mLegacyReadByteBufferAdjustment = true; |
265 | 266 |
266 if (isDone()) { | 267 if (isDone()) { |
267 return; | 268 return; |
268 } | 269 } |
269 | 270 |
270 // Indicate buffer has no new data. This is primarily to make it | 271 // Indicate buffer has no new data. This is primarily to make it |
271 // clear the buffer has no data in the failure and completion cases. | 272 // clear the buffer has no data in the failure and completion cases. |
272 buffer.limit(buffer.position()); | 273 buffer.limit(buffer.position()); |
273 | 274 |
274 if (!nativeReadData(mUrlRequestAdapter, buffer, buffer.position(), | 275 if (!nativeReadData(mUrlRequestAdapter, buffer, buffer.position(), |
275 buffer.capacity())) { | 276 buffer.capacity())) { |
276 // Still waiting on read. This is just to have consistent | 277 // Still waiting on read. This is just to have consistent |
277 // behavior with the other error cases. | 278 // behavior with the other error cases. |
278 mWaitingOnRead = true; | 279 mWaitingOnRead = true; |
279 // Since accessing byteBuffer's memory failed, it's presumably | 280 throw new IllegalArgumentException("Unable to call native read")
; |
280 // not a direct ByteBuffer. | |
281 throw new IllegalArgumentException( | |
282 "byteBuffer must be a direct ByteBuffer."); | |
283 } | 281 } |
284 } | 282 } |
285 } | 283 } |
286 | 284 |
287 @Override | 285 @Override |
288 public void readNew(ByteBuffer buffer) { | 286 public void readNew(ByteBuffer buffer) { |
| 287 Preconditions.checkHasRemaining(buffer); |
| 288 Preconditions.checkDirect(buffer); |
289 synchronized (mUrlRequestAdapterLock) { | 289 synchronized (mUrlRequestAdapterLock) { |
290 if (!buffer.hasRemaining()) { | |
291 throw new IllegalArgumentException("ByteBuffer is already full."
); | |
292 } | |
293 | |
294 if (!mWaitingOnRead) { | 290 if (!mWaitingOnRead) { |
295 throw new IllegalStateException("Unexpected read attempt."); | 291 throw new IllegalStateException("Unexpected read attempt."); |
296 } | 292 } |
297 mWaitingOnRead = false; | 293 mWaitingOnRead = false; |
298 mLegacyReadByteBufferAdjustment = false; | 294 mLegacyReadByteBufferAdjustment = false; |
299 | 295 |
300 if (isDone()) { | 296 if (isDone()) { |
301 return; | 297 return; |
302 } | 298 } |
303 | 299 |
304 if (!nativeReadData(mUrlRequestAdapter, buffer, buffer.position(), b
uffer.limit())) { | 300 if (!nativeReadData(mUrlRequestAdapter, buffer, buffer.position(), b
uffer.limit())) { |
305 // Still waiting on read. This is just to have consistent | 301 // Still waiting on read. This is just to have consistent |
306 // behavior with the other error cases. | 302 // behavior with the other error cases. |
307 mWaitingOnRead = true; | 303 mWaitingOnRead = true; |
308 // Since accessing byteBuffer's memory failed, it's presumably | 304 throw new IllegalArgumentException("Unable to call native read")
; |
309 // not a direct ByteBuffer. | |
310 throw new IllegalArgumentException("byteBuffer must be a direct
ByteBuffer."); | |
311 } | 305 } |
312 } | 306 } |
313 } | 307 } |
314 | 308 |
315 @Override | 309 @Override |
316 public void cancel() { | 310 public void cancel() { |
317 synchronized (mUrlRequestAdapterLock) { | 311 synchronized (mUrlRequestAdapterLock) { |
318 if (isDone() || !mStarted) { | 312 if (isDone() || !mStarted) { |
319 return; | 313 return; |
320 } | 314 } |
(...skipping 464 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
785 | 779 |
786 @NativeClassQualifiedName("CronetURLRequestAdapter") | 780 @NativeClassQualifiedName("CronetURLRequestAdapter") |
787 private native String nativeGetProxyServer(long nativePtr); | 781 private native String nativeGetProxyServer(long nativePtr); |
788 | 782 |
789 @NativeClassQualifiedName("CronetURLRequestAdapter") | 783 @NativeClassQualifiedName("CronetURLRequestAdapter") |
790 private native void nativeGetStatus(long nativePtr, UrlRequest.StatusListene
r listener); | 784 private native void nativeGetStatus(long nativePtr, UrlRequest.StatusListene
r listener); |
791 | 785 |
792 @NativeClassQualifiedName("CronetURLRequestAdapter") | 786 @NativeClassQualifiedName("CronetURLRequestAdapter") |
793 private native boolean nativeGetWasCached(long nativePtr); | 787 private native boolean nativeGetWasCached(long nativePtr); |
794 } | 788 } |
OLD | NEW |