1 /* 2 * Copyright (C) 2015 Square, Inc. 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 package com.squareup.okhttp; 17 18 import com.squareup.okhttp.internal.huc.JavaApiConverter; 19 20 import java.io.File; 21 import java.io.IOException; 22 import java.net.CacheRequest; 23 import java.net.CacheResponse; 24 import java.net.ResponseCache; 25 import java.net.URI; 26 import java.net.URLConnection; 27 import java.util.List; 28 import java.util.Map; 29 30 /** 31 * A class provided for use by Android so that it can continue supporting a {@link ResponseCache} 32 * with stats. 33 */ 34 public class AndroidShimResponseCache extends ResponseCache { 35 36 private final Cache delegate; 37 AndroidShimResponseCache(Cache delegate)38 private AndroidShimResponseCache(Cache delegate) { 39 this.delegate = delegate; 40 } 41 create(File directory, long maxSize)42 public static AndroidShimResponseCache create(File directory, long maxSize) throws IOException { 43 Cache cache = new Cache(directory, maxSize); 44 return new AndroidShimResponseCache(cache); 45 } 46 isEquivalent(File directory, long maxSize)47 public boolean isEquivalent(File directory, long maxSize) { 48 Cache installedCache = getCache(); 49 return (installedCache.getDirectory().equals(directory) 50 && installedCache.getMaxSize() == maxSize 51 && !installedCache.isClosed()); 52 } 53 getCache()54 public Cache getCache() { 55 return delegate; 56 } 57 get(URI uri, String requestMethod, Map<String, List<String>> requestHeaders)58 @Override public CacheResponse get(URI uri, String requestMethod, 59 Map<String, List<String>> requestHeaders) throws IOException { 60 Request okRequest = JavaApiConverter.createOkRequest(uri, requestMethod, requestHeaders); 61 Response okResponse = delegate.internalCache.get(okRequest); 62 if (okResponse == null) { 63 return null; 64 } 65 return JavaApiConverter.createJavaCacheResponse(okResponse); 66 } 67 put(URI uri, URLConnection urlConnection)68 @Override public CacheRequest put(URI uri, URLConnection urlConnection) throws IOException { 69 Response okResponse = JavaApiConverter.createOkResponseForCachePut(uri, urlConnection); 70 if (okResponse == null) { 71 // The URLConnection is not cacheable or could not be converted. Stop. 72 return null; 73 } 74 com.squareup.okhttp.internal.http.CacheRequest okCacheRequest = 75 delegate.internalCache.put(okResponse); 76 if (okCacheRequest == null) { 77 return null; 78 } 79 return JavaApiConverter.createJavaCacheRequest(okCacheRequest); 80 } 81 82 /** 83 * Returns the number of bytes currently being used to store the values in 84 * this cache. This may be greater than the {@link #maxSize} if a background 85 * deletion is pending. 86 */ size()87 public long size() throws IOException { 88 return delegate.getSize(); 89 } 90 91 /** 92 * Returns the maximum number of bytes that this cache should use to store 93 * its data. 94 */ maxSize()95 public long maxSize() { 96 return delegate.getMaxSize(); 97 } 98 99 /** 100 * Force buffered operations to the filesystem. This ensures that responses 101 * written to the cache will be available the next time the cache is opened, 102 * even if this process is killed. 103 */ flush()104 public void flush() throws IOException { 105 delegate.flush(); 106 } 107 108 /** 109 * Returns the number of HTTP requests that required the network to either 110 * supply a response or validate a locally cached response. 111 */ getNetworkCount()112 public int getNetworkCount() { 113 return delegate.getNetworkCount(); 114 } 115 116 /** 117 * Returns the number of HTTP requests whose response was provided by the 118 * cache. This may include conditional {@code GET} requests that were 119 * validated over the network. 120 */ getHitCount()121 public int getHitCount() { 122 return delegate.getHitCount(); 123 } 124 125 /** 126 * Returns the total number of HTTP requests that were made. This includes 127 * both client requests and requests that were made on the client's behalf 128 * to handle a redirects and retries. 129 */ getRequestCount()130 public int getRequestCount() { 131 return delegate.getRequestCount(); 132 } 133 134 /** 135 * Uninstalls the cache and releases any active resources. Stored contents 136 * will remain on the filesystem. 137 */ close()138 public void close() throws IOException { 139 delegate.close(); 140 } 141 142 /** 143 * Uninstalls the cache and deletes all of its stored contents. 144 */ delete()145 public void delete() throws IOException { 146 delegate.delete(); 147 } 148 149 } 150