, . svg . CPU, .
, . (, 2) svg, .
10 Context.getCacheDir(), .
, , , Cache, , .. .
, Serializable, :
public class ObjectCacheFile<T> {
private final File mFile;
public ObjectCacheFile(Context context, String name) {
mFile = new File(context.getCacheDir(), name);
}
public File getFile() {
return mFile;
}
public void put(T o) {
try {
if (!mFile.exists()) {
mFile.createNewFile();
}
FileOutputStream fos = new FileOutputStream(mFile);
ObjectOutputStream objOut = new ObjectOutputStream(fos);
try {
objOut.writeObject(o);
} finally {
objOut.close();
}
} catch (IOException e) {
Log.e(App.getLogTag(this), "error saving cache file", e);
}
}
@SuppressWarnings("unchecked")
public T get() {
if (!mFile.exists()) {
return null;
}
try {
ObjectInputStream objIn = new ObjectInputStream(new FileInputStream(mFile));
try {
return (T) objIn.readObject();
} finally {
objIn.close();
}
} catch (IOException e) {
Log.e(App.getLogTag(this), "error reading cache file", e);
} catch (ClassNotFoundException e1) {
Log.e(App.getLogTag(this), "cache file corrupted, deleting", e1);
mFile.delete();
}
return null;
}
}