@@ -69,20 +69,15 @@ public class BaseImageDownloader implements ImageDownloader {
69
69
protected final Context context ;
70
70
protected final int connectTimeout ;
71
71
protected final int readTimeout ;
72
- /**
73
- * Whether read stream if server returned non-200 response
74
- */
75
- protected final boolean readOnError ;
76
72
77
73
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 );
79
75
}
80
76
81
- public BaseImageDownloader (Context context , int connectTimeout , int readTimeout , boolean readOnError ) {
77
+ public BaseImageDownloader (Context context , int connectTimeout , int readTimeout ) {
82
78
this .context = context .getApplicationContext ();
83
79
this .connectTimeout = connectTimeout ;
84
80
this .readTimeout = readTimeout ;
85
- this .readOnError = readOnError ;
86
81
}
87
82
88
83
@ Override
@@ -126,18 +121,30 @@ protected InputStream getStreamFromNetwork(String imageUri, Object extra) throws
126
121
127
122
InputStream imageStream ;
128
123
try {
129
- if (conn .getResponseCode () != 200 && !readOnError ) {
130
- throw new IOException ("Unable to retrieve image. Response code: " + conn .getResponseCode ());
131
- }
132
124
imageStream = conn .getInputStream ();
133
125
} catch (IOException e ) {
134
126
// Read all data to allow reuse connection (http://bit.ly/1ad35PY)
135
127
IoUtils .readAndCloseStream (conn .getErrorStream ());
136
128
throw e ;
137
129
}
130
+ if (!shouldBeProcessed (conn )) {
131
+ IoUtils .closeSilently (imageStream );
132
+ throw new IOException ("Image request failed with response code " + conn .getResponseCode ());
133
+ }
134
+
138
135
return new ContentLengthInputStream (new BufferedInputStream (imageStream , BUFFER_SIZE ), conn .getContentLength ());
139
136
}
140
137
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
+
141
148
/**
142
149
* Create {@linkplain HttpURLConnection HTTP connection} for incoming URL
143
150
*
0 commit comments