Loading core/java/android/net/http/HttpResponseCache.java +33 −31 Original line number Original line Diff line number Diff line Loading @@ -16,9 +16,8 @@ package android.net.http; package android.net.http; import com.android.okhttp.Cache; import com.android.okhttp.internalandroidapi.AndroidResponseCacheAdapter; import com.android.okhttp.AndroidShimResponseCache; import com.android.okhttp.internalandroidapi.HasCacheHolder; import com.android.okhttp.OkCacheContainer; import java.io.Closeable; import java.io.Closeable; import java.io.File; import java.io.File; Loading Loading @@ -149,12 +148,12 @@ import java.util.Map; * } catch (Exception httpResponseCacheNotAvailable) { * } catch (Exception httpResponseCacheNotAvailable) { * }}</pre> * }}</pre> */ */ public final class HttpResponseCache extends ResponseCache implements Closeable, OkCacheContainer { public final class HttpResponseCache extends ResponseCache implements HasCacheHolder, Closeable { private final AndroidShimResponseCache delegate; private final AndroidResponseCacheAdapter mDelegate; private HttpResponseCache(AndroidShimResponseCache delegate) { private HttpResponseCache(AndroidResponseCacheAdapter delegate) { this.delegate = delegate; mDelegate = delegate; } } /** /** Loading Loading @@ -184,30 +183,33 @@ public final class HttpResponseCache extends ResponseCache implements Closeable, ResponseCache installed = ResponseCache.getDefault(); ResponseCache installed = ResponseCache.getDefault(); if (installed instanceof HttpResponseCache) { if (installed instanceof HttpResponseCache) { HttpResponseCache installedResponseCache = (HttpResponseCache) installed; HttpResponseCache installedResponseCache = (HttpResponseCache) installed; CacheHolder cacheHolder = installedResponseCache.getCacheHolder(); // don't close and reopen if an equivalent cache is already installed // don't close and reopen if an equivalent cache is already installed AndroidShimResponseCache trueResponseCache = installedResponseCache.delegate; if (cacheHolder.isEquivalent(directory, maxSize)) { if (trueResponseCache.isEquivalent(directory, maxSize)) { return installedResponseCache; return installedResponseCache; } else { } else { // The HttpResponseCache that owns this object is about to be replaced. // The HttpResponseCache that owns this object is about to be replaced. trueResponseCache.close(); installedResponseCache.close(); } } } } AndroidShimResponseCache trueResponseCache = CacheHolder cacheHolder = CacheHolder.create(directory, maxSize); AndroidShimResponseCache.create(directory, maxSize); AndroidResponseCacheAdapter androidResponseCacheAdapter = HttpResponseCache newResponseCache = new HttpResponseCache(trueResponseCache); new AndroidResponseCacheAdapter(cacheHolder); ResponseCache.setDefault(newResponseCache); HttpResponseCache responseCache = new HttpResponseCache(androidResponseCacheAdapter); return newResponseCache; ResponseCache.setDefault(responseCache); return responseCache; } } @Override public CacheResponse get(URI uri, String requestMethod, @Override public CacheResponse get(URI uri, String requestMethod, Map<String, List<String>> requestHeaders) throws IOException { Map<String, List<String>> requestHeaders) throws IOException { return delegate.get(uri, requestMethod, requestHeaders); return mDelegate.get(uri, requestMethod, requestHeaders); } } @Override public CacheRequest put(URI uri, URLConnection urlConnection) throws IOException { @Override return delegate.put(uri, urlConnection); public CacheRequest put(URI uri, URLConnection urlConnection) throws IOException { return mDelegate.put(uri, urlConnection); } } /** /** Loading @@ -217,7 +219,7 @@ public final class HttpResponseCache extends ResponseCache implements Closeable, */ */ public long size() { public long size() { try { try { return delegate.size(); return mDelegate.getSize(); } catch (IOException e) { } catch (IOException e) { // This can occur if the cache failed to lazily initialize. // This can occur if the cache failed to lazily initialize. return -1; return -1; Loading @@ -229,7 +231,7 @@ public final class HttpResponseCache extends ResponseCache implements Closeable, * its data. * its data. */ */ public long maxSize() { public long maxSize() { return delegate.maxSize(); return mDelegate.getMaxSize(); } } /** /** Loading @@ -239,7 +241,7 @@ public final class HttpResponseCache extends ResponseCache implements Closeable, */ */ public void flush() { public void flush() { try { try { delegate.flush(); mDelegate.flush(); } catch (IOException ignored) { } catch (IOException ignored) { } } } } Loading @@ -249,7 +251,7 @@ public final class HttpResponseCache extends ResponseCache implements Closeable, * supply a response or validate a locally cached response. * supply a response or validate a locally cached response. */ */ public int getNetworkCount() { public int getNetworkCount() { return delegate.getNetworkCount(); return mDelegate.getNetworkCount(); } } /** /** Loading @@ -258,7 +260,7 @@ public final class HttpResponseCache extends ResponseCache implements Closeable, * validated over the network. * validated over the network. */ */ public int getHitCount() { public int getHitCount() { return delegate.getHitCount(); return mDelegate.getHitCount(); } } /** /** Loading @@ -267,18 +269,19 @@ public final class HttpResponseCache extends ResponseCache implements Closeable, * to handle a redirects and retries. * to handle a redirects and retries. */ */ public int getRequestCount() { public int getRequestCount() { return delegate.getRequestCount(); return mDelegate.getRequestCount(); } } /** /** * Uninstalls the cache and releases any active resources. Stored contents * Uninstalls the cache and releases any active resources. Stored contents * will remain on the filesystem. * will remain on the filesystem. */ */ @Override public void close() throws IOException { @Override public void close() throws IOException { if (ResponseCache.getDefault() == this) { if (ResponseCache.getDefault() == this) { ResponseCache.setDefault(null); ResponseCache.setDefault(null); } } delegate.close(); mDelegate.close(); } } /** /** Loading @@ -288,13 +291,12 @@ public final class HttpResponseCache extends ResponseCache implements Closeable, if (ResponseCache.getDefault() == this) { if (ResponseCache.getDefault() == this) { ResponseCache.setDefault(null); ResponseCache.setDefault(null); } } delegate.delete(); mDelegate.delete(); } } /** @hide Needed for OkHttp integration. */ /** @hide Needed for OkHttp integration. */ @Override @Override public Cache getCache() { public CacheHolder getCacheHolder() { return delegate.getCache(); return mDelegate.getCacheHolder(); } } } } Loading
core/java/android/net/http/HttpResponseCache.java +33 −31 Original line number Original line Diff line number Diff line Loading @@ -16,9 +16,8 @@ package android.net.http; package android.net.http; import com.android.okhttp.Cache; import com.android.okhttp.internalandroidapi.AndroidResponseCacheAdapter; import com.android.okhttp.AndroidShimResponseCache; import com.android.okhttp.internalandroidapi.HasCacheHolder; import com.android.okhttp.OkCacheContainer; import java.io.Closeable; import java.io.Closeable; import java.io.File; import java.io.File; Loading Loading @@ -149,12 +148,12 @@ import java.util.Map; * } catch (Exception httpResponseCacheNotAvailable) { * } catch (Exception httpResponseCacheNotAvailable) { * }}</pre> * }}</pre> */ */ public final class HttpResponseCache extends ResponseCache implements Closeable, OkCacheContainer { public final class HttpResponseCache extends ResponseCache implements HasCacheHolder, Closeable { private final AndroidShimResponseCache delegate; private final AndroidResponseCacheAdapter mDelegate; private HttpResponseCache(AndroidShimResponseCache delegate) { private HttpResponseCache(AndroidResponseCacheAdapter delegate) { this.delegate = delegate; mDelegate = delegate; } } /** /** Loading Loading @@ -184,30 +183,33 @@ public final class HttpResponseCache extends ResponseCache implements Closeable, ResponseCache installed = ResponseCache.getDefault(); ResponseCache installed = ResponseCache.getDefault(); if (installed instanceof HttpResponseCache) { if (installed instanceof HttpResponseCache) { HttpResponseCache installedResponseCache = (HttpResponseCache) installed; HttpResponseCache installedResponseCache = (HttpResponseCache) installed; CacheHolder cacheHolder = installedResponseCache.getCacheHolder(); // don't close and reopen if an equivalent cache is already installed // don't close and reopen if an equivalent cache is already installed AndroidShimResponseCache trueResponseCache = installedResponseCache.delegate; if (cacheHolder.isEquivalent(directory, maxSize)) { if (trueResponseCache.isEquivalent(directory, maxSize)) { return installedResponseCache; return installedResponseCache; } else { } else { // The HttpResponseCache that owns this object is about to be replaced. // The HttpResponseCache that owns this object is about to be replaced. trueResponseCache.close(); installedResponseCache.close(); } } } } AndroidShimResponseCache trueResponseCache = CacheHolder cacheHolder = CacheHolder.create(directory, maxSize); AndroidShimResponseCache.create(directory, maxSize); AndroidResponseCacheAdapter androidResponseCacheAdapter = HttpResponseCache newResponseCache = new HttpResponseCache(trueResponseCache); new AndroidResponseCacheAdapter(cacheHolder); ResponseCache.setDefault(newResponseCache); HttpResponseCache responseCache = new HttpResponseCache(androidResponseCacheAdapter); return newResponseCache; ResponseCache.setDefault(responseCache); return responseCache; } } @Override public CacheResponse get(URI uri, String requestMethod, @Override public CacheResponse get(URI uri, String requestMethod, Map<String, List<String>> requestHeaders) throws IOException { Map<String, List<String>> requestHeaders) throws IOException { return delegate.get(uri, requestMethod, requestHeaders); return mDelegate.get(uri, requestMethod, requestHeaders); } } @Override public CacheRequest put(URI uri, URLConnection urlConnection) throws IOException { @Override return delegate.put(uri, urlConnection); public CacheRequest put(URI uri, URLConnection urlConnection) throws IOException { return mDelegate.put(uri, urlConnection); } } /** /** Loading @@ -217,7 +219,7 @@ public final class HttpResponseCache extends ResponseCache implements Closeable, */ */ public long size() { public long size() { try { try { return delegate.size(); return mDelegate.getSize(); } catch (IOException e) { } catch (IOException e) { // This can occur if the cache failed to lazily initialize. // This can occur if the cache failed to lazily initialize. return -1; return -1; Loading @@ -229,7 +231,7 @@ public final class HttpResponseCache extends ResponseCache implements Closeable, * its data. * its data. */ */ public long maxSize() { public long maxSize() { return delegate.maxSize(); return mDelegate.getMaxSize(); } } /** /** Loading @@ -239,7 +241,7 @@ public final class HttpResponseCache extends ResponseCache implements Closeable, */ */ public void flush() { public void flush() { try { try { delegate.flush(); mDelegate.flush(); } catch (IOException ignored) { } catch (IOException ignored) { } } } } Loading @@ -249,7 +251,7 @@ public final class HttpResponseCache extends ResponseCache implements Closeable, * supply a response or validate a locally cached response. * supply a response or validate a locally cached response. */ */ public int getNetworkCount() { public int getNetworkCount() { return delegate.getNetworkCount(); return mDelegate.getNetworkCount(); } } /** /** Loading @@ -258,7 +260,7 @@ public final class HttpResponseCache extends ResponseCache implements Closeable, * validated over the network. * validated over the network. */ */ public int getHitCount() { public int getHitCount() { return delegate.getHitCount(); return mDelegate.getHitCount(); } } /** /** Loading @@ -267,18 +269,19 @@ public final class HttpResponseCache extends ResponseCache implements Closeable, * to handle a redirects and retries. * to handle a redirects and retries. */ */ public int getRequestCount() { public int getRequestCount() { return delegate.getRequestCount(); return mDelegate.getRequestCount(); } } /** /** * Uninstalls the cache and releases any active resources. Stored contents * Uninstalls the cache and releases any active resources. Stored contents * will remain on the filesystem. * will remain on the filesystem. */ */ @Override public void close() throws IOException { @Override public void close() throws IOException { if (ResponseCache.getDefault() == this) { if (ResponseCache.getDefault() == this) { ResponseCache.setDefault(null); ResponseCache.setDefault(null); } } delegate.close(); mDelegate.close(); } } /** /** Loading @@ -288,13 +291,12 @@ public final class HttpResponseCache extends ResponseCache implements Closeable, if (ResponseCache.getDefault() == this) { if (ResponseCache.getDefault() == this) { ResponseCache.setDefault(null); ResponseCache.setDefault(null); } } delegate.delete(); mDelegate.delete(); } } /** @hide Needed for OkHttp integration. */ /** @hide Needed for OkHttp integration. */ @Override @Override public Cache getCache() { public CacheHolder getCacheHolder() { return delegate.getCache(); return mDelegate.getCacheHolder(); } } } }