]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/util/PersistentObjectCache.java
Another resource leak fix.
[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 com.hughes.android.dictionary.DictionaryApplication;
22 import com.hughes.android.dictionary.DictionaryInfo;
23
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.InvalidClassException;
30 import java.io.ObjectInputStream;
31 import java.io.ObjectOutputStream;
32 import java.io.ObjectStreamClass;
33 import java.io.Serializable;
34 import java.util.ArrayList;
35 import java.util.HashMap;
36 import java.util.LinkedHashMap;
37 import java.util.Map;
38
39 public class PersistentObjectCache {
40
41     private final File dir;
42     private final Map<String, Object> objects = new LinkedHashMap<String, Object>();
43
44     class ConstrainedOIS extends ObjectInputStream {
45       public ConstrainedOIS(InputStream in) throws IOException {
46         super(in);
47       }
48
49       protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
50         String name = desc.getName();
51         // Note: try to avoid adding more classes.
52         // LinkedHashMap is already more than enough for a DoS
53         if (!name.equals(ArrayList.class.getName()) &&
54             !name.equals(HashMap.class.getName()) &&
55             !name.equals(LinkedHashMap.class.getName()) &&
56             !name.equals(String.class.getName()) &&
57             !name.equals(DictionaryApplication.DictionaryConfig.class.getName()) &&
58             !name.equals(DictionaryInfo.class.getName()) &&
59             !name.equals(DictionaryInfo.IndexInfo.class.getName()))
60         {
61           throw new InvalidClassException("Not allowed to deserialize class", name);
62         }
63         return super.resolveClass(desc);
64       }
65     }
66
67     public synchronized <T extends Serializable> T read(final String filename, final Class<T> resultClass) {
68         try {
69             Object object = (objects.get(filename));
70             if (object != null) {
71                 return resultClass.cast(object);
72             }
73             Log.d(getClass().getSimpleName(), "Cache miss.");
74             final File src = new File(dir, filename);
75             if (!src.canRead()) {
76                 Log.d(getClass().getSimpleName(), "File empty: " + src);
77                 return null;
78             }
79             ObjectInputStream in = null;
80             try {
81                 in = new ConstrainedOIS(new FileInputStream(src));
82                 object = in.readObject();
83                 in.close();
84             } catch (Exception e) {
85                 Log.e(getClass().getSimpleName(), "Deserialization failed: " + src, e);
86                 try { if (in != null) in.close(); } catch (IOException e2) {}
87                 return null;
88             }
89             objects.put(filename, object);
90             return resultClass.cast(object);
91         } catch (ClassCastException e) {
92             return null;
93         }
94     }
95
96     public synchronized void write(final String filename, final Serializable object) {
97         objects.put(filename, object);
98         final File dest = new File(dir, filename);
99         ObjectOutputStream out = null;
100         try {
101             out = new ObjectOutputStream(new FileOutputStream(dest));
102             out.writeObject(object);
103         } catch (Exception e) {
104             Log.e(getClass().getSimpleName(), "Serialization failed: " + dest, e);
105         }
106         try { if (out != null) out.close(); } catch (IOException e) {}
107     }
108
109     private PersistentObjectCache(final Context context) {
110         final File filesDir = context.getFilesDir();
111         dir = filesDir != null ? filesDir : Environment.getExternalStorageDirectory();
112         if (dir == null) {
113             throw new RuntimeException("context.getFilesDir() == " + context.getFilesDir()
114                     + ", Environment.getExternalStorageDirectory()="
115                     + Environment.getExternalStorageDirectory());
116         }
117     }
118
119     public static synchronized PersistentObjectCache getInstance() {
120         if (instance == null) {
121             throw new RuntimeException("getInstance called before init.");
122         }
123         return instance;
124     }
125
126     public static synchronized PersistentObjectCache init(final Context context) {
127         if (instance == null) {
128             instance = new PersistentObjectCache(context);
129         } else {
130             if (!instance.dir.equals(context.getFilesDir())) {
131                 throw new RuntimeException("File dir changed.  old=" + instance.dir + ", new="
132                         + context.getFilesDir());
133             }
134         }
135         return instance;
136     }
137
138     private static PersistentObjectCache instance = null;
139
140 }