package tk.wlemuel.cotable.cache;

import android.content.Context;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InvalidClassException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 * CacheManager
 *
 * @author Steve Lemuel
 * @version 0.1
 * @desc CacheManager
 * @created 2015/05/09
 * @updated 2015/05/09
 */
public class CacheManager {

    /**
     * Save the object
     *
     * @param ser serializable object
     * @param file cache file
     * @throws java.io.IOException IOException
     */
    @SuppressWarnings("JavaDoc")
    public static boolean saveObject(Context context, Serializable ser,
                                     String file) {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = context.openFileOutput(file, Context.MODE_PRIVATE);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(ser);
            oos.flush();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                if (oos != null) oos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (fos != null) fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * Read the object
     *
     * @param file cache file
     * @return serializable object
     * @throws java.io.IOException Exception
     */
    @SuppressWarnings("JavaDoc")
    public static Serializable readObject(Context context, String file) {
        if (!isExistDataCache(context, file))
            return null;
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = context.openFileInput(file);
            ois = new ObjectInputStream(fis);
            return (Serializable) ois.readObject();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
            // 反序列化失败 - 删除缓存文件
            if (e instanceof InvalidClassException) {
                File data = context.getFileStreamPath(file);
                //noinspection ResultOfMethodCallIgnored
                data.delete();
            }
        } finally {
            try {
                if (ois != null) ois.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (fis != null) fis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * Judge whether the cache file is readable.
     *
     * @param context context
     * @param cachefile cache file
     * @return true if the cache file is readable, false otherwise.
     */
    public static boolean isReadDataCache(Context context, String cachefile) {
        return readObject(context, cachefile) != null;
    }

    /**
     * Juget whether the cache file exists.
     *
     * @param context context
     * @param cachefile cache file
     * @return true if the cache data exists, false otherwise.
     */
    public static boolean isExistDataCache(Context context, String cachefile) {
        if (context == null)
            return false;
        boolean exist = false;
        File data = context.getFileStreamPath(cachefile);
        if (data.exists())
            exist = true;
        return exist;
    }
}

@@ -538,19 +538,53 @@
  @param 
-ser
+context context%0A     * @param ser    
  seriali
@@ -607,32 +607,35 @@
   * @param file 
+   
 cache file%0A     
@@ -1592,32 +1592,62 @@
 e object%0A     *%0A
+     * @param context context%0A
      * @param fi
@@ -1645,24 +1645,27 @@
 @param file 
+   
 cache file%0A 
@@ -2323,23 +2323,60 @@
  // 
-%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E5%A4%B1%E8%B4%A5 - %E5%88%A0%E9%99%A4%E7%BC%93%E5%AD%98%E6%96%87%E4%BB%B6
+Delete the cache file if the deserialization failed.
 %0A   
@@ -3027,32 +3027,34 @@
  @param context 
+  
 context%0A     * @
@@ -3348,32 +3348,32 @@
  exists.%0A     *%0A
-
      * @param co
@@ -3370,32 +3370,34 @@
  @param context 
+  
 context%0A     * @

Modify the comments