X-Git-Url: http://gitweb.fperrin.net/?a=blobdiff_plain;f=src%2Fcom%2Fhughes%2Fandroid%2Futil%2FPersistentObjectCache.java;h=388eb7acce1deee0a085c02a3d23742c2f3f9247;hb=3d72bc11d5ef9d58e62b5beb4e33a437da28d4b8;hp=a4b8cbfb746b27f0542cc2f2797a5a0e4418d6f2;hpb=4a0a832b09c2e1c189701b4b9e2529b37592e0b0;p=Dictionary.git diff --git a/src/com/hughes/android/util/PersistentObjectCache.java b/src/com/hughes/android/util/PersistentObjectCache.java index a4b8cbf..388eb7a 100644 --- a/src/com/hughes/android/util/PersistentObjectCache.java +++ b/src/com/hughes/android/util/PersistentObjectCache.java @@ -28,74 +28,77 @@ import java.util.Map; public class PersistentObjectCache { - private final File dir; - private final Map objects = new LinkedHashMap(); - - public synchronized T read(final String filename, final Class resultClass) { - try { - Object object = (objects.get(filename)); - if (object != null) { - return resultClass.cast(object); - } - Log.d(getClass().getSimpleName(), "Cache miss."); - final File src = new File(dir, filename); - if (!src.canRead()) { - Log.d(getClass().getSimpleName(), "File empty: " + src); - return null; - } - try { - final ObjectInputStream in = new ObjectInputStream(new FileInputStream(src)); - object = in.readObject(); - in.close(); - } catch (Exception e) { - Log.e(getClass().getSimpleName(), "Deserialization failed: " + src, e); - return null; - } - objects.put(filename, object); - return resultClass.cast(object); - } catch (ClassCastException e) { - return null; + private final File dir; + private final Map objects = new LinkedHashMap(); + + public synchronized T read(final String filename, final Class resultClass) { + try { + Object object = (objects.get(filename)); + if (object != null) { + return resultClass.cast(object); + } + Log.d(getClass().getSimpleName(), "Cache miss."); + final File src = new File(dir, filename); + if (!src.canRead()) { + Log.d(getClass().getSimpleName(), "File empty: " + src); + return null; + } + try { + final ObjectInputStream in = new ObjectInputStream(new FileInputStream(src)); + object = in.readObject(); + in.close(); + } catch (Exception e) { + Log.e(getClass().getSimpleName(), "Deserialization failed: " + src, e); + return null; + } + objects.put(filename, object); + return resultClass.cast(object); + } catch (ClassCastException e) { + return null; + } } - } - - public synchronized void write(final String filename, final Object object) { - objects.put(filename, object); - final File dest = new File(dir, filename); - try { - final ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(dest)); - out.writeObject(object); - out.close(); - } catch (Exception e) { - Log.e(getClass().getSimpleName(), "Serialization failed: " + dest, e); + + public synchronized void write(final String filename, final Object object) { + objects.put(filename, object); + final File dest = new File(dir, filename); + try { + final ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(dest)); + out.writeObject(object); + out.close(); + } catch (Exception e) { + Log.e(getClass().getSimpleName(), "Serialization failed: " + dest, e); + } } - } - private PersistentObjectCache(final Context context) { - final File filesDir = context.getFilesDir(); - dir = filesDir != null ? filesDir : Environment.getExternalStorageDirectory(); - if (dir == null) { - throw new RuntimeException("context.getFilesDir() == " + context.getFilesDir() + ", Environment.getExternalStorageDirectory()=" + Environment.getExternalStorageDirectory()); + private PersistentObjectCache(final Context context) { + final File filesDir = context.getFilesDir(); + dir = filesDir != null ? filesDir : Environment.getExternalStorageDirectory(); + if (dir == null) { + throw new RuntimeException("context.getFilesDir() == " + context.getFilesDir() + + ", Environment.getExternalStorageDirectory()=" + + Environment.getExternalStorageDirectory()); + } } - } - - public static synchronized PersistentObjectCache getInstance() { - if (instance == null) { - throw new RuntimeException("getInstance called before init."); + + public static synchronized PersistentObjectCache getInstance() { + if (instance == null) { + throw new RuntimeException("getInstance called before init."); + } + return instance; } - return instance; - } - public static synchronized PersistentObjectCache init(final Context context) { - if (instance == null) { - instance = new PersistentObjectCache(context); - } else { - if (!instance.dir.equals(context.getFilesDir())) { - throw new RuntimeException("File dir changed. old=" + instance.dir + ", new=" + context.getFilesDir()); + public static synchronized PersistentObjectCache init(final Context context) { + if (instance == null) { + instance = new PersistentObjectCache(context); + } else { + if (!instance.dir.equals(context.getFilesDir())) { + throw new RuntimeException("File dir changed. old=" + instance.dir + ", new=" + + context.getFilesDir()); + } } - } - return instance; - } - - private static PersistentObjectCache instance = null; + return instance; + } + + private static PersistentObjectCache instance = null; }