forked from fhucho/simple-disk-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleDiskCache.java
332 lines (276 loc) · 8.22 KB
/
SimpleDiskCache.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package cz.fhucho.android.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import com.jakewharton.disklrucache.DiskLruCache;
/**
* Adapted from https://github.com/fhucho/simple-disk-cache
* License Apache 2.0
*/
public class SimpleDiskCache {
private static final int VALUE_IDX = 0;
private static final int METADATA_IDX = 1;
private static final List<File> usedDirs = new ArrayList<File>();
private com.jakewharton.disklrucache.DiskLruCache diskLruCache;
private int mAppVersion;
private SimpleDiskCache(File dir, int appVersion, long maxSize) throws IOException {
mAppVersion = appVersion;
diskLruCache = DiskLruCache.open(dir, appVersion, 2, maxSize);
}
public static synchronized SimpleDiskCache open(File dir, int appVersion, long maxSize)
throws IOException {
if (usedDirs.contains(dir)) {
throw new IllegalStateException("Cache dir " + dir.getAbsolutePath() + " was used before.");
}
usedDirs.add(dir);
return new SimpleDiskCache(dir, appVersion, maxSize);
}
/**
* User should be sure there are no outstanding operations.
* @throws IOException
*/
public void clear() throws IOException {
File dir = diskLruCache.getDirectory();
long maxSize = diskLruCache.getMaxSize();
diskLruCache.delete();
diskLruCache = DiskLruCache.open(dir, mAppVersion, 2, maxSize);
}
public DiskLruCache getCache() {
return diskLruCache;
}
public InputStreamEntry getInputStream(String key) throws IOException {
DiskLruCache.Snapshot snapshot = diskLruCache.get(toInternalKey(key));
if (snapshot == null) return null;
return new InputStreamEntry(snapshot, readMetadata(snapshot));
}
public BitmapEntry getBitmap(String key) throws IOException {
DiskLruCache.Snapshot snapshot = diskLruCache.get(toInternalKey(key));
if (snapshot == null) return null;
try {
Bitmap bitmap = BitmapFactory.decodeStream(snapshot.getInputStream(VALUE_IDX));
return new BitmapEntry(bitmap, readMetadata(snapshot));
} finally {
snapshot.close();
}
}
public StringEntry getString(String key) throws IOException {
DiskLruCache.Snapshot snapshot = diskLruCache.get(toInternalKey(key));
if (snapshot == null) return null;
try {
return new StringEntry(snapshot.getString(VALUE_IDX), readMetadata(snapshot));
} finally {
snapshot.close();
}
}
public boolean contains(String key) throws IOException {
DiskLruCache.Snapshot snapshot = diskLruCache.get(toInternalKey(key));
if(snapshot==null) return false;
snapshot.close();
return true;
}
public OutputStream openStream(String key) throws IOException {
return openStream(key, new HashMap<String, Serializable>());
}
public OutputStream openStream(String key, Map<String, ? extends Serializable> metadata)
throws IOException {
DiskLruCache.Editor editor = diskLruCache.edit(toInternalKey(key));
try {
writeMetadata(metadata, editor);
BufferedOutputStream bos = new BufferedOutputStream(editor.newOutputStream(VALUE_IDX));
return new CacheOutputStream(bos, editor);
} catch (IOException e) {
editor.abort();
throw e;
}
}
public void put(String key, InputStream is) throws IOException {
put(key, is, new HashMap<String, Serializable>());
}
public void put(String key, InputStream is, Map<String, Serializable> annotations)
throws IOException {
OutputStream os = null;
try {
os = openStream(key, annotations);
IOUtils.copy(is, os);
} finally {
if (os != null) os.close();
}
}
public void put(String key, String value) throws IOException {
put(key, value, new HashMap<String, Serializable>());
}
public void put(String key, String value, Map<String, ? extends Serializable> annotations)
throws IOException {
OutputStream cos = null;
try {
cos = openStream(key, annotations);
cos.write(value.getBytes());
} finally {
if (cos != null) cos.close();
}
}
private void writeMetadata(Map<String, ? extends Serializable> metadata,
DiskLruCache.Editor editor) throws IOException {
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new BufferedOutputStream(
editor.newOutputStream(METADATA_IDX)));
oos.writeObject(metadata);
} finally {
IOUtils.closeQuietly(oos);
}
}
private Map<String, Serializable> readMetadata(DiskLruCache.Snapshot snapshot)
throws IOException {
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new BufferedInputStream(
snapshot.getInputStream(METADATA_IDX)));
@SuppressWarnings("unchecked")
Map<String, Serializable> annotations = (Map<String, Serializable>) ois.readObject();
return annotations;
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} finally {
IOUtils.closeQuietly(ois);
}
}
private String toInternalKey(String key) {
return md5(key);
}
private String md5(String s) {
try {
MessageDigest m = MessageDigest.getInstance("MD5");
m.update(s.getBytes("UTF-8"));
byte[] digest = m.digest();
BigInteger bigInt = new BigInteger(1, digest);
return bigInt.toString(16);
} catch (NoSuchAlgorithmException e) {
throw new AssertionError();
} catch (UnsupportedEncodingException e) {
throw new AssertionError();
}
}
private class CacheOutputStream extends FilterOutputStream {
private final DiskLruCache.Editor editor;
private boolean failed = false;
private CacheOutputStream(OutputStream os, DiskLruCache.Editor editor) {
super(os);
this.editor = editor;
}
@Override
public void close() throws IOException {
IOException closeException = null;
try {
super.close();
} catch (IOException e) {
closeException = e;
}
if (failed) {
editor.abort();
} else {
editor.commit();
}
if (closeException != null) throw closeException;
}
@Override
public void flush() throws IOException {
try {
super.flush();
} catch (IOException e) {
failed = true;
throw e;
}
}
@Override
public void write(int oneByte) throws IOException {
try {
super.write(oneByte);
} catch (IOException e) {
failed = true;
throw e;
}
}
@Override
public void write(byte[] buffer) throws IOException {
try {
super.write(buffer);
} catch (IOException e) {
failed = true;
throw e;
}
}
@Override
public void write(byte[] buffer, int offset, int length) throws IOException {
try {
super.write(buffer, offset, length);
} catch (IOException e) {
failed = true;
throw e;
}
}
}
public static class InputStreamEntry {
private final DiskLruCache.Snapshot snapshot;
private final Map<String, Serializable> metadata;
public InputStreamEntry(DiskLruCache.Snapshot snapshot, Map<String, Serializable> metadata) {
this.metadata = metadata;
this.snapshot = snapshot;
}
public InputStream getInputStream() {
return snapshot.getInputStream(VALUE_IDX);
}
public Map<String, Serializable> getMetadata() {
return metadata;
}
public void close() {
snapshot.close();
}
}
public static class BitmapEntry {
private final Bitmap bitmap;
private final Map<String, Serializable> metadata;
public BitmapEntry(Bitmap bitmap, Map<String, Serializable> metadata) {
this.bitmap = bitmap;
this.metadata = metadata;
}
public Bitmap getBitmap() {
return bitmap;
}
public Map<String, Serializable> getMetadata() {
return metadata;
}
}
public static class StringEntry {
private final String string;
private final Map<String, Serializable> metadata;
public StringEntry(String string, Map<String, Serializable> metadata) {
this.string = string;
this.metadata = metadata;
}
public String getString() {
return string;
}
public Map<String, Serializable> getMetadata() {
return metadata;
}
}
}