]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/util/PersistentObjectCache.java
Check at compiletime that PersistentObjectCache objects are serializable.
[Dictionary.git] / src / com / hughes / android / util / PersistentObjectCache.java
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
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
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15 package com.hughes.android.util;
16
17 import android.content.Context;
18 import android.os.Environment;
19 import android.util.Log;
20
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileOutputStream;
24 import java.io.ObjectInputStream;
25 import java.io.ObjectOutputStream;
26 import java.io.Serializable;
27 import java.util.LinkedHashMap;
28 import java.util.Map;
29
30 public class PersistentObjectCache {
31
32     private final File dir;
33     private final Map<String, Object> objects = new LinkedHashMap<String, Object>();
34
35     public synchronized <T extends Serializable> T read(final String filename, final Class<T> resultClass) {
36         try {
37             Object object = (objects.get(filename));
38             if (object != null) {
39                 return resultClass.cast(object);
40             }
41             Log.d(getClass().getSimpleName(), "Cache miss.");
42             final File src = new File(dir, filename);
43             if (!src.canRead()) {
44                 Log.d(getClass().getSimpleName(), "File empty: " + src);
45                 return null;
46             }
47             try {
48                 final ObjectInputStream in = new ObjectInputStream(new FileInputStream(src));
49                 object = in.readObject();
50                 in.close();
51             } catch (Exception e) {
52                 Log.e(getClass().getSimpleName(), "Deserialization failed: " + src, e);
53                 return null;
54             }
55             objects.put(filename, object);
56             return resultClass.cast(object);
57         } catch (ClassCastException e) {
58             return null;
59         }
60     }
61
62     public synchronized void write(final String filename, final Serializable object) {
63         objects.put(filename, object);
64         final File dest = new File(dir, filename);
65         try {
66             final ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(dest));
67             out.writeObject(object);
68             out.close();
69         } catch (Exception e) {
70             Log.e(getClass().getSimpleName(), "Serialization failed: " + dest, e);
71         }
72     }
73
74     private PersistentObjectCache(final Context context) {
75         final File filesDir = context.getFilesDir();
76         dir = filesDir != null ? filesDir : Environment.getExternalStorageDirectory();
77         if (dir == null) {
78             throw new RuntimeException("context.getFilesDir() == " + context.getFilesDir()
79                     + ", Environment.getExternalStorageDirectory()="
80                     + Environment.getExternalStorageDirectory());
81         }
82     }
83
84     public static synchronized PersistentObjectCache getInstance() {
85         if (instance == null) {
86             throw new RuntimeException("getInstance called before init.");
87         }
88         return instance;
89     }
90
91     public static synchronized PersistentObjectCache init(final Context context) {
92         if (instance == null) {
93             instance = new PersistentObjectCache(context);
94         } else {
95             if (!instance.dir.equals(context.getFilesDir())) {
96                 throw new RuntimeException("File dir changed.  old=" + instance.dir + ", new="
97                         + context.getFilesDir());
98             }
99         }
100         return instance;
101     }
102
103     private static PersistentObjectCache instance = null;
104
105 }