1 // Copyright 2011 Google Inc. All Rights Reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 package com.hughes.android.util;
17 import android.content.Context;
18 import android.os.Environment;
19 import android.util.Log;
22 import java.io.FileInputStream;
23 import java.io.FileOutputStream;
24 import java.io.ObjectInputStream;
25 import java.io.ObjectOutputStream;
26 import java.util.LinkedHashMap;
29 public class PersistentObjectCache {
31 private final File dir;
32 private final Map<String, Object> objects = new LinkedHashMap<String, Object>();
34 public synchronized <T> T read(final String filename, final Class<T> resultClass) {
36 Object object = (objects.get(filename));
38 return resultClass.cast(object);
40 Log.d(getClass().getSimpleName(), "Cache miss.");
41 final File src = new File(dir, filename);
43 Log.d(getClass().getSimpleName(), "File empty: " + src);
47 final ObjectInputStream in = new ObjectInputStream(new FileInputStream(src));
48 object = in.readObject();
50 } catch (Exception e) {
51 Log.e(getClass().getSimpleName(), "Deserialization failed: " + src, e);
54 objects.put(filename, object);
55 return resultClass.cast(object);
56 } catch (ClassCastException e) {
61 public synchronized void write(final String filename, final Object object) {
62 objects.put(filename, object);
63 final File dest = new File(dir, filename);
65 final ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(dest));
66 out.writeObject(object);
68 } catch (Exception e) {
69 Log.e(getClass().getSimpleName(), "Serialization failed: " + dest, e);
73 private PersistentObjectCache(final Context context) {
74 final File filesDir = context.getFilesDir();
75 dir = filesDir != null ? filesDir : Environment.getExternalStorageDirectory();
77 throw new RuntimeException("context.getFilesDir() == " + context.getFilesDir()
78 + ", Environment.getExternalStorageDirectory()="
79 + Environment.getExternalStorageDirectory());
83 public static synchronized PersistentObjectCache getInstance() {
84 if (instance == null) {
85 throw new RuntimeException("getInstance called before init.");
90 public static synchronized PersistentObjectCache init(final Context context) {
91 if (instance == null) {
92 instance = new PersistentObjectCache(context);
94 if (!instance.dir.equals(context.getFilesDir())) {
95 throw new RuntimeException("File dir changed. old=" + instance.dir + ", new="
96 + context.getFilesDir());
102 private static PersistentObjectCache instance = null;