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.File;
25import java.io.FileInputStream;
26import java.io.FileOutputStream;
27import java.io.FilterInputStream;
28import java.io.IOException;
29import java.io.InputStream;
30import java.io.ObjectInputStream;
31import java.io.ObjectOutputStream;
32import java.io.OutputStream;
33import java.util.Iterator;
34import java.util.LinkedHashMap;
35import java.util.Map;
36
37/**
38 * Cache implementation that caches files directly onto the hard disk in the specified
39 * directory. The default disk usage size is 5MB, but is configurable.
40 */
41public class DiskBasedCache implements Cache {
42
43    /** Map of the Key, CacheHeader pairs */
44    private final Map<String, CacheHeader> mEntries =
45            new LinkedHashMap<String, CacheHeader>(16, .75f, true);
46
47    /** Total amount of space currently used by the cache in bytes. */
48    private long mTotalSize = 0;
49
50    /** The root directory to use for the cache. */
51    private final File mRootDirectory;
52
53    /** The maximum size of the cache in bytes. */
54    private final int mMaxCacheSizeInBytes;
55
56    /** Default maximum disk usage in bytes. */
57    private static final int DEFAULT_DISK_USAGE_BYTES = 5 * 1024 * 1024;
58
59    /** High water mark percentage for the cache */
60    private static final float HYSTERESIS_FACTOR = 0.9f;
61
62    /** Current cache version */
63    private static final int CACHE_VERSION = 1;
64
65    /**
66     * Constructs an instance of the DiskBasedCache at the specified directory.
67     * @param rootDirectory The root directory of the cache.
68     * @param maxCacheSizeInBytes The maximum size of the cache in bytes.
69     */
70    public DiskBasedCache(File rootDirectory, int maxCacheSizeInBytes) {
71        mRootDirectory = rootDirectory;
72        mMaxCacheSizeInBytes = maxCacheSizeInBytes;
73    }
74
75    /**
76     * Constructs an instance of the DiskBasedCache at the specified directory using
77     * the default maximum cache size of 5MB.
78     * @param rootDirectory The root directory of the cache.
79     */
80    public DiskBasedCache(File rootDirectory) {
81        this(rootDirectory, DEFAULT_DISK_USAGE_BYTES);
82    }
83
84    /**
85     * Clears the cache. Deletes all cached files from disk.
86     */
87    @Override
88    public synchronized void clear() {
89        File[] files = mRootDirectory.listFiles();
90        if (files != null) {
91            for (File file : files) {
92                file.delete();
93            }
94        }
95        mEntries.clear();
96        mTotalSize = 0;
97        VolleyLog.d("Cache cleared.");
98    }
99
100    /**
101     * Returns the cache entry with the specified key if it exists, null otherwise.
102     */
103    @Override
104    public synchronized Entry get(String key) {
105        CacheHeader entry = mEntries.get(key);
106        // if the entry does not exist, return.
107        if (entry == null) {
108            return null;
109        }
110
111        File file = getFileForKey(key);
112        CountingInputStream cis = null;
113        try {
114            cis = new CountingInputStream(new FileInputStream(file));
115            CacheHeader.readHeader(cis); // eat header
116            byte[] data = streamToBytes(cis, (int) (file.length() - cis.bytesRead));
117            return entry.toCacheEntry(data);
118        } catch (IOException e) {
119            VolleyLog.d("%s: %s", file.getAbsolutePath(), e.toString());
120            remove(key);
121            return null;
122        } finally {
123            if (cis != null) {
124                try {
125                    cis.close();
126                } catch (IOException ioe) {
127                    return null;
128                }
129            }
130        }
131    }
132
133    /**
134     * Initializes the DiskBasedCache by scanning for all files currently in the
135     * specified root directory.
136     */
137    @Override
138    public synchronized void initialize() {
139        File[] files = mRootDirectory.listFiles();
140        if (files == null) {
141            return;
142        }
143        for (File file : files) {
144            FileInputStream fis = null;
145            try {
146                fis = new FileInputStream(file);
147                CacheHeader entry = CacheHeader.readHeader(fis);
148                entry.size = file.length();
149                putEntry(entry.key, entry);
150            } catch (IOException e) {
151                if (file != null) {
152                   file.delete();
153                }
154            } finally {
155                try {
156                    if (fis != null) {
157                        fis.close();
158                    }
159                } catch (IOException ignored) { }
160            }
161        }
162    }
163
164    /**
165     * Invalidates an entry in the cache.
166     * @param key Cache key
167     * @param fullExpire True to fully expire the entry, false to soft expire
168     */
169    @Override
170    public synchronized void invalidate(String key, boolean fullExpire) {
171        Entry entry = get(key);
172        if (entry != null) {
173            entry.softTtl = 0;
174            if (fullExpire) {
175                entry.ttl = 0;
176            }
177            put(key, entry);
178        }
179
180    }
181
182    /**
183     * Puts the entry with the specified key into the cache.
184     */
185    @Override
186    public synchronized void put(String key, Entry entry) {
187        pruneIfNeeded(entry.data.length);
188        File file = getFileForKey(key);
189        try {
190            FileOutputStream fos = new FileOutputStream(file);
191            CacheHeader e = new CacheHeader(key, entry);
192            e.writeHeader(fos);
193            fos.write(entry.data);
194            fos.close();
195            putEntry(key, e);
196            return;
197        } catch (IOException e) {
198        }
199        boolean deleted = file.delete();
200        if (!deleted) {
201            VolleyLog.d("Could not clean up file %s", file.getAbsolutePath());
202        }
203    }
204
205    /**
206     * Removes the specified key from the cache if it exists.
207     */
208    @Override
209    public synchronized void remove(String key) {
210        boolean deleted = getFileForKey(key).delete();
211        removeEntry(key);
212        if (!deleted) {
213            VolleyLog.d("Could not delete cache entry for key=%s, filename=%s",
214                    key, getFilenameForKey(key));
215        }
216    }
217
218    /**
219     * Creates a pseudo-unique filename for the specified cache key.
220     * @param key The key to generate a file name for.
221     * @return A pseudo-unique filename.
222     */
223    private String getFilenameForKey(String key) {
224        int firstHalfLength = key.length() / 2;
225        String localFilename = String.valueOf(key.substring(0, firstHalfLength).hashCode());
226        localFilename += String.valueOf(key.substring(firstHalfLength).hashCode());
227        return localFilename;
228    }
229
230    /**
231     * Returns a file object for the given cache key.
232     */
233    public File getFileForKey(String key) {
234        return new File(mRootDirectory, getFilenameForKey(key));
235    }
236
237    /**
238     * Prunes the cache to fit the amount of bytes specified.
239     * @param neededSpace The amount of bytes we are trying to fit into the cache.
240     */
241    private void pruneIfNeeded(int neededSpace) {
242        if ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes) {
243            return;
244        }
245        if (VolleyLog.DEBUG) {
246            VolleyLog.v("Pruning old cache entries.");
247        }
248
249        long before = mTotalSize;
250        int prunedFiles = 0;
251        long startTime = SystemClock.elapsedRealtime();
252
253        Iterator<Map.Entry<String, CacheHeader>> iterator = mEntries.entrySet().iterator();
254        while (iterator.hasNext()) {
255            Map.Entry<String, CacheHeader> entry = iterator.next();
256            CacheHeader e = entry.getValue();
257            boolean deleted = getFileForKey(e.key).delete();
258            if (deleted) {
259                mTotalSize -= e.size;
260            } else {
261               VolleyLog.d("Could not delete cache entry for key=%s, filename=%s",
262                       e.key, getFilenameForKey(e.key));
263            }
264            iterator.remove();
265            prunedFiles++;
266
267            if ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes * HYSTERESIS_FACTOR) {
268                break;
269            }
270        }
271
272        if (VolleyLog.DEBUG) {
273            VolleyLog.v("pruned %d files, %d bytes, %d ms",
274                    prunedFiles, (mTotalSize - before), SystemClock.elapsedRealtime() - startTime);
275        }
276    }
277
278    /**
279     * Puts the entry with the specified key into the cache.
280     * @param key The key to identify the entry by.
281     * @param entry The entry to cache.
282     */
283    private void putEntry(String key, CacheHeader entry) {
284        if (!mEntries.containsKey(key)) {
285            mTotalSize += entry.size;
286        } else {
287            CacheHeader oldEntry = mEntries.get(key);
288            mTotalSize += (entry.size - oldEntry.size);
289        }
290        mEntries.put(key, entry);
291    }
292
293    /**
294     * Removes the entry identified by 'key' from the cache.
295     */
296    private void removeEntry(String key) {
297        CacheHeader entry = mEntries.get(key);
298        if (entry != null) {
299            mTotalSize -= entry.size;
300            mEntries.remove(key);
301        }
302    }
303
304    /**
305     * Reads the contents of an InputStream into a byte[].
306     * */
307    private static byte[] streamToBytes(InputStream in, int length) throws IOException {
308        byte[] bytes = new byte[length];
309        int count;
310        int pos = 0;
311        while (pos < length && ((count = in.read(bytes, pos, length - pos)) != -1)) {
312            pos += count;
313        }
314        if (pos != length) {
315            throw new IOException("Expected " + length + " bytes, read " + pos + " bytes");
316        }
317        return bytes;
318    }
319
320    /**
321     * Handles holding onto the cache headers for an entry.
322     */
323    private static class CacheHeader {
324        /** The size of the data identified by this CacheHeader. (This is not
325         * serialized to disk. */
326        public long size;
327
328        /** The key that identifies the cache entry. */
329        public String key;
330
331        /** ETag for cache coherence. */
332        public String etag;
333
334        /** Date of this response as reported by the server. */
335        public long serverDate;
336
337        /** TTL for this record. */
338        public long ttl;
339
340        /** Soft TTL for this record. */
341        public long softTtl;
342
343        private CacheHeader() { }
344
345        /**
346         * Instantiates a new CacheHeader object
347         * @param key The key that identifies the cache entry
348         * @param entry The cache entry.
349         */
350        public CacheHeader(String key, Entry entry) {
351            this.key = key;
352            this.size = entry.data.length;
353            this.etag = entry.etag;
354            this.serverDate = entry.serverDate;
355            this.ttl = entry.ttl;
356            this.softTtl = entry.softTtl;
357        }
358
359        /**
360         * Reads the header off of an InputStream and returns a CacheHeader object.
361         * @param is The InputStream to read from.
362         * @throws IOException
363         */
364        public static CacheHeader readHeader(InputStream is) throws IOException {
365            CacheHeader entry = new CacheHeader();
366            ObjectInputStream ois = new ObjectInputStream(is);
367            int version = ois.readByte();
368            if (version != CACHE_VERSION) {
369                // don't bother deleting, it'll get pruned eventually
370                throw new IOException();
371            }
372            entry.key = ois.readUTF();
373            entry.etag = ois.readUTF();
374            if (entry.etag.equals("")) {
375                entry.etag = null;
376            }
377            entry.serverDate = ois.readLong();
378            entry.ttl = ois.readLong();
379            entry.softTtl = ois.readLong();
380            return entry;
381        }
382
383        /**
384         * Creates a cache entry for the specified data.
385         */
386        public Entry toCacheEntry(byte[] data) {
387            Entry e = new Entry();
388            e.data = data;
389            e.etag = etag;
390            e.serverDate = serverDate;
391            e.ttl = ttl;
392            e.softTtl = softTtl;
393            return e;
394        }
395
396        /**
397         * Writes the contents of this CacheHeader to the specified OutputStream.
398         */
399        public boolean writeHeader(OutputStream os) {
400            try {
401                ObjectOutputStream oos = new ObjectOutputStream(os);
402                oos.writeByte(CACHE_VERSION);
403                oos.writeUTF(key);
404                oos.writeUTF(etag == null ? "" : etag);
405                oos.writeLong(serverDate);
406                oos.writeLong(ttl);
407                oos.writeLong(softTtl);
408                oos.flush();
409                return true;
410            } catch (IOException e) {
411                VolleyLog.d("%s", e.toString());
412                return false;
413            }
414        }
415    }
416
417    private static class CountingInputStream extends FilterInputStream {
418        private int bytesRead = 0;
419
420        private CountingInputStream(InputStream in) {
421            super(in);
422        }
423
424        @Override
425        public int read() throws IOException {
426            int result = super.read();
427            if (result != -1) {
428                bytesRead++;
429            }
430            return result;
431        }
432
433        @Override
434        public int read(byte[] buffer, int offset, int count) throws IOException {
435            int result = super.read(buffer, offset, count);
436            if (result != -1) {
437                bytesRead += result;
438            }
439            return result;
440        }
441    }
442}
443