com.android.sdklib.internal.repository
Class DownloadCache

java.lang.Object
  extended by com.android.sdklib.internal.repository.DownloadCache

public class DownloadCache
extends java.lang.Object

A simple cache for the XML resources handled by the SDK Manager.

Callers should use openDirectUrl(java.lang.String, org.apache.http.Header[], com.android.sdklib.internal.repository.ITaskMonitor) to download "large files" that should not be cached (like actual installation packages which are several MBs big) and call openCachedUrl(String, ITaskMonitor) to download small XML files.

The cache can work in 3 different strategies (direct is a pass-through, fresh-cache is the default and tries to update resources if they are older than 10 minutes by respecting either ETag or Last-Modified, and finally server-cache is a strategy to always serve cached entries if present.)


Nested Class Summary
static class DownloadCache.Strategy
           
 
Constructor Summary
DownloadCache(DownloadCache.Strategy strategy)
          Creates a default instance of the URL cache
 
Method Summary
 void clearCache()
          Removes all cached files from the cache directory.
 void clearOldCache()
          Removes all obsolete cached files from the cache directory that do not match the latest revision.
 java.io.File getCacheRoot()
           
 long getCurrentSize()
          Computes the size of the cached files.
 DownloadCache.Strategy getStrategy()
           
protected  java.io.File initCacheRoot()
          Returns the directory to be used as a cache.
 java.io.InputStream openCachedUrl(java.lang.String urlString, ITaskMonitor monitor)
          Downloads a small file, typically XML manifests.
 com.android.utils.Pair<java.io.InputStream,org.apache.http.HttpResponse> openDirectUrl(java.lang.String urlString, org.apache.http.Header[] headers, ITaskMonitor monitor)
          Does a direct download of the given URL using UrlOpener.
 com.android.utils.Pair<java.io.InputStream,java.lang.Integer> openDirectUrl(java.lang.String urlString, ITaskMonitor monitor)
          This is a simplified convenience method that calls openDirectUrl(String, Header[], ITaskMonitor) without passing any specific HTTP headers and returns the resulting input stream and the HTTP status code.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DownloadCache

public DownloadCache(DownloadCache.Strategy strategy)
Creates a default instance of the URL cache

Method Detail

getStrategy

public DownloadCache.Strategy getStrategy()

getCacheRoot

public java.io.File getCacheRoot()

getCurrentSize

public long getCurrentSize()
Computes the size of the cached files.

Returns:
The sum of the byte size of the cached files.

clearCache

public void clearCache()
Removes all cached files from the cache directory.


clearOldCache

public void clearOldCache()
Removes all obsolete cached files from the cache directory that do not match the latest revision.


initCacheRoot

protected java.io.File initCacheRoot()
Returns the directory to be used as a cache. Creates it if necessary. Makes it possible to disable or override the cache location in unit tests.

Returns:
An existing directory to use as a cache root dir, or null in case of error in which case the cache will be disabled.

openDirectUrl

public com.android.utils.Pair<java.io.InputStream,org.apache.http.HttpResponse> openDirectUrl(@NonNull
                                                                                              java.lang.String urlString,
                                                                                              @Nullable
                                                                                              org.apache.http.Header[] headers,
                                                                                              @NonNull
                                                                                              ITaskMonitor monitor)
                                                                                       throws java.io.IOException,
                                                                                              CanceledByUserException
Does a direct download of the given URL using UrlOpener. This does not check the download cache and does not attempt to cache the file. Instead the HttpClient library returns a progressive download stream.

For details on realm authentication and user/password handling, check the underlying UrlOpener.openUrl(String, boolean, ITaskMonitor, Header[]) documentation.

The resulting input stream may not support mark/reset.

Parameters:
urlString - the URL string to be opened.
headers - An optional set of headers to pass when requesting the resource. Can be null.
monitor - ITaskMonitor which is related to this URL fetching.
Returns:
Returns a pair with a InputStream and an HttpResponse. The pair is never null. The input stream can be null in case of error, although in general the method will probably throw an exception instead. The caller should look at the response code's status and only accept the input stream if it's the desired code (e.g. 200 or 206).
Throws:
java.io.IOException - Exception thrown when there are problems retrieving the URL or its content.
CanceledByUserException - Exception thrown if the user cancels the authentication dialog.

openDirectUrl

public com.android.utils.Pair<java.io.InputStream,java.lang.Integer> openDirectUrl(@NonNull
                                                                                   java.lang.String urlString,
                                                                                   @NonNull
                                                                                   ITaskMonitor monitor)
                                                                            throws java.io.IOException,
                                                                                   CanceledByUserException
This is a simplified convenience method that calls openDirectUrl(String, Header[], ITaskMonitor) without passing any specific HTTP headers and returns the resulting input stream and the HTTP status code. See the original method's description for details on its behavior.

openDirectUrl(String, Header[], ITaskMonitor) can accept customized HTTP headers to send with the requests and also returns the full HTTP response -- status line with code and protocol and all headers.

The resulting input stream may not support mark/reset.

Parameters:
urlString - the URL string to be opened.
monitor - ITaskMonitor which is related to this URL fetching.
Returns:
Returns a pair with a InputStream and an HTTP status code. The pair is never null. The input stream can be null in case of error, although in general the method will probably throw an exception instead. The caller should look at the response code's status and only accept the input stream if it's the desired code (e.g. 200 or 206).
Throws:
java.io.IOException - Exception thrown when there are problems retrieving the URL or its content.
CanceledByUserException - Exception thrown if the user cancels the authentication dialog.
See Also:
openDirectUrl(String, Header[], ITaskMonitor)

openCachedUrl

public java.io.InputStream openCachedUrl(java.lang.String urlString,
                                         ITaskMonitor monitor)
                                  throws java.io.IOException,
                                         CanceledByUserException
Downloads a small file, typically XML manifests. The current DownloadCache.Strategy governs whether the file is served as-is from the cache, potentially updated first or directly downloaded.

For large downloads (e.g. installable archives) please do not invoke the cache and instead use the openDirectUrl(java.lang.String, org.apache.http.Header[], com.android.sdklib.internal.repository.ITaskMonitor) method.

For details on realm authentication and user/password handling, check the underlying UrlOpener.openUrl(String, boolean, ITaskMonitor, Header[]) documentation.

Parameters:
urlString - the URL string to be opened.
monitor - ITaskMonitor which is related to this URL fetching.
Returns:
Returns an InputStream holding the URL content. Returns null if there's no content (e.g. resource not found.) Returns null if the document is not cached and strategy is DownloadCache.Strategy.ONLY_CACHE.
Throws:
java.io.IOException - Exception thrown when there are problems retrieving the URL or its content.
CanceledByUserException - Exception thrown if the user cancels the authentication dialog.