Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(631)

Unified Diff: components/cronet/android/java/src/org/chromium/net/urlconnection/CronetInputStream.java

Issue 972213002: [Cronet] Implement batch read in CronetInputStream (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed Matt's comments Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/cronet/android/java/src/org/chromium/net/urlconnection/CronetInputStream.java
diff --git a/components/cronet/android/java/src/org/chromium/net/urlconnection/CronetInputStream.java b/components/cronet/android/java/src/org/chromium/net/urlconnection/CronetInputStream.java
index fc72ab11b6c23360b846cca35160d633fce4667f..c5fff687942b56692249291c058a11566a43a03f 100644
--- a/components/cronet/android/java/src/org/chromium/net/urlconnection/CronetInputStream.java
+++ b/components/cronet/android/java/src/org/chromium/net/urlconnection/CronetInputStream.java
@@ -29,18 +29,49 @@ class CronetInputStream extends InputStream {
@Override
public int read() throws IOException {
- if (!mResponseDataCompleted
- && (mBuffer == null || !mBuffer.hasRemaining())) {
- // Requests more data from CronetHttpURLConnection.
- mBuffer = mHttpURLConnection.getMoreData();
- }
- if (mBuffer != null && mBuffer.hasRemaining()) {
+ getMoreDataIfNeeded();
+ if (hasUnreadData()) {
return mBuffer.get() & 0xFF;
}
return -1;
}
+ @Override
+ public int read(byte[] buffer, int byteOffset, int byteCount) throws IOException {
+ if (byteOffset < 0 || byteCount < 0 || byteOffset + byteCount > buffer.length) {
+ throw new IndexOutOfBoundsException();
+ }
+ if (byteCount == 0) {
+ return 0;
+ }
+ getMoreDataIfNeeded();
+ if (hasUnreadData()) {
+ int bytesRead = Math.min(mBuffer.limit() - mBuffer.position(), byteCount);
+ mBuffer.get(buffer, byteOffset, bytesRead);
+ return bytesRead;
+ }
+ return -1;
+ }
+
+ /**
+ * Called by {@link CronetHttpURLConnection} to notify that the entire
+ * response body has been read.
+ */
void setResponseDataCompleted() {
mResponseDataCompleted = true;
}
+
+ private void getMoreDataIfNeeded() throws IOException {
+ if (!mResponseDataCompleted && !hasUnreadData()) {
+ // Requests more data from CronetHttpURLConnection.
mmenke 2015/03/05 21:42:21 nit: Request? (-s) Inline code comments general
+ mBuffer = mHttpURLConnection.getMoreData();
+ }
+ }
+
+ /**
+ * Returns whether {@link #mBuffer} has unread data.
+ */
+ private boolean hasUnreadData() {
+ return mBuffer != null && mBuffer.hasRemaining();
+ }
}

Powered by Google App Engine
This is Rietveld 408576698