Skip to content

Commit

Permalink
Fix intermediate results propagation in NetworkFetchProducer
Browse files Browse the repository at this point in the history
  • Loading branch information
plamenko authored and tyronen committed Apr 6, 2015
1 parent 9ba338e commit a68a7b0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void onResponse(Response response) {
if (contentLength < 0) {
contentLength = 0;
}
processResult(requestState, body.byteStream(), (int) contentLength, false);
processResult(requestState, body.byteStream(), (int) contentLength);
} catch (IOException ioe) {
handleException(call, requestState, ioe);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void run() {
URL url = new URL(uri.toString());
connection = (HttpURLConnection) url.openConnection();
InputStream is = connection.getInputStream();
processResult(requestState, is, 0, false);
processResult(requestState, is, -1);
} catch (Exception e) {
onFailure(requestState, e, null);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,18 @@ protected abstract RS newRequestState(
*/
protected abstract void fetchImage(final RS requestState);

/**
* Subclasses should call this method once they receive the InputStream from their network stack.
*
* @param requestState Request-specific data.
* @param responseData The InputStream for the data
* @param responseContentLength the length of the data if known, -1 otherwise
* @throws IOException
*/
protected void processResult(
RS requestState,
InputStream responseData,
int responseContentLength,
boolean propagateIntermediateResults)
int responseContentLength)
throws IOException {
final PooledByteBufferOutputStream pooledOutputStream;
if (responseContentLength > 0) {
Expand All @@ -124,7 +131,7 @@ protected void processResult(
while ((length = responseData.read(ioArray)) >= 0) {
if (length > 0) {
pooledOutputStream.write(ioArray, 0, length);
if (propagateIntermediateResults) {
if (shouldPropagateIntermediateResults(requestState)) {
maybeHandleIntermediateResult(pooledOutputStream, requestState);
}
float progress = calculateProgress(pooledOutputStream.size(), responseContentLength);
Expand Down Expand Up @@ -242,4 +249,14 @@ protected void onCancellation(RS requestState, Map<String, String> extraMap) {
extraMap);
requestState.getConsumer().onCancellation();
}

/**
* Gets whether the intermediate results should be propagated.
*
* @param requestState Request-specific data.
* @return whether the intermediate results should be propagated
*/
protected boolean shouldPropagateIntermediateResults(RS requestState) {
return requestState.getContext().getImageRequest().getProgressiveRenderingEnabled();
}
}

0 comments on commit a68a7b0

Please sign in to comment.