123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- package com.benyanyi.okhttp.util;
- import android.annotation.SuppressLint;
- import android.content.Context;
- import android.os.Environment;
- import com.benyanyi.okhttp.OkHttpUtil;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.FileReader;
- import java.io.OutputStreamWriter;
- /**
- * @author myLove
- */
- public final class CacheUtils {
- private static File realFile;
- @SuppressLint("StaticFieldLeak")
- private static Context mContext;
- @SuppressLint("StaticFieldLeak")
- private static CacheUtils instance;
- public static CacheUtils getInstance(Context context) {
- if (instance == null) {
- instance = new CacheUtils();
- mContext = context;
- }
- if (OkHttpUtil.cacheFile == null) {
- File dir = mContext.getExternalFilesDir(null);
- if (dir != null && !dir.exists() && Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
- dir.mkdirs();
- } else {
- dir = mContext.getFilesDir();
- if (!dir.exists()) {
- dir.mkdirs();
- }
- }
- realFile = dir;
- } else {
- realFile = OkHttpUtil.cacheFile;
- }
- return instance;
- }
- /**
- * 根据url的MD5作为文件名,进行缓存
- *
- * @param url 文件名
- * @param json
- */
- public void setCacheToLocalJson(String url, String json) {
- String urlMD5 = Md5keyUtil.newInstance().getKeyBeanOfStr(url);
- String path = realFile.getAbsolutePath() + "/" + urlMD5;
- try {
- File file = new File(path);
- if (file.exists()) {
- file.delete();
- }
- FileOutputStream fis = new FileOutputStream(file);
- BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fis));
- long currentTime = System.currentTimeMillis();
- bw.write(currentTime + "");
- bw.newLine();
- bw.write(json);
- bw.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * 根据缓存地址,从缓存中取出数据
- *
- * @param url
- * @return
- */
- public String getCacheToLocalJson(String url) {
- StringBuilder sb = new StringBuilder();
- String urlMD5 = Md5keyUtil.newInstance().getKeyBeanOfStr(url);
- // 创建缓存文件夹
- File file = new File(realFile, urlMD5);
- if (file.exists()) {
- try {
- FileReader fr = new FileReader(file);
- BufferedReader br = new BufferedReader(fr);
- br.readLine();
- String temp;
- while ((temp = br.readLine()) != null) {
- sb.append(temp);
- }
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- return sb.toString();
- }
- }
|