diff options
author | Jesse Wilson <jessewilson@google.com> | 2011-05-13 16:41:41 -0700 |
---|---|---|
committer | Jesse Wilson <jessewilson@google.com> | 2011-05-14 10:16:34 -0700 |
commit | fb4a6392a04b1f3a1124b3db6bae51d8cbfa53f8 (patch) | |
tree | fc5d567e33e59feafbbe49e8c8ba6e908f92cf9c /luni | |
parent | 03e4ca2be54b0997575311ddc2ebaf3b3d8252c6 (diff) | |
download | libcore-fb4a6392a04b1f3a1124b3db6bae51d8cbfa53f8.zip libcore-fb4a6392a04b1f3a1124b3db6bae51d8cbfa53f8.tar.gz libcore-fb4a6392a04b1f3a1124b3db6bae51d8cbfa53f8.tar.bz2 |
Move APIs from internal HttpResponseCache to external one.
Change-Id: Ia6c88f292088bfd3ed546c067376fcb36b435b48
http://b/3180373
Diffstat (limited to 'luni')
5 files changed, 69 insertions, 60 deletions
diff --git a/luni/src/main/java/java/lang/IntegralToString.java b/luni/src/main/java/java/lang/IntegralToString.java index a643124..03a5359 100644 --- a/luni/src/main/java/java/lang/IntegralToString.java +++ b/luni/src/main/java/java/lang/IntegralToString.java @@ -465,9 +465,10 @@ public final class IntegralToString { public static String bytesToHexString(byte[] bytes, boolean upperCase) { char[] digits = upperCase ? UPPER_CASE_DIGITS : DIGITS; char[] buf = new char[bytes.length * 2]; + int c = 0; for (byte b : bytes) { - buf[0] = digits[(b >> 4) & 0xf]; - buf[1] = digits[b & 0xf]; + buf[c++] = digits[(b >> 4) & 0xf]; + buf[c++] = digits[b & 0xf]; } return new String(buf); } diff --git a/luni/src/main/java/libcore/io/DiskLruCache.java b/luni/src/main/java/libcore/io/DiskLruCache.java index 10f4ca3..29de727 100644 --- a/luni/src/main/java/libcore/io/DiskLruCache.java +++ b/luni/src/main/java/libcore/io/DiskLruCache.java @@ -84,6 +84,8 @@ import static libcore.io.OsConstants.O_RDONLY; */ public final class DiskLruCache implements Closeable { // TODO: test with fault injection + // TODO: a full disk should fail quietly! + // TODO: missing individual files should fail quietly static final String JOURNAL_FILE = "journal"; static final String JOURNAL_FILE_TMP = "journal.tmp"; @@ -165,7 +167,7 @@ public final class DiskLruCache implements Closeable { } }; - private DiskLruCache(File directory, int appVersion, int valueCount, int maxSize) { + private DiskLruCache(File directory, int appVersion, int valueCount, long maxSize) { this.directory = directory; this.appVersion = appVersion; this.journalFile = new File(directory, JOURNAL_FILE); @@ -182,8 +184,9 @@ public final class DiskLruCache implements Closeable { * @param appVersion * @param valueCount the number of values per cache entry. Must be positive. * @param maxSize the maximum number of bytes this cache should use to store + * @throws IOException if reading or writing the cache directory fails */ - public static DiskLruCache open(File directory, int appVersion, int valueCount, int maxSize) + public static DiskLruCache open(File directory, int appVersion, int valueCount, long maxSize) throws IOException { if (maxSize <= 0) { throw new IllegalArgumentException("maxSize <= 0"); @@ -208,6 +211,7 @@ public final class DiskLruCache implements Closeable { } // create a new empty cache + directory.mkdirs(); cache = new DiskLruCache(directory, appVersion, valueCount, maxSize); cache.rebuildJournal(); return cache; @@ -401,6 +405,21 @@ public final class DiskLruCache implements Closeable { } /** + * Returns the directory where this cache stores its data. + */ + public File getDirectory() { + return directory; + } + + /** + * Returns the maximum number of bytes that this cache should use to store + * its data. + */ + public long maxSize() { + return maxSize; + } + + /** * Returns the number of bytes currently being used to store the values in * this cache. This may be greater than the max size if a background * deletion is pending. @@ -500,6 +519,13 @@ public final class DiskLruCache implements Closeable { return true; } + /** + * Returns true if this cache has been closed. + */ + public boolean isClosed() { + return journalWriter == null; + } + private void checkNotClosed() { if (journalWriter == null) { throw new IllegalStateException("cache is closed"); @@ -507,7 +533,7 @@ public final class DiskLruCache implements Closeable { } /** - * Force all buffered and pending operations to the file system. + * Force buffered operations to the file system. */ public synchronized void flush() throws IOException { checkNotClosed(); diff --git a/luni/src/main/java/libcore/net/http/HttpResponseCache.java b/luni/src/main/java/libcore/net/http/HttpResponseCache.java index dbe39ac..f51b1d0 100644 --- a/luni/src/main/java/libcore/net/http/HttpResponseCache.java +++ b/luni/src/main/java/libcore/net/http/HttpResponseCache.java @@ -19,7 +19,6 @@ package libcore.net.http; import java.io.BufferedInputStream; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; -import java.io.Closeable; import java.io.File; import java.io.FilterInputStream; import java.io.FilterOutputStream; @@ -55,12 +54,11 @@ import libcore.io.IoUtils; import libcore.io.Streams; /** - * Cache responses in a cache directory. + * Cache responses in a directory on the file system. Most clients should use + * {@code android.net.HttpResponseCache}, the stable, documented front end for + * this. */ -public final class HttpResponseCache extends ResponseCache implements Closeable { - // TODO: default max size - // TODO: default cache directory for android apps - // TODO: move this class to android.util +public final class HttpResponseCache extends ResponseCache { // TODO: add APIs to iterate the cache private static final int VERSION = 201105; @@ -77,7 +75,7 @@ public final class HttpResponseCache extends ResponseCache implements Closeable private int hitCount; private int requestCount; - public HttpResponseCache(File directory, int maxSize) throws IOException { + public HttpResponseCache(File directory, long maxSize) throws IOException { cache = DiskLruCache.open(directory, VERSION, ENTRY_COUNT, maxSize); } @@ -160,61 +158,18 @@ public final class HttpResponseCache extends ResponseCache implements Closeable return new CacheRequestImpl(editor); } - /** - * Closes this cache. Stored contents will remain on the filesystem. - */ - public void close() throws IOException { - cache.close(); - } - - /** - * Closes this cache and deletes all of its stored contents. - */ - public void delete() throws IOException { - cache.delete(); + public DiskLruCache getCache() { + return cache; } - /** - * Returns the number of responses that were aborted before they were - * stored. - */ synchronized int getWriteAbortCount() { return writeAbortCount; } - /** - * Returns the number of responses that were stored successfully. - */ synchronized int getWriteSuccessCount() { return writeSuccessCount; } - /** - * Returns the number of HTTP requests that required the network to either - * supply a response or validate a locally cached response. - */ - public synchronized int getNetworkCount() { - return networkCount; - } - - /** - * Returns the number of HTTP requests whose response was provided by the - * cache. This may include conditional {@code GET} requests that were - * validated over the network. - */ - public synchronized int getHitCount() { - return hitCount; - } - - /** - * Returns the total number of HTTP requests that were made. This includes - * both client requests and requests that were made on the client's behalf - * to handle a redirects and retries. - */ - public synchronized int getRequestCount() { - return requestCount; - } - synchronized void trackResponse(ResponseSource source) { requestCount++; @@ -233,6 +188,18 @@ public final class HttpResponseCache extends ResponseCache implements Closeable hitCount++; } + public synchronized int getNetworkCount() { + return networkCount; + } + + public synchronized int getHitCount() { + return hitCount; + } + + public synchronized int getRequestCount() { + return requestCount; + } + private final class CacheRequestImpl extends CacheRequest { private final DiskLruCache.Editor editor; private OutputStream cacheOut; diff --git a/luni/src/test/java/libcore/io/DiskLruCacheTest.java b/luni/src/test/java/libcore/io/DiskLruCacheTest.java index 990076f..812248f 100644 --- a/luni/src/test/java/libcore/io/DiskLruCacheTest.java +++ b/luni/src/test/java/libcore/io/DiskLruCacheTest.java @@ -34,13 +34,15 @@ import static libcore.io.DiskLruCache.VERSION_1; public final class DiskLruCacheTest extends TestCase { private final int appVersion = 100; + private String javaTmpDir; private File cacheDir; private File journalFile; private DiskLruCache cache; @Override public void setUp() throws Exception { super.setUp(); - cacheDir = new File(System.getProperty("java.io.tmpdir"), "DiskLruCacheTest"); + javaTmpDir = System.getProperty("java.io.tmpdir"); + cacheDir = new File(javaTmpDir, "DiskLruCacheTest"); cacheDir.mkdir(); journalFile = new File(cacheDir, JOURNAL_FILE); for (File file : cacheDir.listFiles()) { @@ -557,6 +559,16 @@ public final class DiskLruCacheTest extends TestCase { assertValue("B", "b", "b"); } + public void testOpenCreatesDirectoryIfNecessary() throws Exception { + cache.close(); + File dir = new File(javaTmpDir, "testOpenCreatesDirectoryIfNecessary"); + cache = DiskLruCache.open(dir, appVersion, 2, Integer.MAX_VALUE); + set("A", "a", "a"); + assertTrue(new File(dir, "A.0").exists()); + assertTrue(new File(dir, "A.1").exists()); + assertTrue(new File(dir, "journal").exists()); + } + private void assertJournalEquals(String... expectedBodyLines) throws Exception { List<String> expectedLines = new ArrayList<String>(); expectedLines.add(MAGIC); diff --git a/luni/src/test/java/libcore/net/http/HttpResponseCacheTest.java b/luni/src/test/java/libcore/net/http/HttpResponseCacheTest.java index 266ca89..ba60e0a 100644 --- a/luni/src/test/java/libcore/net/http/HttpResponseCacheTest.java +++ b/luni/src/test/java/libcore/net/http/HttpResponseCacheTest.java @@ -60,6 +60,10 @@ import tests.http.RecordedRequest; import static tests.http.SocketPolicy.DISCONNECT_AT_END; public final class HttpResponseCacheTest extends TestCase { + + // TODO: test cache + cookies + // TODO: test cache + user-provided Range header + private MockWebServer server = new MockWebServer(); private HttpResponseCache cache; @@ -68,7 +72,6 @@ public final class HttpResponseCacheTest extends TestCase { String tmp = System.getProperty("java.io.tmpdir"); File cacheDir = new File(tmp, "HttpCache-" + UUID.randomUUID()); - cacheDir.mkdir(); cache = new HttpResponseCache(cacheDir, Integer.MAX_VALUE); ResponseCache.setDefault(cache); } @@ -76,7 +79,7 @@ public final class HttpResponseCacheTest extends TestCase { @Override protected void tearDown() throws Exception { server.shutdown(); ResponseCache.setDefault(null); - cache.delete(); + cache.getCache().delete(); super.tearDown(); } |