static InputStream getCachedImageStream(Uri uri, Context context) { InputStream imageStream = null; if (uri != null) { if (isCDNURL(uri)) { try { FileLruCache cache = getCache(context); imageStream = cache.get(uri.toString()); } catch (IOException e) { Logger.log(LoggingBehavior.CACHE, Log.WARN, TAG, e.toString()); } } } return imageStream; }
private static void readFromCache( RequestKey key, Context context, boolean allowCachedRedirects) { InputStream cachedStream = null; boolean isCachedRedirect = false; if (allowCachedRedirects) { Uri redirectUri = UrlRedirectCache.getRedirectedUri(key.uri); if (redirectUri != null) { cachedStream = ImageResponseCache.getCachedImageStream(redirectUri, context); isCachedRedirect = cachedStream != null; } } if (!isCachedRedirect) { cachedStream = ImageResponseCache.getCachedImageStream(key.uri, context); } if (cachedStream != null) { // We were able to find a cached image. Bitmap bitmap = BitmapFactory.decodeStream(cachedStream); Utility.closeQuietly(cachedStream); issueResponse(key, null, bitmap, isCachedRedirect); } else { // Once the old downloader context is removed, we are thread-safe since this is the // only reference to it DownloaderContext downloaderContext = removePendingRequest(key); if (downloaderContext != null && !downloaderContext.isCancelled) { enqueueDownload(downloaderContext.request, key); } } }
stream = ImageResponseCache.interceptAndCacheImageStream(context, connection); bitmap = BitmapFactory.decodeStream(stream); break;
private Bitmap readImage(String uri, boolean expectedFromCache) { Bitmap bmp = null; InputStream istream = null; try { Uri url = Uri.parse(uri); // Check if the cache contains value for this url boolean isInCache = (ImageResponseCache.getCache(safeGetContext()).get(url.toString()) != null); assertTrue(isInCache == expectedFromCache); // Read the image istream = ImageResponseCache.getCachedImageStream(url, safeGetContext()); if (istream == null) { HttpURLConnection connection = (HttpURLConnection) (new URL(uri)).openConnection(); istream = ImageResponseCache.interceptAndCacheImageStream( safeGetContext(), connection); } assertTrue(istream != null); bmp = BitmapFactory.decodeStream(istream); assertTrue(bmp != null); } catch (Exception e) { assertNull(e); } finally { Utility.closeQuietly(istream); } return bmp; }
static void clearCache(Context context) { try { getCache(context).clearCache(); } catch (IOException e) { Logger.log(LoggingBehavior.CACHE, Log.WARN, TAG, "clearCache failed " + e.getMessage()); } }
public static void clearCache(Context context) { ImageResponseCache.clearCache(context); UrlRedirectCache.clearCache(); }
static void clearCache(Context context) { try { getCache(context).clearCache(); } catch (IOException e) { Logger.log(LoggingBehavior.CACHE, Log.WARN, TAG, "clearCache failed " + e.getMessage()); } }
public static void clearCache(Context context) { ImageResponseCache.clearCache(context); UrlRedirectCache.clearCache(context); }
static InputStream interceptAndCacheImageStream( Context context, HttpURLConnection connection ) throws IOException { InputStream stream = null; if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { Uri uri = Uri.parse(connection.getURL().toString()); stream = connection.getInputStream(); // Default stream in case caching fails try { if (isCDNURL(uri)) { FileLruCache cache = getCache(context); // Wrap stream with a caching stream stream = cache.interceptAndPut( uri.toString(), new BufferedHttpInputStream(stream, connection)); } } catch (IOException e) { // Caching is best effort } } return stream; }
private static void readFromCache(RequestKey key, Context context, boolean allowCachedRedirects) { InputStream cachedStream = null; boolean isCachedRedirect = false; if (allowCachedRedirects) { URI redirectUri = UrlRedirectCache.getRedirectedUri(context, key.uri); if (redirectUri != null) { cachedStream = ImageResponseCache.getCachedImageStream(redirectUri, context); isCachedRedirect = cachedStream != null; } } if (!isCachedRedirect) { cachedStream = ImageResponseCache.getCachedImageStream(key.uri, context); } if (cachedStream != null) { // We were able to find a cached image. Bitmap bitmap = BitmapFactory.decodeStream(cachedStream); Utility.closeQuietly(cachedStream); issueResponse(key, null, bitmap, isCachedRedirect); } else { // Once the old downloader context is removed, we are thread-safe since this is the // only reference to it DownloaderContext downloaderContext = removePendingRequest(key); if (downloaderContext != null && !downloaderContext.isCancelled) { enqueueDownload(downloaderContext.request, key); } } }
stream = ImageResponseCache.interceptAndCacheImageStream(context, connection); bitmap = BitmapFactory.decodeStream(stream); break;
static InputStream getCachedImageStream(URI url, Context context) { InputStream imageStream = null; if (url != null) { if (isCDNURL(url)) { try { FileLruCache cache = getCache(context); imageStream = cache.get(url.toString()); } catch (IOException e) { Logger.log(LoggingBehavior.CACHE, Log.WARN, TAG, e.toString()); } } } return imageStream; }
static InputStream interceptAndCacheImageStream(Context context, HttpURLConnection connection) throws IOException { InputStream stream = null; if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { URL url = connection.getURL(); stream = connection.getInputStream(); // Default stream in case caching fails try { if (isCDNURL(url.toURI())) { FileLruCache cache = getCache(context); // Wrap stream with a caching stream stream = cache.interceptAndPut( url.toString(), new BufferedHttpInputStream(stream, connection)); } } catch (IOException e) { // Caching is best effort } catch (URISyntaxException e) { // Caching is best effort } } return stream; }