VolleyDiskCacheWrapper.java revision ac28599e2b40e0dd6b97f6a91849585531264622
1package com.bumptech.glide.volley;
2
3import android.util.Log;
4import com.android.volley.Cache;
5import com.android.volley.toolbox.ByteArrayPool;
6import com.android.volley.toolbox.DiskBasedCache;
7import com.android.volley.toolbox.PoolingByteArrayOutputStream;
8import com.bumptech.glide.load.engine.cache.StringKey;
9import com.bumptech.glide.load.engine.cache.DiskCache;
10
11import java.io.EOFException;
12import java.io.IOException;
13import java.io.InputStream;
14import java.io.OutputStream;
15import java.util.Collections;
16import java.util.HashMap;
17import java.util.Map;
18
19/**
20 * Closely based on {@link DiskBasedCache}.
21 */
22public class VolleyDiskCacheWrapper implements Cache {
23    private static final String TAG = "VolleyDiskCacheWrapper";
24    /** Magic number for current version of cache file format. */
25    private static final int CACHE_MAGIC = 0x20120504;
26    // 2 mb.
27    private static final int BYTE_POOL_SIZE = 2 * 1024 * 1024;
28    // 8 kb.
29    private static final int DEFAULT_BYTE_ARRAY_SIZE = 8 * 1024;
30
31    private final DiskCache diskCache;
32    private final ByteArrayPool byteArrayPool;
33
34    public VolleyDiskCacheWrapper(DiskCache diskCache) {
35        this.diskCache = diskCache;
36        this.byteArrayPool = new ByteArrayPool(BYTE_POOL_SIZE);
37    }
38
39    @Override
40    public Entry get(String key) {
41        InputStream result = diskCache.get(new StringKey(key));
42        if (result == null) {
43            return null;
44        }
45        try {
46            CacheHeader header = readHeader(result);
47            byte[] data = streamToBytes(result);
48            return header.toCacheEntry(data);
49        } catch (IOException e) {
50            if (Log.isLoggable(TAG, Log.DEBUG)) {
51                e.printStackTrace();
52            }
53            diskCache.delete(new StringKey(key));
54        } finally {
55            try {
56                result.close();
57            } catch (IOException e) { }
58        }
59        return null;
60    }
61
62    @Override
63    public void put(final String key, final Entry entry) {
64        diskCache.put(new StringKey(key), new DiskCache.Writer() {
65            @Override
66            public void write(OutputStream os) {
67                CacheHeader header = new CacheHeader(key, entry);
68                header.writeHeader(os);
69                try {
70                    os.write(entry.data);
71                } catch (IOException e) {
72                    if (Log.isLoggable(TAG, Log.DEBUG)) {
73                        Log.d(TAG, "Unable to write data", e);
74                    }
75                }
76            }
77        });
78    }
79
80    @Override
81    public void initialize() { }
82
83    @Override
84    public void invalidate(String key, boolean fullExpire) {
85        Entry entry = get(key);
86        if (entry != null) {
87            entry.softTtl = 0;
88            if (fullExpire) {
89                entry.ttl = 0;
90            }
91            put(key, entry);
92        }
93    }
94
95    @Override
96    public void remove(String key) {
97        diskCache.delete(new StringKey(key));
98    }
99
100    @Override
101    public void clear() { }
102
103    /**
104     * Reads the header off of an InputStream and returns a CacheHeader object.
105     * @param is The InputStream to read from.
106     * @throws IOException
107     */
108    public CacheHeader readHeader(InputStream is) throws IOException {
109        CacheHeader entry = new CacheHeader();
110        int magic = readInt(is);
111        if (magic != CACHE_MAGIC) {
112            // don't bother deleting, it'll get pruned eventually
113            throw new IOException();
114        }
115        entry.key = readString(is);
116        entry.etag = readString(is);
117        if (entry.etag.equals("")) {
118            entry.etag = null;
119        }
120        entry.serverDate = readLong(is);
121        entry.ttl = readLong(is);
122        entry.softTtl = readLong(is);
123        entry.responseHeaders = readStringStringMap(is);
124        return entry;
125    }
126
127        /**
128     * Handles holding onto the cache headers for an entry.
129     */
130    // Visible for testing.
131    class CacheHeader {
132        /** The size of the data identified by this CacheHeader. (This is not
133         * serialized to disk. */
134        public long size;
135
136        /** The key that identifies the cache entry. */
137        public String key;
138
139        /** ETag for cache coherence. */
140        public String etag;
141
142        /** Date of this response as reported by the server. */
143        public long serverDate;
144
145        /** TTL for this record. */
146        public long ttl;
147
148        /** Soft TTL for this record. */
149        public long softTtl;
150
151        /** Headers from the response resulting in this cache entry. */
152        public Map<String, String> responseHeaders;
153
154        private CacheHeader() { }
155
156        /**
157         * Instantiates a new CacheHeader object
158         * @param key The key that identifies the cache entry
159         * @param entry The cache entry.
160         */
161        public CacheHeader(String key, Entry entry) {
162            this.key = key;
163            this.size = entry.data.length;
164            this.etag = entry.etag;
165            this.serverDate = entry.serverDate;
166            this.ttl = entry.ttl;
167            this.softTtl = entry.softTtl;
168            this.responseHeaders = entry.responseHeaders;
169        }
170
171        /**
172         * Creates a cache entry for the specified data.
173         */
174        public Entry toCacheEntry(byte[] data) {
175            Entry e = new Entry();
176            e.data = data;
177            e.etag = etag;
178            e.serverDate = serverDate;
179            e.ttl = ttl;
180            e.softTtl = softTtl;
181            e.responseHeaders = responseHeaders;
182            return e;
183        }
184
185        /**
186         * Writes the contents of this CacheHeader to the specified OutputStream.
187         */
188        public boolean writeHeader(OutputStream os) {
189            try {
190                writeInt(os, CACHE_MAGIC);
191                writeString(os, key);
192                writeString(os, etag == null ? "" : etag);
193                writeLong(os, serverDate);
194                writeLong(os, ttl);
195                writeLong(os, softTtl);
196                writeStringStringMap(responseHeaders, os);
197                os.flush();
198                return true;
199            } catch (IOException e) {
200                if (Log.isLoggable(TAG, Log.DEBUG)) {
201                    Log.d("%s", e.toString());
202                }
203                return false;
204            }
205        }
206    }
207
208    /*
209     * Homebrewed simple serialization system used for reading and writing cache
210     * headers on disk. Once upon a time, this used the standard Java
211     * Object{Input,Output}Stream, but the default implementation relies heavily
212     * on reflection (even for standard types) and generates a ton of garbage.
213     */
214
215    /**
216     * Simple wrapper around {@link InputStream#read()} that throws EOFException
217     * instead of returning -1.
218     */
219    private static int read(InputStream is) throws IOException {
220        int b = is.read();
221        if (b == -1) {
222            throw new EOFException();
223        }
224        return b;
225    }
226
227    static void writeInt(OutputStream os, int n) throws IOException {
228        os.write((n >> 0) & 0xff);
229        os.write((n >> 8) & 0xff);
230        os.write((n >> 16) & 0xff);
231        os.write((n >> 24) & 0xff);
232    }
233
234    static int readInt(InputStream is) throws IOException {
235        int n = 0;
236        n |= (read(is) << 0);
237        n |= (read(is) << 8);
238        n |= (read(is) << 16);
239        n |= (read(is) << 24);
240        return n;
241    }
242
243    static void writeLong(OutputStream os, long n) throws IOException {
244        os.write((byte)(n >>> 0));
245        os.write((byte)(n >>> 8));
246        os.write((byte)(n >>> 16));
247        os.write((byte)(n >>> 24));
248        os.write((byte)(n >>> 32));
249        os.write((byte)(n >>> 40));
250        os.write((byte)(n >>> 48));
251        os.write((byte)(n >>> 56));
252    }
253
254    static long readLong(InputStream is) throws IOException {
255        long n = 0;
256        n |= ((read(is) & 0xFFL) << 0);
257        n |= ((read(is) & 0xFFL) << 8);
258        n |= ((read(is) & 0xFFL) << 16);
259        n |= ((read(is) & 0xFFL) << 24);
260        n |= ((read(is) & 0xFFL) << 32);
261        n |= ((read(is) & 0xFFL) << 40);
262        n |= ((read(is) & 0xFFL) << 48);
263        n |= ((read(is) & 0xFFL) << 56);
264        return n;
265    }
266
267    static void writeString(OutputStream os, String s) throws IOException {
268        byte[] b = s.getBytes("UTF-8");
269        writeLong(os, b.length);
270        os.write(b, 0, b.length);
271    }
272
273    String readString(InputStream is) throws IOException {
274        int n = (int) readLong(is);
275        byte[] b = streamToBytes(is, n, byteArrayPool.getBuf(n));
276        String result = new String(b, "UTF-8");
277        byteArrayPool.returnBuf(b);
278        return result;
279    }
280
281    static void writeStringStringMap(Map<String, String> map, OutputStream os) throws IOException {
282        if (map != null) {
283            writeInt(os, map.size());
284            for (Map.Entry<String, String> entry : map.entrySet()) {
285                writeString(os, entry.getKey());
286                writeString(os, entry.getValue());
287            }
288        } else {
289            writeInt(os, 0);
290        }
291    }
292
293    Map<String, String> readStringStringMap(InputStream is) throws IOException {
294        int size = readInt(is);
295        Map<String, String> result = (size == 0)
296                ? Collections.<String, String>emptyMap()
297                : new HashMap<String, String>(size);
298        for (int i = 0; i < size; i++) {
299            String key = readString(is).intern();
300            String value = readString(is).intern();
301            result.put(key, value);
302        }
303        return result;
304    }
305
306    /**
307     * Reads the contents of an InputStream into a byte[].
308     */
309    private static byte[] streamToBytes(InputStream in, int length, byte[] bytes) throws IOException {
310        int count;
311        int pos = 0;
312        while (pos < length && ((count = in.read(bytes, pos, length - pos)) != -1)) {
313            pos += count;
314        }
315        if (pos != length) {
316            throw new IOException("Expected " + length + " bytes, read " + pos + " bytes");
317        }
318        return bytes;
319    }
320
321    private byte[] streamToBytes(InputStream in) throws IOException {
322        PoolingByteArrayOutputStream outputStream = new PoolingByteArrayOutputStream(byteArrayPool);
323        byte[] bytes = byteArrayPool.getBuf(DEFAULT_BYTE_ARRAY_SIZE);
324        int pos = 0;
325        while ((in.read(bytes, pos, bytes.length - pos)) != -1) {
326            outputStream.write(bytes);
327        }
328        byteArrayPool.returnBuf(bytes);
329        byte[] result = outputStream.toByteArray();
330        outputStream.close();
331        return result;
332    }
333}
334