-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDiskLruCacheHelper.java
488 lines (462 loc) · 11 KB
/
DiskLruCacheHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
package com.jakewharton.disklrucache;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.text.TextUtils;
/**
* @author Rowandjj
*
*DiskLruCache的辅助工具类(DiskLruCache:http://jakewharton.github.io/DiskLruCache,https://github.com/JakeWharton/DiskLruCache)
* 提供创建/读取/写入缓存的方法,并支持异步缓存写入.
*/
public class DiskLruCacheHelper
{
private static final long DEFAULT_MAX_SIZE = 10*1024*1024;
private static ExecutorService service = null;
/**
*
* 创建DiskLruCache实例,默认版本号为当前应用版本号,缓存位置由getDiskCacheDir指定
* @param context 上下文
* @param cacheDirName 缓存文件夹名称
* @param maxSize 缓存最大值,单位是byte
* @return 创建成功返回DiskLruCache实例否则返回null
*/
public static DiskLruCache createCache(Context context,String cacheDirName,long maxSize)
{
DiskLruCache cache = null;
try
{
cache = DiskLruCache.open(getDiskCacheDir(cacheDirName, context), getAppVersion(context),1, maxSize);
} catch (IOException e)
{
e.printStackTrace();
}
return cache;
}
/**
* 返回一个具有默认大小的DiskLruCache实例,默认大小为10Mb
* @param context
* @param cacheDirName
* @return 创建成功返回DiskLruCache实例否则返回null
*/
public static DiskLruCache createCache(Context context,String cacheDirName)
{
return createCache(context, cacheDirName, DEFAULT_MAX_SIZE);
}
/**
* 将图片写入缓存
*/
public static boolean writeBitmapToCache(DiskLruCache cache,Bitmap bitmap,String url)
{
return writeBitmapToCache(cache, bitmap, url, CompressFormat.JPEG,100);
}
/**
* 异步地将图片写入缓存。将不会不会写入结果。
* @param cache
* @param bitmap
* @param url
*/
public static void asyncWriteBitmapToCache(final DiskLruCache cache,final Bitmap bitmap,final String url)
{
if(service == null)
service = Executors.newSingleThreadExecutor();
service.execute(new Runnable()
{
@Override
public void run()
{
writeBitmapToCache(cache, bitmap, url);
}
});
}
/**
* 将图片写入缓存
* @param cache 缓存对象
* @param bitmap 图片对象
* @param url 用于标识bitmap的唯一名称,通常为图片url
* @return true表示写入缓存成功否则为false
*/
public static boolean writeBitmapToCache(DiskLruCache cache,Bitmap bitmap,String url,CompressFormat format, int quality)
{
if(cache == null || bitmap == null || url == null || TextUtils.isEmpty(url))
return false;
try
{
DiskLruCache.Editor editor = cache.edit(generateKey(url));
if(editor != null)
{
OutputStream out = editor.newOutputStream(0);
if(bitmap.compress(format,quality, out))
{
editor.commit();
return true;
}
else
{
editor.abort();
}
}
} catch (IOException e)
{
e.printStackTrace();
}
return false;
}
/**
* 异步地将图片写入缓存。将不会不会写入结果。
*/
public static void asyncWriteBitmapToCache(final DiskLruCache cache,final Bitmap bitmap,final String url,final CompressFormat format, final int quality)
{
if(service == null)
service = Executors.newSingleThreadExecutor();
service.execute(new Runnable()
{
@Override
public void run()
{
writeBitmapToCache(cache, bitmap, url,format,quality);
}
});
}
/**
* 异步地将inputStram流写入缓存,将不会返回写入结果。
*/
public static void asyncWriteStreamToCache(final DiskLruCache cache,final InputStream in,final String url)
{
if(service == null)
service = Executors.newSingleThreadExecutor();
service.execute(new Runnable()
{
@Override
public void run()
{
asyncWriteStreamToCache(cache, in, url);
}
});
}
/**
* 将inputStram流写入缓存
* @param cache
* @param in
* @param url
* @return
*/
public static boolean writeStreamToCache(DiskLruCache cache,InputStream in,String url)
{
if(cache == null || in == null || url == null|| TextUtils.isEmpty(url))
return false;
DiskLruCache.Editor editor = null;
try
{
editor = cache.edit(generateKey(url));
if(editor != null)
{
OutputStream out = editor.newOutputStream(0);
BufferedInputStream bin = new BufferedInputStream(in);
byte[] buffer = new byte[1024];
int len = 0;
while((len = bin.read(buffer)) != -1)
{
out.write(buffer, 0, len);
out.flush();
}
editor.commit();
return true;
}
} catch (IOException e)
{
e.printStackTrace();
}
return false;
}
/**
* 异步地将文件写入缓存,将不会返回写入结果。
*/
public static void asyncWriteFileToCache(final DiskLruCache cache,final File file,final String url)
{
if(service == null)
service = Executors.newSingleThreadExecutor();
service.execute(new Runnable()
{
@Override
public void run()
{
writeFileToCache(cache, file, url);
}
});
}
/**
* 将文件写入缓存
* @return true表示写入成功否则写入失败
*/
public static boolean writeFileToCache(DiskLruCache cache,File file,String url)
{
if(cache == null || file == null || url == null || !file.exists() || TextUtils.isEmpty(url))
{
return false;
}
FileInputStream fin = null;
try
{
fin = new FileInputStream(file);
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
return writeStreamToCache(cache, fin, url);
}
/**
* 异步地将字符串写入缓存,将不会返回写入结果
*/
public static void asyncWriteStringToCache(final DiskLruCache cache,final String str,final String url)
{
if(service == null)
service = Executors.newSingleThreadExecutor();
service.execute(new Runnable()
{
@Override
public void run()
{
writeStringToCache(cache, str, url);
}
});
}
/**
* 将字符串写入缓存
* @param cache
* @param str
* @param url
* @return
*/
public static boolean writeStringToCache(DiskLruCache cache,String str,String url)
{
if(cache == null || str == null || url == null || TextUtils.isEmpty(url) || TextUtils.isEmpty(str))
{
return false;
}
DiskLruCache.Editor editor = null;
try
{
editor = cache.edit(generateKey(url));
if(editor != null)
{
OutputStream out = editor.newOutputStream(0);
out.write(str.getBytes());
out.flush();
}
editor.commit();
return true;
} catch (IOException e)
{
e.printStackTrace();
}
return false;
}
/**
* 停止内部正在写缓存的线程,
* 这将导致部分写缓存任务不能进行。
*/
public static void stop()
{
if(service != null)
service.shutdownNow();
}
/**
*
* 根据url获取缓存,并将结果以String形式返回
* @param cache
* @param url
* @return 成功则返回String否则返回null
*/
public static String readCacheToString(DiskLruCache cache,String url)
{
if(cache == null || url == null || TextUtils.isEmpty(url))
return null;
String key = generateKey(url);
DiskLruCache.Snapshot snapshot = null;
try
{
snapshot = cache.get(key);
if(snapshot != null)
{
InputStream in = snapshot.getInputStream(0);
StringBuilder builder = new StringBuilder(1024*2);
int len = 0;
byte[] buffer = new byte[1024];
while((len = in.read(buffer)) != -1)
{
builder.append(new String(buffer,0,len));
}
return builder.toString();
}
} catch (Exception e)
{
e.printStackTrace();
}
return null;
}
/**
* 根据url获取缓存,并将缓存以InputStream形式返回
*
* @param cache DiskLruCache实例
* @param url 缓存名
* @return 命中则返回InputStream流否则返回null
*/
public static InputStream readCacheToInputStream(DiskLruCache cache,String url)
{
if(cache == null || url == null || TextUtils.isEmpty(url))
return null;
String key = generateKey(url);
DiskLruCache.Snapshot snapshot = null;
try
{
snapshot = cache.get(key);
} catch (IOException e)
{
e.printStackTrace();
}
if(snapshot != null)
return snapshot.getInputStream(0);
return null;
}
/**
* 根据url获取缓存,并将缓存以Bitmap形式返回
* @param cache
* @param url
* @return 成功返回bitmap,否则返回null
*/
public static Bitmap readCacheToBitmap(DiskLruCache cache,String url)
{
InputStream in = readCacheToInputStream(cache, url);
if(in != null)
return BitmapFactory.decodeStream(in);
return null;
}
/**
* 获取缓存文件路径(优先选择sd卡)
* @param cacheDirName 缓存文件夹名称
* @param context 上下文
* @return
*/
public static File getDiskCacheDir(String cacheDirName,Context context)
{
String cacheDir;
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)&&!Environment.isExternalStorageRemovable())
{
cacheDir = getExternalCacheDir(context);
if(cacheDir == null)//部分机型返回了null
cacheDir = getInternalCacheDir(context);
}else
{
cacheDir = getInternalCacheDir(context);
}
File dir = new File(cacheDir,cacheDirName);
if(!dir.exists())
dir.mkdirs();
return dir;
}
/**
* 获取当前app版本号
* @param context 上下文
* @return 当前app版本号
*/
public static int getAppVersion(Context context)
{
PackageManager manager = context.getPackageManager();
int code = 1;
try
{
code = manager.getPackageInfo(context.getPackageName(),0).versionCode;
} catch (NameNotFoundException e)
{
e.printStackTrace();
}
return code;
}
/**
* 根据指定的url移除指定缓存
* Note:请不要是使用DiskLruCache.remove()
*
* @param cache
* @param url
* @return
*/
public static boolean remove(DiskLruCache cache,String url)
{
if(cache == null || url == null || TextUtils.isEmpty(url))
{
return false;
}
try
{
return cache.remove(generateKey(url));
} catch (IOException e)
{
e.printStackTrace();
}
return false;
}
/**
* 根据原始键生成新键,以保证键的名称的合法性
* @param key 原始键,通常是url
* @return
*/
public static String generateKey(String key)
{
String cacheKey;
try
{
MessageDigest digest = MessageDigest.getInstance("md5");
digest.update(key.getBytes());
cacheKey = bytesToHexString(digest.digest());
} catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
cacheKey = String.valueOf(key.hashCode());
}
return cacheKey;
}
private static String bytesToHexString(byte[] bytes)
{
StringBuilder builder = new StringBuilder();
for(int i = 0; i < bytes.length; i++)
{
String hex = Integer.toHexString(0xff&bytes[i]);
if(hex.length() == 1)
builder.append('0');
builder.append(hex);
}
return builder.toString();
}
private static String getExternalCacheDir(Context context)
{
File dir = context.getExternalCacheDir();
if(dir == null)
return null;
if(!dir.exists())
dir.mkdirs();
return dir.getPath();
}
private static String getInternalCacheDir(Context context)
{
File dir = context.getCacheDir();
if(!dir.exists())
dir.mkdirs();
return dir.getPath();
}
}