Skip to content

Commit

Permalink
Update HttpResponseCache to use the new cache API.
Browse files Browse the repository at this point in the history
The new API was introduced in

https://android-review.googlesource.com/#/c/59985/

Change-Id: Ie64de7fc64b818bebf1077dff407813865769387
  • Loading branch information
narayank committed Jun 12, 2013
1 parent fa6d625 commit 68a3cd7
Showing 1 changed file with 30 additions and 39 deletions.
69 changes: 30 additions & 39 deletions core/java/android/net/http/HttpResponseCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
package android.net.http;

import android.content.Context;
import com.android.okhttp.OkResponseCache;
import com.android.okhttp.ResponseSource;
import com.android.okhttp.internal.DiskLruCache;
import com.android.okhttp.internal.http.OkResponseCache;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -151,13 +151,12 @@
* } catch (Exception httpResponseCacheNotAvailable) {
* }}</pre>
*/
public final class HttpResponseCache extends ResponseCache
implements Closeable, OkResponseCache {
public final class HttpResponseCache extends ResponseCache implements Closeable {

private final com.android.okhttp.internal.http.HttpResponseCache delegate;
private final com.android.okhttp.HttpResponseCache delegate;

private HttpResponseCache(File directory, long maxSize) throws IOException {
this.delegate = new com.android.okhttp.internal.http.HttpResponseCache(directory, maxSize);
private HttpResponseCache(com.android.okhttp.HttpResponseCache delegate) {
this.delegate = delegate;
}

/**
Expand All @@ -166,7 +165,12 @@ private HttpResponseCache(File directory, long maxSize) throws IOException {
*/
public static HttpResponseCache getInstalled() {
ResponseCache installed = ResponseCache.getDefault();
return installed instanceof HttpResponseCache ? (HttpResponseCache) installed : null;
if (installed instanceof com.android.okhttp.HttpResponseCache) {
return new HttpResponseCache(
(com.android.okhttp.HttpResponseCache) installed);
}

return null;
}

/**
Expand All @@ -181,22 +185,25 @@ public static HttpResponseCache getInstalled() {
* warning.
*/
public static HttpResponseCache install(File directory, long maxSize) throws IOException {
HttpResponseCache installed = getInstalled();
if (installed != null) {
ResponseCache installed = ResponseCache.getDefault();
if (installed instanceof com.android.okhttp.HttpResponseCache) {
com.android.okhttp.HttpResponseCache installedCache =
(com.android.okhttp.HttpResponseCache) installed;
// don't close and reopen if an equivalent cache is already installed
DiskLruCache installedCache = installed.delegate.getCache();
if (installedCache.getDirectory().equals(directory)
&& installedCache.maxSize() == maxSize
&& installedCache.getMaxSize() == maxSize
&& !installedCache.isClosed()) {
return installed;
return new HttpResponseCache(installedCache);
} else {
IoUtils.closeQuietly(installed);
// The HttpResponseCache that owns this object is about to be replaced.
installedCache.close();
}
}

HttpResponseCache result = new HttpResponseCache(directory, maxSize);
ResponseCache.setDefault(result);
return result;
com.android.okhttp.HttpResponseCache responseCache =
new com.android.okhttp.HttpResponseCache(directory, maxSize);
ResponseCache.setDefault(responseCache);
return new HttpResponseCache(responseCache);
}

@Override public CacheResponse get(URI uri, String requestMethod,
Expand All @@ -214,15 +221,15 @@ public static HttpResponseCache install(File directory, long maxSize) throws IOE
* deletion is pending.
*/
public long size() {
return delegate.getCache().size();
return delegate.getSize();
}

/**
* Returns the maximum number of bytes that this cache should use to store
* its data.
*/
public long maxSize() {
return delegate.getCache().maxSize();
return delegate.getMaxSize();
}

/**
Expand All @@ -232,7 +239,7 @@ public long maxSize() {
*/
public void flush() {
try {
delegate.getCache().flush();
delegate.flush();
} catch (IOException ignored) {
}
}
Expand Down Expand Up @@ -263,40 +270,24 @@ public int getRequestCount() {
return delegate.getRequestCount();
}

/** @hide */
@Override public void trackResponse(ResponseSource source) {
delegate.trackResponse(source);
}

/** @hide */
@Override public void trackConditionalCacheHit() {
delegate.trackConditionalCacheHit();
}

/** @hide */
@Override public void update(CacheResponse conditionalCacheHit, HttpURLConnection connection)
throws IOException {
delegate.update(conditionalCacheHit, connection);
}

/**
* Uninstalls the cache and releases any active resources. Stored contents
* will remain on the filesystem.
*/
@Override public void close() throws IOException {
if (ResponseCache.getDefault() == this) {
if (ResponseCache.getDefault() == this.delegate) {
ResponseCache.setDefault(null);
}
delegate.getCache().close();
delegate.close();
}

/**
* Uninstalls the cache and deletes all of its stored contents.
*/
public void delete() throws IOException {
if (ResponseCache.getDefault() == this) {
if (ResponseCache.getDefault() == this.delegate) {
ResponseCache.setDefault(null);
}
delegate.getCache().delete();
delegate.delete();
}
}

0 comments on commit 68a3cd7

Please sign in to comment.