public OutputStream openPutStream(final String key) throws IOException { return openPutStream(key, null); }
public InputStream interceptAndPut(String key, InputStream input) throws IOException { OutputStream output = openPutStream(key); return new CopyingInputStream(input, output); }
/** * NOTE: This MUST be called ONLY via the SerializeToDiskWorkItem class to ensure that it * happens on the right thread, at the right time. */ private static void serializeToDiskSynchronously(String cacheKey, String controllerJson) { OutputStream outputStream = null; try { outputStream = controllerDiskCache.openPutStream(cacheKey); outputStream.write(controllerJson.getBytes()); } catch (IOException e) { Log.e(TAG, "Unable to serialize controller to disk", e); } finally { if (outputStream != null) { Utility.closeQuietly(outputStream); } } }
static void cacheUriRedirect(Uri fromUri, Uri toUri) { if (fromUri == null || toUri == null) { return; } OutputStream redirectStream = null; try { FileLruCache cache = getCache(); redirectStream = cache.openPutStream(fromUri.toString(), REDIRECT_CONTENT_TAG); redirectStream.write(toUri.toString().getBytes()); } catch (IOException e) { // Caching is best effort } finally { Utility.closeQuietly(redirectStream); } }
void put(FileLruCache cache, String key, byte[] data) { try { OutputStream stream = cache.openPutStream(key); assertNotNull(stream); stream.write(data); stream.close(); } catch (IOException e) { // Fail test and print Exception assertNull(e); } }
@Test public void testCacheClearMidBuffer() throws Exception { int dataSize = 1024; byte[] data = generateBytes(dataSize); String key = "a"; String key2 = "b"; // Limit to 2x to allow for extra header data FileLruCache cache = new FileLruCache("testCacheClear", limitCacheSize(2*dataSize)); try { TestUtils.clearFileLruCache(cache); put(cache, key, data); checkValue(cache, key, data); OutputStream stream = cache.openPutStream(key2); Thread.sleep(200); TestUtils.clearFileLruCache(cache); stream.write(data); stream.close(); assertEquals(false, hasValue(cache, key)); assertEquals(false, hasValue(cache, key2)); assertEquals(0, cache.sizeInBytesForTest()); } finally { TestUtils.clearAndDeleteLruCacheDirectory(cache); } }
OutputStream openPutStream(final String key) throws IOException { return openPutStream(key, null); }
public InputStream interceptAndPut(String key, InputStream input) throws IOException { OutputStream output = openPutStream(key); return new CopyingInputStream(input, output); }
static void cacheUriRedirect(Context context, URI fromUri, URI toUri) { if (fromUri == null || toUri == null) { return; } OutputStream redirectStream = null; try { FileLruCache cache = getCache(context); redirectStream = cache.openPutStream(fromUri.toString(), REDIRECT_CONTENT_TAG); redirectStream.write(toUri.toString().getBytes()); } catch (IOException e) { // Caching is best effort } finally { Utility.closeQuietly(redirectStream); } }