]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/util/PersistentObjectCache.java
Prefer ordinary HashMap over LinkedHashMap.
[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 HashMap<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                 throw new InvalidClassException("Not allowed to deserialize class", name);
61             }
62             return super.resolveClass(desc);
63         }
64     }
65
66     public synchronized <T extends Serializable> T read(final String filename, final Class<T> resultClass) {
67         try {
68             Object object = (objects.get(filename));
69             if (object != null) {
70                 return resultClass.cast(object);
71             }
72             Log.d(getClass().getSimpleName(), "Cache miss.");
73             final File src = new File(dir, filename);
74             if (!src.canRead()) {
75                 Log.d(getClass().getSimpleName(), "File empty: " + src);
76                 return null;
77             }
78             ObjectInputStream in = null;
79             try {
80                 in = new ConstrainedOIS(new FileInputStream(src));
81                 object = in.readObject();
82                 in.close();
83             } catch (Exception e) {
84                 Log.e(getClass().getSimpleName(), "Deserialization failed: " + src, e);
85                 try {
86                     if (in != null) in.close();
87                 } catch (IOException e2) {}
88                 return null;
89             }
90             objects.put(filename, object);
91             return resultClass.cast(object);
92         } catch (ClassCastException e) {
93             return null;
94         }
95     }
96
97     public synchronized void write(final String filename, final Serializable object) {
98         objects.put(filename, object);
99         final File dest = new File(dir, filename);
100         ObjectOutputStream out = null;
101         try {
102             out = new ObjectOutputStream(new FileOutputStream(dest));
103             out.writeObject(object);
104         } catch (Exception e) {
105             Log.e(getClass().getSimpleName(), "Serialization failed: " + dest, e);
106         }
107         try {
108             if (out != null) out.close();
109         } catch (IOException e) {}
110     }
111
112     private PersistentObjectCache(final Context context) {
113         final File filesDir = context.getFilesDir();
114         dir = filesDir != null ? filesDir : Environment.getExternalStorageDirectory();
115         if (dir == null) {
116             throw new RuntimeException("context.getFilesDir() == " + context.getFilesDir()
117                                        + ", Environment.getExternalStorageDirectory()="
118                                        + Environment.getExternalStorageDirectory());
119         }
120     }
121
122     public static synchronized PersistentObjectCache getInstance() {
123         if (instance == null) {
124             throw new RuntimeException("getInstance called before init.");
125         }
126         return instance;
127     }
128
129     public static synchronized PersistentObjectCache init(final Context context) {
130         if (instance == null) {
131             instance = new PersistentObjectCache(context);
132         } else {
133             if (!instance.dir.equals(context.getFilesDir())) {
134                 throw new RuntimeException("File dir changed.  old=" + instance.dir + ", new="
135                                            + context.getFilesDir());
136             }
137         }
138         return instance;
139     }
140
141     private static PersistentObjectCache instance = null;
142
143 }