package dream.yanhua.com.diskcache;

import android.text.TextUtils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;

import dream.yanhua.com.diskcache.impl.LruDiskCache;
import dream.yanhua.com.diskcache.naming.Md5Sha1FileNameGenerator;
import dream.yanhua.com.diskcache.utils.IoUtils;

class DiskCacheHelper {
    private static final boolean DEBUG = false;
    /**
     * 200M
     */
    private static final long CACHE_MAX_SIZE = 50 * 1024 * 1024;
    private static final int CACHE_MAX_COUNT = 1000;

    private LruDiskCache mDiskLruCache;

    private DiskCacheHelper(File cacheDir) {
        try {
            mDiskLruCache = new LruDiskCache(cacheDir, null, new Md5Sha1FileNameGenerator(), CACHE_MAX_SIZE, CACHE_MAX_COUNT);
        } catch (Exception e) {
            if (DEBUG) {
                e.printStackTrace();
            }
        }
    }

    public synchronized static DiskCacheHelper get(File cacheDir) {
        return new DiskCacheHelper(cacheDir);
    }

    public synchronized void put(final String key, final String value) {
        putString(key, value);
    }


    private void putString(final String key, final String value) {
        if (TextUtils.isEmpty(value)) {
            return;
        }
        if (mDiskLruCache != null) {
            ByteArrayInputStream inputStream = null;
            try {
                inputStream = new ByteArrayInputStream(value.getBytes("UTF-8"));
                mDiskLruCache.save(key, inputStream);
            } catch (Exception e) {
                if (DEBUG) {
                    e.printStackTrace();
                }
            } finally {
                IoUtils.closeSilently(inputStream);
            }
        }
    }

    public synchronized String getString(String key) {
        return get(key);
    }

    private String get(String key) {
        if (TextUtils.isEmpty(key)) {
            return null;
        }
        if (mDiskLruCache != null) {
            File file = mDiskLruCache.get(key);
            if (file != null) {
                FileInputStream fileInputStream = null;
                ByteArrayOutputStream outputStream = null;
                try {
                    fileInputStream = new FileInputStream(file);
                    outputStream = new ByteArrayOutputStream();
                    byte[] buffer = new byte[8 * 1024];
                    int read;
                    while ((read = fileInputStream.read(buffer)) > 0) {
                        outputStream.write(buffer, 0, read);
                    }
                    return outputStream.toString("UTF-8");
                } catch (Exception e) {
                    if (DEBUG) {
                        e.printStackTrace();
                    }
                } finally {
                    IoUtils.closeSilently(fileInputStream);
                    IoUtils.closeSilently(outputStream);
                }
            }
        }
        return null;
    }

    public synchronized void clear() {
        new Thread() {
            @Override
            public void run() {
                synchronized (DiskCacheHelper.class) {
                    if (mDiskLruCache != null) {
                        try {
                            mDiskLruCache.clear();
                        } catch (Exception e) {
                            if (DEBUG) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        }.start();
    }
}

@@ -34,40 +34,8 @@
 e;%0A%0A
-import android.text.TextUtils;%0A%0A
 impo
@@ -328,17 +328,67 @@
 oUtils;%0A
-%0A
+import dream.yanhua.com.diskcache.utils.L;%0A%0Apublic 
 class Di
@@ -465,16 +465,23 @@
 *%0A     *
+ %E9%BB%98%E8%AE%A4%E6%9C%80%E5%A4%A7%E7%BC%93%E5%AD%98
  200M%0A  
@@ -551,16 +551,54 @@
 * 1024;%0A
+    /**%0A     * %E9%BB%98%E8%AE%A4%E6%9C%80%E5%A4%A7%E7%BC%93%E5%AD%98key1000%E4%B8%AA%0A     */%0A
     priv
@@ -717,35 +717,248 @@
 er(File cacheDir
-) %7B
+, long cacheMaxSize, int cacheMaxCount) %7B%0A        if (cacheMaxSize %3C= 0) %7B%0A            cacheMaxSize = CACHE_MAX_SIZE;%0A        %7D%0A        if (cacheMaxCount %3C= 0) %7B%0A            cacheMaxCount = CACHE_MAX_COUNT;%0A        %7D
 %0A        try %7B%0A 
@@ -1053,39 +1053,35 @@
 (), 
-CACHE_MAX_SIZE, CACHE_MAX_COUNT
+cacheMaxSize, cacheMaxCount
 );%0A 
@@ -1291,36 +1291,214 @@
 urn 
-new DiskCacheHelper(cacheDir
+get(cacheDir, 0, 0);%0A    %7D%0A%0A    public synchronized static DiskCacheHelper get(File cacheDir, long cacheMaxSize, int cacheMaxCount) %7B%0A        return new DiskCacheHelper(cacheDir, cacheMaxSize, cacheMaxCount
 );%0A 
@@ -1690,34 +1690,24 @@
         if (
-TextUtils.
 isEmpty(valu
@@ -2356,18 +2356,8 @@
 if (
-TextUtils.
 isEm
@@ -4019,10 +4019,312 @@
 ;%0A    %7D%0A
-%7D
 %0A
+    private static boolean isEmpty(String str) %7B%0A        if (str == null %7C%7C str.length() == 0) %7B%0A            return true;%0A        %7D else %7B%0A            return false;%0A        %7D%0A    %7D%0A%0A    public static void enableLog(boolean enable) %7B%0A        L.writeDebugLogs(enable);%0A        L.writeLogs(enable);%0A    %7D%0A%7D

添加带缓存大小和缓存数量的构造方法 删除TextUtils的使用