Skip to content

Commit b76b0f9

Browse files
committed
add isConnecionKeepAlive
to set whether http connecion is keep alive when get images from network
1 parent 9a1b77b commit b76b0f9

File tree

4 files changed

+118
-14
lines changed

4 files changed

+118
-14
lines changed

src/cn/trinea/android/common/service/impl/ImageCache.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,16 @@ public void setContext(Context context) {
229229
secondaryCache.setContext(context);
230230
}
231231

232+
@Override
233+
public boolean isConnecionKeepAlive() {
234+
return secondaryCache.isConnecionKeepAlive();
235+
}
236+
237+
@Override
238+
public void setConnecionKeepAlive(boolean isConnecionKeepAlive) {
239+
secondaryCache.setConnecionKeepAlive(isConnecionKeepAlive);
240+
}
241+
232242
/**
233243
* get cache folder path which be used when saving images, default is {@link #DEFAULT_CACHE_FOLDER}
234244
*

src/cn/trinea/android/common/service/impl/ImageMemoryCache.java

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,29 @@
5656
*/
5757
public class ImageMemoryCache extends PreloadDataCache<String, Drawable> {
5858

59-
private static final long serialVersionUID = 1L;
59+
private static final long serialVersionUID = 1L;
6060

61-
private static final String TAG = "ImageCache";
61+
private static final String TAG = "ImageCache";
6262

6363
/** callback interface after image get success **/
6464
private OnImageCallbackListener onImageCallbackListener;
6565
/** http read image time out, if less than 0, not set. default is not set **/
66-
private int httpReadTimeOut = -1;
66+
private int httpReadTimeOut = -1;
6767
/**
6868
* whether open waiting queue, default is true. If true, save all view waiting for image loaded, else only save the
6969
* newest one
7070
**/
71-
private boolean isOpenWaitingQueue = true;
71+
private boolean isOpenWaitingQueue = true;
72+
/** whether http connecion is keep alive **/
73+
private boolean isConnecionKeepAlive = true;
7274

7375
/** recommend default max cache size according to dalvik max memory **/
74-
public static final int DEFAULT_MAX_SIZE = getDefaultMaxSize();
76+
public static final int DEFAULT_MAX_SIZE = getDefaultMaxSize();
7577
/** image got success message what **/
76-
private static final int IMAGE_LOADED_WHAT = 1;
78+
private static final int IMAGE_LOADED_WHAT = 1;
7779

7880
/** thread pool whose wait for data got, attention, not the get data thread pool **/
79-
private transient ExecutorService threadPool = Executors.newFixedThreadPool(SystemUtils.DEFAULT_THREAD_POOL_SIZE);
81+
private transient ExecutorService threadPool = Executors.newFixedThreadPool(SystemUtils.DEFAULT_THREAD_POOL_SIZE);
8082
/**
8183
* key is image url, value is the newest view which waiting for image loaded, used when {@link #isOpenWaitingQueue}
8284
* is false
@@ -209,6 +211,29 @@ public void setOpenWaitingQueue(boolean isOpenWaitingQueue) {
209211
this.isOpenWaitingQueue = isOpenWaitingQueue;
210212
}
211213

214+
/**
215+
* get whether http connecion is keep alive
216+
*
217+
* @return the isConnecionKeepAlive
218+
*/
219+
public boolean isConnecionKeepAlive() {
220+
return isConnecionKeepAlive;
221+
}
222+
223+
/**
224+
* set whether http connecion is keep alive
225+
* <ul>
226+
* <li>if image is from the same server, isConnecionKeepAlive is set to true recommended, and this is the default
227+
* value</li>
228+
* <li>else if image is from the different server, isConnecionKeepAlive is set to false recommended</li>
229+
* </ul>
230+
*
231+
* @param isConnecionKeepAlive the isConnecionKeepAlive to set
232+
*/
233+
public void setConnecionKeepAlive(boolean isConnecionKeepAlive) {
234+
this.isConnecionKeepAlive = isConnecionKeepAlive;
235+
}
236+
212237
/**
213238
* <ul>
214239
* <li>Get data listener is {@link #getDefaultOnGetImageListener()}</li>
@@ -407,7 +432,7 @@ public OnGetDataListener<String, Drawable> getDefaultOnGetImageListener() {
407432
public CacheObject<Drawable> onGetData(String key) {
408433
Drawable d = null;
409434
try {
410-
d = ImageUtils.getDrawableFromUrl(key, httpReadTimeOut);
435+
d = ImageUtils.getDrawableFromUrl(key, httpReadTimeOut, isConnecionKeepAlive);
411436
} catch (Exception e) {
412437
Log.e(TAG, "get drawable exception, imageUrl is:" + key, e);
413438
}

src/cn/trinea/android/common/service/impl/ImageSDCardCache.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ public class ImageSDCardCache extends PreloadDataCache<String, String> {
8484
* newest one
8585
**/
8686
private boolean isOpenWaitingQueue = true;
87+
/** whether http connecion is keep alive **/
88+
private boolean isConnecionKeepAlive = true;
8789

8890
/** recommend default max cache size according to dalvik max memory **/
8991
public static final int DEFAULT_MAX_SIZE = getDefaultMaxSize();
@@ -277,6 +279,29 @@ public void setOpenWaitingQueue(boolean isOpenWaitingQueue) {
277279
this.isOpenWaitingQueue = isOpenWaitingQueue;
278280
}
279281

282+
/**
283+
* get whether http connecion is keep alive
284+
*
285+
* @return the isConnecionKeepAlive
286+
*/
287+
public boolean isConnecionKeepAlive() {
288+
return isConnecionKeepAlive;
289+
}
290+
291+
/**
292+
* set whether http connecion is keep alive
293+
* <ul>
294+
* <li>if image is from the same server, isConnecionKeepAlive is set to true recommended, and this is the default
295+
* value</li>
296+
* <li>else if image is from the different server, isConnecionKeepAlive is set to false recommended</li>
297+
* </ul>
298+
*
299+
* @param isConnecionKeepAlive the isConnecionKeepAlive to set
300+
*/
301+
public void setConnecionKeepAlive(boolean isConnecionKeepAlive) {
302+
this.isConnecionKeepAlive = isConnecionKeepAlive;
303+
}
304+
280305
/**
281306
* <ul>
282307
* <li>Get data listener is {@link #getDefaultOnGetImageListener()}</li>
@@ -678,7 +703,7 @@ public CacheObject<String> onGetData(String key) {
678703

679704
String savePath = null;
680705
try {
681-
InputStream stream = ImageUtils.getInputStreamFromUrl(key, httpReadTimeOut);
706+
InputStream stream = ImageUtils.getInputStreamFromUrl(key, httpReadTimeOut, isConnecionKeepAlive);
682707
if (stream != null) {
683708
savePath = cacheFolder + File.separator + fileNameRule.getFileName(key);
684709
try {

src/cn/trinea/android/common/util/ImageUtils.java

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,38 @@ public static Drawable byteToDrawable(byte[] b) {
106106
return bitmapToDrawable(byteToBitmap(b));
107107
}
108108

109+
/**
110+
* get input stream from network by imageurl, and http conection is keep alive, you need to close inputStream
111+
* yourself
112+
*
113+
* @param imageUrl
114+
* @param readTimeOutMillis
115+
* @return
116+
* @see ImageUtils#getInputStreamFromUrl(String, int, boolean)
117+
*/
118+
public static InputStream getInputStreamFromUrl(String imageUrl, int readTimeOutMillis) {
119+
return getInputStreamFromUrl(imageUrl, readTimeOutMillis, true);
120+
}
121+
109122
/**
110123
* get input stream from network by imageurl, you need to close inputStream yourself
111124
*
112125
* @param imageUrl
113126
* @param readTimeOutMillis read time out, if less than 0, not set, in mills
127+
* @param isConnecionKeepAlive whether connection keep alive
114128
* @return
115129
* @throws MalformedURLException
116130
* @throws IOException
117131
*/
118-
public static InputStream getInputStreamFromUrl(String imageUrl, int readTimeOutMillis) {
132+
public static InputStream getInputStreamFromUrl(String imageUrl, int readTimeOutMillis, boolean isConnecionKeepAlive) {
119133
InputStream stream = null;
120134
try {
121135
URL url = new URL(imageUrl);
122136
HttpURLConnection con = (HttpURLConnection)url.openConnection();
137+
// default is keep alive
138+
if (!isConnecionKeepAlive) {
139+
con.addRequestProperty("Connection", "false");
140+
}
123141
if (readTimeOutMillis > 0) {
124142
con.setReadTimeout(readTimeOutMillis);
125143
}
@@ -134,28 +152,54 @@ public static InputStream getInputStreamFromUrl(String imageUrl, int readTimeOut
134152
return stream;
135153
}
136154

155+
/**
156+
* get drawable by imageUrl , and http conection is keep alive
157+
*
158+
* @param imageUrl
159+
* @param readTimeOutMillis
160+
* @return
161+
* @see ImageUtils#getDrawableFromUrl(String, int, boolean)
162+
*/
163+
public static Drawable getDrawableFromUrl(String imageUrl, int readTimeOutMillis) {
164+
return getDrawableFromUrl(imageUrl, readTimeOutMillis, true);
165+
}
166+
137167
/**
138168
* get drawable by imageUrl
139169
*
140170
* @param imageUrl
141171
* @param readTimeOutMillis read time out, if less than 0, not set, in mills
172+
* @param isConnecionKeepAlive whether connection keep alive
142173
* @return
143174
*/
144-
public static Drawable getDrawableFromUrl(String imageUrl, int readTimeOutMillis) {
145-
InputStream stream = getInputStreamFromUrl(imageUrl, readTimeOutMillis);
175+
public static Drawable getDrawableFromUrl(String imageUrl, int readTimeOutMillis, boolean isConnecionKeepAlive) {
176+
InputStream stream = getInputStreamFromUrl(imageUrl, readTimeOutMillis, isConnecionKeepAlive);
146177
Drawable d = Drawable.createFromStream(stream, "src");
147178
closeInputStream(stream);
148179
return d;
149180
}
150181

151182
/**
152-
* get Bitmap by imageUrl
183+
* get Bitmap by imageUrl, and http conection is keep alive
153184
*
154185
* @param imageUrl
186+
* @param readTimeOut
155187
* @return
188+
* @see ImageUtils#getBitmapFromUrl(String, int, boolean)
156189
*/
157190
public static Bitmap getBitmapFromUrl(String imageUrl, int readTimeOut) {
158-
InputStream stream = getInputStreamFromUrl(imageUrl, readTimeOut);
191+
return getBitmapFromUrl(imageUrl, readTimeOut, true);
192+
}
193+
194+
/**
195+
* get Bitmap by imageUrl
196+
*
197+
* @param imageUrl
198+
* @param isConnecionKeepAlive whether connection keep alive
199+
* @return
200+
*/
201+
public static Bitmap getBitmapFromUrl(String imageUrl, int readTimeOut, boolean isConnecionKeepAlive) {
202+
InputStream stream = getInputStreamFromUrl(imageUrl, readTimeOut, isConnecionKeepAlive);
159203
Bitmap b = BitmapFactory.decodeStream(stream);
160204
closeInputStream(stream);
161205
return b;

0 commit comments

Comments
 (0)