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.urlconnection; | 5 package org.chromium.net.urlconnection; |
6 | 6 |
7 import java.io.IOException; | 7 import java.io.IOException; |
8 import java.io.InputStream; | 8 import java.io.InputStream; |
9 import java.nio.ByteBuffer; | 9 import java.nio.ByteBuffer; |
10 | 10 |
11 /** | 11 /** |
12 * An InputStream that is used by {@link CronetHttpURLConnection} to request | 12 * An InputStream that is used by {@link CronetHttpURLConnection} to request |
13 * data from the network stack as needed. | 13 * data from the network stack as needed. |
14 */ | 14 */ |
15 class CronetInputStream extends InputStream { | 15 class CronetInputStream extends InputStream { |
16 private final CronetHttpURLConnection mHttpURLConnection; | 16 private final CronetHttpURLConnection mHttpURLConnection; |
17 // Indicates whether listener's onSucceeded or onFailed callback is invoked. | 17 // Indicates whether listener's onSucceeded or onFailed callback is invoked. |
18 private boolean mResponseDataCompleted; | 18 private boolean mResponseDataCompleted; |
19 private ByteBuffer mBuffer; | 19 private ByteBuffer mBuffer; |
20 private IOException mException; | |
20 | 21 |
21 private static final int READ_BUFFER_SIZE = 32 * 1024; | 22 private static final int READ_BUFFER_SIZE = 32 * 1024; |
22 | 23 |
23 /** | 24 /** |
24 * Constructs a CronetInputStream. | 25 * Constructs a CronetInputStream. |
25 * @param httpURLConnection the CronetHttpURLConnection that is associated | 26 * @param httpURLConnection the CronetHttpURLConnection that is associated |
26 * with this InputStream. | 27 * with this InputStream. |
27 */ | 28 */ |
28 public CronetInputStream(CronetHttpURLConnection httpURLConnection) { | 29 public CronetInputStream(CronetHttpURLConnection httpURLConnection) { |
29 mHttpURLConnection = httpURLConnection; | 30 mHttpURLConnection = httpURLConnection; |
(...skipping 21 matching lines...) Expand all Loading... | |
51 int bytesRead = Math.min(mBuffer.limit() - mBuffer.position(), byteC ount); | 52 int bytesRead = Math.min(mBuffer.limit() - mBuffer.position(), byteC ount); |
52 mBuffer.get(buffer, byteOffset, bytesRead); | 53 mBuffer.get(buffer, byteOffset, bytesRead); |
53 return bytesRead; | 54 return bytesRead; |
54 } | 55 } |
55 return -1; | 56 return -1; |
56 } | 57 } |
57 | 58 |
58 /** | 59 /** |
59 * Called by {@link CronetHttpURLConnection} to notify that the entire | 60 * Called by {@link CronetHttpURLConnection} to notify that the entire |
60 * response body has been read. | 61 * response body has been read. |
62 * @param exception if not {@code null}, it is the exception to throw when c aller | |
63 * tries to read more data than what is buffered. | |
pauljensen
2015/12/07 15:20:07
nit: I'm a little confused by the "more data than
xunjieli
2015/12/07 15:53:12
Done.
| |
61 */ | 64 */ |
62 void setResponseDataCompleted() { | 65 void setResponseDataCompleted(IOException exception) { |
66 mException = exception; | |
63 mResponseDataCompleted = true; | 67 mResponseDataCompleted = true; |
64 // Nothing else to read, so can free the buffer. | 68 // Nothing else to read, so can free the buffer. |
65 mBuffer = null; | 69 mBuffer = null; |
66 } | 70 } |
67 | 71 |
68 private void getMoreDataIfNeeded() throws IOException { | 72 private void getMoreDataIfNeeded() throws IOException { |
69 if (!mResponseDataCompleted && !hasUnreadData()) { | 73 if (mResponseDataCompleted) { |
74 if (mException != null) { | |
75 throw mException; | |
76 } | |
77 return; | |
78 } | |
79 if (!hasUnreadData()) { | |
70 // Allocate read buffer if needed. | 80 // Allocate read buffer if needed. |
71 if (mBuffer == null) { | 81 if (mBuffer == null) { |
72 mBuffer = ByteBuffer.allocateDirect(READ_BUFFER_SIZE); | 82 mBuffer = ByteBuffer.allocateDirect(READ_BUFFER_SIZE); |
73 } | 83 } |
74 mBuffer.clear(); | 84 mBuffer.clear(); |
75 | 85 |
76 // Requests more data from CronetHttpURLConnection. | 86 // Requests more data from CronetHttpURLConnection. |
77 mHttpURLConnection.getMoreData(mBuffer); | 87 mHttpURLConnection.getMoreData(mBuffer); |
88 if (mException != null) { | |
89 throw mException; | |
90 } | |
78 if (mBuffer != null) { | 91 if (mBuffer != null) { |
79 mBuffer.flip(); | 92 mBuffer.flip(); |
80 } | 93 } |
81 } | 94 } |
82 } | 95 } |
83 | 96 |
84 /** | 97 /** |
85 * Returns whether {@link #mBuffer} has unread data. | 98 * Returns whether {@link #mBuffer} has unread data. |
86 */ | 99 */ |
87 private boolean hasUnreadData() { | 100 private boolean hasUnreadData() { |
88 return mBuffer != null && mBuffer.hasRemaining(); | 101 return mBuffer != null && mBuffer.hasRemaining(); |
89 } | 102 } |
90 } | 103 } |
OLD | NEW |