package com.fuying.sn.disklrucache; import android.content.Context; import android.graphics.Bitmap; import android.graphics.drawable.Drawable; import android.util.Log; import com.fuying.sn.utils.FileUtils; import com.jakewharton.disklrucache.DiskLruCache; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedWriter; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Serializable; import java.nio.charset.StandardCharsets; /** * 磁盘缓存帮助类 * https://www.cnblogs.com/aademeng/articles/6817058.html */ public class CacheHelper { private static final String TAG = "DiskLruCacheHelper"; private static final String DIR_NAME = "diskCache"; private static final int MAX_COUNT = 1024 * 1024 * 1024; private static final int DEFAULT_APP_VERSION = 1; private DiskLruCache mDiskLruCache; public CacheHelper(Context context) { mDiskLruCache = generateCache(context, DIR_NAME, MAX_COUNT); } public CacheHelper(Context context, String dirName) { mDiskLruCache = generateCache(context, dirName, MAX_COUNT); } public CacheHelper(Context context, String dirName, int maxCount) { mDiskLruCache = generateCache(context, dirName, maxCount); } //custom cache dir public CacheHelper(File dir) { mDiskLruCache = generateCache(null, dir, MAX_COUNT); } public CacheHelper(Context context, File dir) { mDiskLruCache = generateCache(context, dir, MAX_COUNT); } public CacheHelper(Context context, File dir, int maxCount) { mDiskLruCache = generateCache(context, dir, maxCount); } private DiskLruCache generateCache(Context context, File dir, int maxCount) { if (!dir.exists() || !dir.isDirectory()) { throw new IllegalArgumentException( dir + " is not a directory or does not exists. "); } int appVersion = context == null ? DEFAULT_APP_VERSION : Utils.getAppVersion(context); try { return DiskLruCache.open( dir, appVersion, DEFAULT_APP_VERSION, maxCount); } catch (IOException e) { e.printStackTrace(); } return null; } private DiskLruCache generateCache(Context context, String dirName, int maxCount) { try { return DiskLruCache.open( getDiskCacheDir(context, dirName), Utils.getAppVersion(context), DEFAULT_APP_VERSION, maxCount); } catch (IOException e) { e.printStackTrace(); } return null; } // ======================================= // ============== String 数据 读写 ============= // ======================================= public void put(String key, String value) { Log.e(TAG, "put: key = " + key + " value = " + value); DiskLruCache.Editor edit = null; BufferedWriter bw = null; try { edit = editor(key); if (edit == null) { return; } OutputStream os = edit.newOutputStream(0); bw = new BufferedWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8)); bw.write(value); edit.commit();//write CLEAN } catch (IOException e) { e.printStackTrace(); Log.e(TAG, "put: " + e.getMessage()); try { if (edit != null) { edit.abort();//write REMOVE } } catch (IOException e1) { e1.printStackTrace(); Log.e(TAG, "put: " + e1.getMessage()); } } finally { try { if (bw != null) { bw.close(); } } catch (IOException e) { e.printStackTrace(); Log.e(TAG, "put: " + e.getMessage()); } } } public String getAsString(String key) { Log.e(TAG, "getAsString: " + key); InputStream inputStream = null; try { inputStream = get(key); if (inputStream == null) { return null; } StringBuilder sb = new StringBuilder(); InputStreamReader isr = new InputStreamReader(inputStream, StandardCharsets.UTF_8); char[] buf = new char[1024]; int len; while ((len = isr.read(buf)) != -1) { sb.append(buf, 0, len); } return sb.toString(); } catch (IOException e) { e.printStackTrace(); Log.e(TAG, "getAsString: " + e.getMessage()); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; } public void put(String key, JSONObject jsonObject) { put(key, jsonObject.toString()); } public JSONObject getAsJson(String key) { String val = getAsString(key); try { if (val != null) { return new JSONObject(val); } } catch (JSONException e) { e.printStackTrace(); } return null; } // ======================================= // ============ JSONArray 数据 读写 ============= // ======================================= public void put(String key, JSONArray jsonArray) { put(key, jsonArray.toString()); } public JSONArray getAsJSONArray(String key) { String JSONString = getAsString(key); if (JSONString == null) return null; try { return new JSONArray(JSONString); } catch (Exception e) { e.printStackTrace(); return null; } } // ======================================= // ============== byte 数据 读写 ============= // ======================================= /** * 保存 byte数据 到 缓存中 * * @param key 保存的key * @param value 保存的数据 */ public void put(String key, byte[] value) { OutputStream out = null; DiskLruCache.Editor editor = null; try { editor = editor(key); if (editor == null) { return; } out = editor.newOutputStream(0); out.write(value); out.flush(); editor.commit();//write CLEAN } catch (Exception e) { e.printStackTrace(); try { if (editor != null) { editor.abort();//write REMOVE } } catch (IOException e1) { e1.printStackTrace(); } } finally { if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } } public byte[] getAsBytes(String key) { byte[] res = null; InputStream is = get(key); if (is == null) { return null; } try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buf = new byte[256]; int len = 0; while ((len = is.read(buf)) != -1) { baos.write(buf, 0, len); } res = baos.toByteArray(); } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return res; } // ======================================= // ============== 序列化 数据 读写 ============= // ======================================= public void put(String key, Serializable value) { DiskLruCache.Editor editor = editor(key); ObjectOutputStream oos = null; if (editor == null) { return; } try { OutputStream os = editor.newOutputStream(0); oos = new ObjectOutputStream(os); oos.writeObject(value); oos.flush(); editor.commit(); } catch (IOException e) { e.printStackTrace(); try { if (editor != null) { editor.abort(); } } catch (IOException e1) { e1.printStackTrace(); } } finally { try { if (oos != null) { oos.close(); } } catch (IOException e) { e.printStackTrace(); } } } public T getAsSerializable(String key) { T t = null; InputStream is = get(key); ObjectInputStream ois = null; if (is == null) { return null; } try { ois = new ObjectInputStream(is); t = (T) ois.readObject(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (ois != null) { ois.close(); } } catch (IOException e) { e.printStackTrace(); } } return t; } // ======================================= // ============== bitmap 数据 读写 ============= // ======================================= public void put(String key, Bitmap bitmap) { put(key, Utils.bitmap2Bytes(bitmap)); } public Bitmap getAsBitmap(String key) { byte[] bytes = getAsBytes(key); if (bytes == null) { return null; } return Utils.bytes2Bitmap(bytes); } // ======================================= // ============= drawable 数据 读写 ============= // ======================================= public void put(String key, Drawable value) { put(key, Utils.drawable2Bitmap(value)); } public Drawable getAsDrawable(String key) { byte[] bytes = getAsBytes(key); if (bytes == null) { return null; } return Utils.bitmap2Drawable(Utils.bytes2Bitmap(bytes)); } // ======================================= // ============= other methods ============= // ======================================= public boolean remove(String key) { try { String hashKey = Utils.hashKeyForDisk(key); return mDiskLruCache.remove(hashKey); } catch (IOException e) { e.printStackTrace(); } return false; } public void close() throws IOException { mDiskLruCache.close(); } public void delete() throws IOException { mDiskLruCache.delete(); } public void flush() throws IOException { mDiskLruCache.flush(); } public boolean isClosed() { return mDiskLruCache.isClosed(); } public long size() { return mDiskLruCache.size(); } public void setMaxSize(long maxSize) { mDiskLruCache.setMaxSize(maxSize); } public File getDirectory() { return mDiskLruCache.getDirectory(); } public long getMaxSize() { return mDiskLruCache.getMaxSize(); } // ======================================= // ===遇到文件比较大的,可以直接通过流读写 ===== // ======================================= //basic editor public DiskLruCache.Editor editor(String key) { try { String hashKey = Utils.hashKeyForDisk(key); //wirte DIRTY DiskLruCache.Editor edit = mDiskLruCache.edit(hashKey); //edit maybe null :the entry is editing if (edit == null) { Log.w(TAG, "the entry spcified key:" + hashKey + " is editing by other . "); } return edit; } catch (IOException e) { e.printStackTrace(); Log.e(TAG, "editor: " + e.getMessage()); } return null; } //basic get public InputStream get(String key) { try { DiskLruCache.Snapshot snapshot = mDiskLruCache.get(Utils.hashKeyForDisk(key)); if (snapshot == null) //not find entry , or entry.readable = false { Log.e(TAG, "not find entry , or entry.readable = false"); return null; } //write READ return snapshot.getInputStream(0); } catch (IOException e) { e.printStackTrace(); return null; } } // ======================================= // ============== 序列化 数据 读写 ============= // ======================================= private File getDiskCacheDir(Context context, String uniqueName) { // 与 FileUtils 共用同一套缓存目录选择逻辑,避免两处判定不一致 return new File(FileUtils.getCacheFileDir(context), uniqueName); } }