1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.volley.toolbox;
18
19import android.os.SystemClock;
20
21import com.android.volley.Cache;
22import com.android.volley.VolleyLog;
23
24import java.io.BufferedInputStream;
25import java.io.BufferedOutputStream;
26import java.io.EOFException;
27import java.io.File;
28import java.io.FileInputStream;
29import java.io.FileOutputStream;
30import java.io.FilterInputStream;
31import java.io.IOException;
32import java.io.InputStream;
33import java.io.OutputStream;
34import java.util.Collections;
35import java.util.HashMap;
36import java.util.Iterator;
37import java.util.LinkedHashMap;
38import java.util.Map;
39
40/**
41 * Cache implementation that caches files directly onto the hard disk in the specified
42 * directory. The default disk usage size is 5MB, but is configurable.
43 */
44public class DiskBasedCache implements Cache {
45
46    /** Map of the Key, CacheHeader pairs */
47    private final Map<String, CacheHeader> mEntries =
48            new LinkedHashMap<String, CacheHeader>(16, .75f, true);
49
50    /** Total amount of space currently used by the cache in bytes. */
51    private long mTotalSize = 0;
52
53    /** The root directory to use for the cache. */
54    private final File mRootDirectory;
55
56    /** The maximum size of the cache in bytes. */
57    private final int mMaxCacheSizeInBytes;
58
59    /** Default maximum disk usage in bytes. */
60    private static final int DEFAULT_DISK_USAGE_BYTES = 5 * 1024 * 1024;
61
62    /** High water mark percentage for the cache */
63    private static final float HYSTERESIS_FACTOR = 0.9f;
64
65    /** Magic number for current version of cache file format. */
66    private static final int CACHE_MAGIC = 0x20150306;
67
68    /**
69     * Constructs an instance of the DiskBasedCache at the specified directory.
70     * @param rootDirectory The root directory of the cache.
71     * @param maxCacheSizeInBytes The maximum size of the cache in bytes.
72     */
73    public DiskBasedCache(File rootDirectory, int maxCacheSizeInBytes) {
74        mRootDirectory = rootDirectory;
75        mMaxCacheSizeInBytes = maxCacheSizeInBytes;
76    }
77
78    /**
79     * Constructs an instance of the DiskBasedCache at the specified directory using
80     * the default maximum cache size of 5MB.
81     * @param rootDirectory The root directory of the cache.
82     */
83    public DiskBasedCache(File rootDirectory) {
84        this(rootDirectory, DEFAULT_DISK_USAGE_BYTES);
85    }
86
87    /**
88     * Clears the cache. Deletes all cached files from disk.
89     */
90    @Override
91    public synchronized void clear() {
92        File[] files = mRootDirectory.listFiles();
93        if (files != null) {
94            for (File file : files) {
95                file.delete();
96            }
97        }
98        mEntries.clear();
99        mTotalSize = 0;
100        VolleyLog.d("Cache cleared.");
101    }
102
103    /**
104     * Returns the cache entry with the specified key if it exists, null otherwise.
105     */
106    @Override
107    public synchronized Entry get(String key) {
108        CacheHeader entry = mEntries.get(key);
109        // if the entry does not exist, return.
110        if (entry == null) {
111            return null;
112        }
113
114        File file = getFileForKey(key);
115        CountingInputStream cis = null;
116        try {
117            cis = new CountingInputStream(new BufferedInputStream(new FileInputStream(file)));
118            CacheHeader.readHeader(cis); // eat header
119            byte[] data = streamToBytes(cis, (int) (file.length() - cis.bytesRead));
120            return entry.toCacheEntry(data);
121        } catch (IOException e) {
122            VolleyLog.d("%s: %s", file.getAbsolutePath(), e.toString());
123            remove(key);
124            return null;
125        } finally {
126            if (cis != null) {
127                try {
128                    cis.close();
129                } catch (IOException ioe) {
130                    return null;
131                }
132            }
133        }
134    }
135
136    /**
137     * Initializes the DiskBasedCache by scanning for all files currently in the
138     * specified root directory. Creates the root directory if necessary.
139     */
140    @Override
141    public synchronized void initialize() {
142        if (!mRootDirectory.exists()) {
143            if (!mRootDirectory.mkdirs()) {
144                VolleyLog.e("Unable to create cache dir %s", mRootDirectory.getAbsolutePath());
145            }
146            return;
147        }
148
149        File[] files = mRootDirectory.listFiles();
150        if (files == null) {
151            return;
152        }
153        for (File file : files) {
154            BufferedInputStream fis = null;
155            try {
156                fis = new BufferedInputStream(new FileInputStream(file));
157                CacheHeader entry = CacheHeader.readHeader(fis);
158                entry.size = file.length();
159                putEntry(entry.key, entry);
160            } catch (IOException e) {
161                if (file != null) {
162                   file.delete();
163                }
164            } finally {
165                try {
166                    if (fis != null) {
167                        fis.close();
168                    }
169                } catch (IOException ignored) { }
170            }
171        }
172    }
173
174    /**
175     * Invalidates an entry in the cache.
176     * @param key Cache key
177     * @param fullExpire True to fully expire the entry, false to soft expire
178     */
179    @Override
180    public synchronized void invalidate(String key, boolean fullExpire) {
181        Entry entry = get(key);
182        if (entry != null) {
183            entry.softTtl = 0;
184            if (fullExpire) {
185                entry.ttl = 0;
186            }
187            put(key, entry);
188        }
189
190    }
191
192    /**
193     * Puts the entry with the specified key into the cache.
194     */
195    @Override
196    public synchronized void put(String key, Entry entry) {
197        pruneIfNeeded(entry.data.length);
198        File file = getFileForKey(key);
199        try {
200            BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(file));
201            CacheHeader e = new CacheHeader(key, entry);
202            boolean success = e.writeHeader(fos);
203            if (!success) {
204                fos.close();
205                VolleyLog.d("Failed to write header for %s", file.getAbsolutePath());
206                throw new IOException();
207            }
208            fos.write(entry.data);
209            fos.close();
210            putEntry(key, e);
211            return;
212        } catch (IOException e) {
213        }
214        boolean deleted = file.delete();
215        if (!deleted) {
216            VolleyLog.d("Could not clean up file %s", file.getAbsolutePath());
217        }
218    }
219
220    /**
221     * Removes the specified key from the cache if it exists.
222     */
223    @Override
224    public synchronized void remove(String key) {
225        boolean deleted = getFileForKey(key).delete();
226        removeEntry(key);
227        if (!deleted) {
228            VolleyLog.d("Could not delete cache entry for key=%s, filename=%s",
229                    key, getFilenameForKey(key));
230        }
231    }
232
233    /**
234     * Creates a pseudo-unique filename for the specified cache key.
235     * @param key The key to generate a file name for.
236     * @return A pseudo-unique filename.
237     */
238    private String getFilenameForKey(String key) {
239        int firstHalfLength = key.length() / 2;
240        String localFilename = String.valueOf(key.substring(0, firstHalfLength).hashCode());
241        localFilename += String.valueOf(key.substring(firstHalfLength).hashCode());
242        return localFilename;
243    }
244
245    /**
246     * Returns a file object for the given cache key.
247     */
248    public File getFileForKey(String key) {
249        return new File(mRootDirectory, getFilenameForKey(key));
250    }
251
252    /**
253     * Prunes the cache to fit the amount of bytes specified.
254     * @param neededSpace The amount of bytes we are trying to fit into the cache.
255     */
256    private void pruneIfNeeded(int neededSpace) {
257        if ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes) {
258            return;
259        }
260        if (VolleyLog.DEBUG) {
261            VolleyLog.v("Pruning old cache entries.");
262        }
263
264        long before = mTotalSize;
265        int prunedFiles = 0;
266        long startTime = SystemClock.elapsedRealtime();
267
268        Iterator<Map.Entry<String, CacheHeader>> iterator = mEntries.entrySet().iterator();
269        while (iterator.hasNext()) {
270            Map.Entry<String, CacheHeader> entry = iterator.next();
271            CacheHeader e = entry.getValue();
272            boolean deleted = getFileForKey(e.key).delete();
273            if (deleted) {
274                mTotalSize -= e.size;
275            } else {
276               VolleyLog.d("Could not delete cache entry for key=%s, filename=%s",
277                       e.key, getFilenameForKey(e.key));
278            }
279            iterator.remove();
280            prunedFiles++;
281
282            if ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes * HYSTERESIS_FACTOR) {
283                break;
284            }
285        }
286
287        if (VolleyLog.DEBUG) {
288            VolleyLog.v("pruned %d files, %d bytes, %d ms",
289                    prunedFiles, (mTotalSize - before), SystemClock.elapsedRealtime() - startTime);
290        }
291    }
292
293    /**
294     * Puts the entry with the specified key into the cache.
295     * @param key The key to identify the entry by.
296     * @param entry The entry to cache.
297     */
298    private void putEntry(String key, CacheHeader entry) {
299        if (!mEntries.containsKey(key)) {
300            mTotalSize += entry.size;
301        } else {
302            CacheHeader oldEntry = mEntries.get(key);
303            mTotalSize += (entry.size - oldEntry.size);
304        }
305        mEntries.put(key, entry);
306    }
307
308    /**
309     * Removes the entry identified by 'key' from the cache.
310     */
311    private void removeEntry(String key) {
312        CacheHeader entry = mEntries.get(key);
313        if (entry != null) {
314            mTotalSize -= entry.size;
315            mEntries.remove(key);
316        }
317    }
318
319    /**
320     * Reads the contents of an InputStream into a byte[].
321     * */
322    private static byte[] streamToBytes(InputStream in, int length) throws IOException {
323        byte[] bytes = new byte[length];
324        int count;
325        int pos = 0;
326        while (pos < length && ((count = in.read(bytes, pos, length - pos)) != -1)) {
327            pos += count;
328        }
329        if (pos != length) {
330            throw new IOException("Expected " + length + " bytes, read " + pos + " bytes");
331        }
332        return bytes;
333    }
334
335    /**
336     * Handles holding onto the cache headers for an entry.
337     */
338    // Visible for testing.
339    static class CacheHeader {
340        /** The size of the data identified by this CacheHeader. (This is not
341         * serialized to disk. */
342        public long size;
343
344        /** The key that identifies the cache entry. */
345        public String key;
346
347        /** ETag for cache coherence. */
348        public String etag;
349
350        /** Date of this response as reported by the server. */
351        public long serverDate;
352
353        /** The last modified date for the requested object. */
354        public long lastModified;
355
356        /** TTL for this record. */
357        public long ttl;
358
359        /** Soft TTL for this record. */
360        public long softTtl;
361
362        /** Headers from the response resulting in this cache entry. */
363        public Map<String, String> responseHeaders;
364
365        private CacheHeader() { }
366
367        /**
368         * Instantiates a new CacheHeader object
369         * @param key The key that identifies the cache entry
370         * @param entry The cache entry.
371         */
372        public CacheHeader(String key, Entry entry) {
373            this.key = key;
374            this.size = entry.data.length;
375            this.etag = entry.etag;
376            this.serverDate = entry.serverDate;
377            this.lastModified = entry.lastModified;
378            this.ttl = entry.ttl;
379            this.softTtl = entry.softTtl;
380            this.responseHeaders = entry.responseHeaders;
381        }
382
383        /**
384         * Reads the header off of an InputStream and returns a CacheHeader object.
385         * @param is The InputStream to read from.
386         * @throws IOException
387         */
388        public static CacheHeader readHeader(InputStream is) throws IOException {
389            CacheHeader entry = new CacheHeader();
390            int magic = readInt(is);
391            if (magic != CACHE_MAGIC) {
392                // don't bother deleting, it'll get pruned eventually
393                throw new IOException();
394            }
395            entry.key = readString(is);
396            entry.etag = readString(is);
397            if (entry.etag.equals("")) {
398                entry.etag = null;
399            }
400            entry.serverDate = readLong(is);
401            entry.lastModified = readLong(is);
402            entry.ttl = readLong(is);
403            entry.softTtl = readLong(is);
404            entry.responseHeaders = readStringStringMap(is);
405
406            return entry;
407        }
408
409        /**
410         * Creates a cache entry for the specified data.
411         */
412        public Entry toCacheEntry(byte[] data) {
413            Entry e = new Entry();
414            e.data = data;
415            e.etag = etag;
416            e.serverDate = serverDate;
417            e.lastModified = lastModified;
418            e.ttl = ttl;
419            e.softTtl = softTtl;
420            e.responseHeaders = responseHeaders;
421            return e;
422        }
423
424
425        /**
426         * Writes the contents of this CacheHeader to the specified OutputStream.
427         */
428        public boolean writeHeader(OutputStream os) {
429            try {
430                writeInt(os, CACHE_MAGIC);
431                writeString(os, key);
432                writeString(os, etag == null ? "" : etag);
433                writeLong(os, serverDate);
434                writeLong(os, lastModified);
435                writeLong(os, ttl);
436                writeLong(os, softTtl);
437                writeStringStringMap(responseHeaders, os);
438                os.flush();
439                return true;
440            } catch (IOException e) {
441                VolleyLog.d("%s", e.toString());
442                return false;
443            }
444        }
445
446    }
447
448    private static class CountingInputStream extends FilterInputStream {
449        private int bytesRead = 0;
450
451        private CountingInputStream(InputStream in) {
452            super(in);
453        }
454
455        @Override
456        public int read() throws IOException {
457            int result = super.read();
458            if (result != -1) {
459                bytesRead++;
460            }
461            return result;
462        }
463
464        @Override
465        public int read(byte[] buffer, int offset, int count) throws IOException {
466            int result = super.read(buffer, offset, count);
467            if (result != -1) {
468                bytesRead += result;
469            }
470            return result;
471        }
472    }
473
474    /*
475     * Homebrewed simple serialization system used for reading and writing cache
476     * headers on disk. Once upon a time, this used the standard Java
477     * Object{Input,Output}Stream, but the default implementation relies heavily
478     * on reflection (even for standard types) and generates a ton of garbage.
479     */
480
481    /**
482     * Simple wrapper around {@link InputStream#read()} that throws EOFException
483     * instead of returning -1.
484     */
485    private static int read(InputStream is) throws IOException {
486        int b = is.read();
487        if (b == -1) {
488            throw new EOFException();
489        }
490        return b;
491    }
492
493    static void writeInt(OutputStream os, int n) throws IOException {
494        os.write((n >> 0) & 0xff);
495        os.write((n >> 8) & 0xff);
496        os.write((n >> 16) & 0xff);
497        os.write((n >> 24) & 0xff);
498    }
499
500    static int readInt(InputStream is) throws IOException {
501        int n = 0;
502        n |= (read(is) << 0);
503        n |= (read(is) << 8);
504        n |= (read(is) << 16);
505        n |= (read(is) << 24);
506        return n;
507    }
508
509    static void writeLong(OutputStream os, long n) throws IOException {
510        os.write((byte)(n >>> 0));
511        os.write((byte)(n >>> 8));
512        os.write((byte)(n >>> 16));
513        os.write((byte)(n >>> 24));
514        os.write((byte)(n >>> 32));
515        os.write((byte)(n >>> 40));
516        os.write((byte)(n >>> 48));
517        os.write((byte)(n >>> 56));
518    }
519
520    static long readLong(InputStream is) throws IOException {
521        long n = 0;
522        n |= ((read(is) & 0xFFL) << 0);
523        n |= ((read(is) & 0xFFL) << 8);
524        n |= ((read(is) & 0xFFL) << 16);
525        n |= ((read(is) & 0xFFL) << 24);
526        n |= ((read(is) & 0xFFL) << 32);
527        n |= ((read(is) & 0xFFL) << 40);
528        n |= ((read(is) & 0xFFL) << 48);
529        n |= ((read(is) & 0xFFL) << 56);
530        return n;
531    }
532
533    static void writeString(OutputStream os, String s) throws IOException {
534        byte[] b = s.getBytes("UTF-8");
535        writeLong(os, b.length);
536        os.write(b, 0, b.length);
537    }
538
539    static String readString(InputStream is) throws IOException {
540        int n = (int) readLong(is);
541        byte[] b = streamToBytes(is, n);
542        return new String(b, "UTF-8");
543    }
544
545    static void writeStringStringMap(Map<String, String> map, OutputStream os) throws IOException {
546        if (map != null) {
547            writeInt(os, map.size());
548            for (Map.Entry<String, String> entry : map.entrySet()) {
549                writeString(os, entry.getKey());
550                writeString(os, entry.getValue());
551            }
552        } else {
553            writeInt(os, 0);
554        }
555    }
556
557    static Map<String, String> readStringStringMap(InputStream is) throws IOException {
558        int size = readInt(is);
559        Map<String, String> result = (size == 0)
560                ? Collections.<String, String>emptyMap()
561                : new HashMap<String, String>(size);
562        for (int i = 0; i < size; i++) {
563            String key = readString(is).intern();
564            String value = readString(is).intern();
565            result.put(key, value);
566        }
567        return result;
568    }
569
570
571}
572