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 |
(...skipping 11 matching lines...) Expand all Loading... | |
22 * Constructs a CronetInputStream. | 22 * Constructs a CronetInputStream. |
23 * @param httpURLConnection the CronetHttpURLConnection that is associated | 23 * @param httpURLConnection the CronetHttpURLConnection that is associated |
24 * with this InputStream. | 24 * with this InputStream. |
25 */ | 25 */ |
26 public CronetInputStream(CronetHttpURLConnection httpURLConnection) { | 26 public CronetInputStream(CronetHttpURLConnection httpURLConnection) { |
27 mHttpURLConnection = httpURLConnection; | 27 mHttpURLConnection = httpURLConnection; |
28 } | 28 } |
29 | 29 |
30 @Override | 30 @Override |
31 public int read() throws IOException { | 31 public int read() throws IOException { |
32 if (!mResponseDataCompleted | 32 getMoreDataIfNeeded(); |
33 && (mBuffer == null || !mBuffer.hasRemaining())) { | 33 if (hasUnreadData()) { |
34 // Requests more data from CronetHttpURLConnection. | |
35 mBuffer = mHttpURLConnection.getMoreData(); | |
36 } | |
37 if (mBuffer != null && mBuffer.hasRemaining()) { | |
38 return mBuffer.get() & 0xFF; | 34 return mBuffer.get() & 0xFF; |
39 } | 35 } |
40 return -1; | 36 return -1; |
41 } | 37 } |
42 | 38 |
39 @Override | |
40 public int read(byte[] buffer, int byteOffset, int byteCount) throws IOExcep tion { | |
41 if (byteOffset < 0 || byteCount < 0 || byteOffset + byteCount > buffer.l ength) { | |
42 throw new IndexOutOfBoundsException(); | |
43 } | |
44 if (byteCount == 0) { | |
45 return 0; | |
46 } | |
47 getMoreDataIfNeeded(); | |
48 if (hasUnreadData()) { | |
49 int bytesRead = Math.min(mBuffer.limit() - mBuffer.position(), byteC ount); | |
50 mBuffer.get(buffer, byteOffset, bytesRead); | |
51 return bytesRead; | |
52 } | |
53 return -1; | |
54 } | |
55 | |
56 /** | |
57 * Called by {@link CronetHttpURLConnection} to notify that the entire | |
58 * response body has been read. | |
59 */ | |
43 void setResponseDataCompleted() { | 60 void setResponseDataCompleted() { |
44 mResponseDataCompleted = true; | 61 mResponseDataCompleted = true; |
45 } | 62 } |
63 | |
64 private void getMoreDataIfNeeded() throws IOException { | |
65 if (!mResponseDataCompleted && !hasUnreadData()) { | |
66 // Requests more data from CronetHttpURLConnection. | |
mmenke
2015/03/05 21:42:21
nit: Request? (-s) Inline code comments general
| |
67 mBuffer = mHttpURLConnection.getMoreData(); | |
68 } | |
69 } | |
70 | |
71 /** | |
72 * Returns whether {@link #mBuffer} has unread data. | |
73 */ | |
74 private boolean hasUnreadData() { | |
75 return mBuffer != null && mBuffer.hasRemaining(); | |
76 } | |
46 } | 77 } |
OLD | NEW |