com.android.volley.toolbox
Class DiskBasedCache

java.lang.Object
  extended by com.android.volley.toolbox.DiskBasedCache
All Implemented Interfaces:
Cache

public class DiskBasedCache
extends Object
implements Cache

Cache implementation that caches files directly onto the hard disk in the specified directory. The default disk usage size is 5MB, but is configurable.


Nested Class Summary
(package private) static class DiskBasedCache.CacheHeader
          Handles holding onto the cache headers for an entry.
 
Nested classes/interfaces inherited from interface com.android.volley.Cache
Cache.Entry
 
Constructor Summary
DiskBasedCache(File rootDirectory)
          Constructs an instance of the DiskBasedCache at the specified directory using the default maximum cache size of 5MB.
DiskBasedCache(File rootDirectory, int maxCacheSizeInBytes)
          Constructs an instance of the DiskBasedCache at the specified directory.
 
Method Summary
 void clear()
          Clears the cache.
 Cache.Entry get(String key)
          Returns the cache entry with the specified key if it exists, null otherwise.
 File getFileForKey(String key)
          Returns a file object for the given cache key.
 void initialize()
          Initializes the DiskBasedCache by scanning for all files currently in the specified root directory.
 void invalidate(String key, boolean fullExpire)
          Invalidates an entry in the cache.
 void put(String key, Cache.Entry entry)
          Puts the entry with the specified key into the cache.
(package private) static int readInt(InputStream is)
           
(package private) static long readLong(InputStream is)
           
(package private) static String readString(InputStream is)
           
(package private) static Map<String,String> readStringStringMap(InputStream is)
           
 void remove(String key)
          Removes the specified key from the cache if it exists.
(package private) static void writeInt(OutputStream os, int n)
           
(package private) static void writeLong(OutputStream os, long n)
           
(package private) static void writeString(OutputStream os, String s)
           
(package private) static void writeStringStringMap(Map<String,String> map, OutputStream os)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DiskBasedCache

public DiskBasedCache(File rootDirectory,
                      int maxCacheSizeInBytes)
Constructs an instance of the DiskBasedCache at the specified directory.

Parameters:
rootDirectory - The root directory of the cache.
maxCacheSizeInBytes - The maximum size of the cache in bytes.

DiskBasedCache

public DiskBasedCache(File rootDirectory)
Constructs an instance of the DiskBasedCache at the specified directory using the default maximum cache size of 5MB.

Parameters:
rootDirectory - The root directory of the cache.
Method Detail

clear

public void clear()
Clears the cache. Deletes all cached files from disk.

Specified by:
clear in interface Cache

get

public Cache.Entry get(String key)
Returns the cache entry with the specified key if it exists, null otherwise.

Specified by:
get in interface Cache
Parameters:
key - Cache key
Returns:
An Cache.Entry or null in the event of a cache miss

initialize

public void initialize()
Initializes the DiskBasedCache by scanning for all files currently in the specified root directory. Creates the root directory if necessary.

Specified by:
initialize in interface Cache

invalidate

public void invalidate(String key,
                       boolean fullExpire)
Invalidates an entry in the cache.

Specified by:
invalidate in interface Cache
Parameters:
key - Cache key
fullExpire - True to fully expire the entry, false to soft expire

put

public void put(String key,
                Cache.Entry entry)
Puts the entry with the specified key into the cache.

Specified by:
put in interface Cache
Parameters:
key - Cache key
entry - Data to store and metadata for cache coherency, TTL, etc.

remove

public void remove(String key)
Removes the specified key from the cache if it exists.

Specified by:
remove in interface Cache
Parameters:
key - Cache key

getFileForKey

public File getFileForKey(String key)
Returns a file object for the given cache key.


writeInt

static void writeInt(OutputStream os,
                     int n)
              throws IOException
Throws:
IOException

readInt

static int readInt(InputStream is)
            throws IOException
Throws:
IOException

writeLong

static void writeLong(OutputStream os,
                      long n)
               throws IOException
Throws:
IOException

readLong

static long readLong(InputStream is)
              throws IOException
Throws:
IOException

writeString

static void writeString(OutputStream os,
                        String s)
                 throws IOException
Throws:
IOException

readString

static String readString(InputStream is)
                  throws IOException
Throws:
IOException

writeStringStringMap

static void writeStringStringMap(Map<String,String> map,
                                 OutputStream os)
                          throws IOException
Throws:
IOException

readStringStringMap

static Map<String,String> readStringStringMap(InputStream is)
                                       throws IOException
Throws:
IOException