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;
21 import com.hughes.android.dictionary.DictionaryApplication;
22 import com.hughes.android.dictionary.DictionaryInfo;
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;
39 public class PersistentObjectCache {
41 private final File dir;
42 private final Map<String, Object> objects = new LinkedHashMap<String, Object>();
44 class ConstrainedOIS extends ObjectInputStream {
45 public ConstrainedOIS(InputStream in) throws IOException {
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()))
61 throw new InvalidClassException("Not allowed to deserialize class", name);
63 return super.resolveClass(desc);
67 public synchronized <T extends Serializable> T read(final String filename, final Class<T> resultClass) {
69 Object object = (objects.get(filename));
71 return resultClass.cast(object);
73 Log.d(getClass().getSimpleName(), "Cache miss.");
74 final File src = new File(dir, filename);
76 Log.d(getClass().getSimpleName(), "File empty: " + src);
80 final ObjectInputStream in = new ConstrainedOIS(new FileInputStream(src));
81 object = in.readObject();
83 } catch (Exception e) {
84 Log.e(getClass().getSimpleName(), "Deserialization failed: " + src, e);
87 objects.put(filename, object);
88 return resultClass.cast(object);
89 } catch (ClassCastException e) {
94 public synchronized void write(final String filename, final Serializable object) {
95 objects.put(filename, object);
96 final File dest = new File(dir, filename);
98 final ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(dest));
99 out.writeObject(object);
101 } catch (Exception e) {
102 Log.e(getClass().getSimpleName(), "Serialization failed: " + dest, e);
106 private PersistentObjectCache(final Context context) {
107 final File filesDir = context.getFilesDir();
108 dir = filesDir != null ? filesDir : Environment.getExternalStorageDirectory();
110 throw new RuntimeException("context.getFilesDir() == " + context.getFilesDir()
111 + ", Environment.getExternalStorageDirectory()="
112 + Environment.getExternalStorageDirectory());
116 public static synchronized PersistentObjectCache getInstance() {
117 if (instance == null) {
118 throw new RuntimeException("getInstance called before init.");
123 public static synchronized PersistentObjectCache init(final Context context) {
124 if (instance == null) {
125 instance = new PersistentObjectCache(context);
127 if (!instance.dir.equals(context.getFilesDir())) {
128 throw new RuntimeException("File dir changed. old=" + instance.dir + ", new="
129 + context.getFilesDir());
135 private static PersistentObjectCache instance = null;