Skip to content

Commit eb794c3

Browse files
committed
Clean code
1 parent dae133f commit eb794c3

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

library/src/com/nostra13/universalimageloader/core/download/BaseImageDownloader.java

+17-10
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,15 @@ public class BaseImageDownloader implements ImageDownloader {
6969
protected final Context context;
7070
protected final int connectTimeout;
7171
protected final int readTimeout;
72-
/**
73-
* Whether read stream if server returned non-200 response
74-
*/
75-
protected final boolean readOnError;
7672

7773
public BaseImageDownloader(Context context) {
78-
this(context.getApplicationContext(), DEFAULT_HTTP_CONNECT_TIMEOUT, DEFAULT_HTTP_READ_TIMEOUT, false);
74+
this(context, DEFAULT_HTTP_CONNECT_TIMEOUT, DEFAULT_HTTP_READ_TIMEOUT);
7975
}
8076

81-
public BaseImageDownloader(Context context, int connectTimeout, int readTimeout, boolean readOnError) {
77+
public BaseImageDownloader(Context context, int connectTimeout, int readTimeout) {
8278
this.context = context.getApplicationContext();
8379
this.connectTimeout = connectTimeout;
8480
this.readTimeout = readTimeout;
85-
this.readOnError = readOnError;
8681
}
8782

8883
@Override
@@ -126,18 +121,30 @@ protected InputStream getStreamFromNetwork(String imageUri, Object extra) throws
126121

127122
InputStream imageStream;
128123
try {
129-
if (conn.getResponseCode() != 200 && !readOnError) {
130-
throw new IOException("Unable to retrieve image. Response code: " + conn.getResponseCode());
131-
}
132124
imageStream = conn.getInputStream();
133125
} catch (IOException e) {
134126
// Read all data to allow reuse connection (http://bit.ly/1ad35PY)
135127
IoUtils.readAndCloseStream(conn.getErrorStream());
136128
throw e;
137129
}
130+
if (!shouldBeProcessed(conn)) {
131+
IoUtils.closeSilently(imageStream);
132+
throw new IOException("Image request failed with response code " + conn.getResponseCode());
133+
}
134+
138135
return new ContentLengthInputStream(new BufferedInputStream(imageStream, BUFFER_SIZE), conn.getContentLength());
139136
}
140137

138+
/**
139+
* @param conn Opened request connection (response code is available)
140+
* @return <b>true</b> - if data from connection is correct and should be read and processed;
141+
* <b>false</b> - if response contains irrelevant data and shouldn't be processed
142+
* @throws IOException
143+
*/
144+
protected boolean shouldBeProcessed(HttpURLConnection conn) throws IOException {
145+
return conn.getResponseCode() == 200;
146+
}
147+
141148
/**
142149
* Create {@linkplain HttpURLConnection HTTP connection} for incoming URL
143150
*

0 commit comments

Comments
 (0)