@Override public Bitmap get(String key) { DiskLruCache.Snapshot snapshot = null; try { snapshot = mDiskCache.get(key); if (snapshot == null) { return null; } InputStream inputStream = snapshot.getInputStream(0); if (inputStream != null) { BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream, IO_BUFFER_SIZE); return BitmapFactory.decodeStream(bufferedInputStream); } } catch (IOException e) { // no op } finally { if (snapshot != null) { snapshot.close(); } } return null; }
return new Snapshot(key, entry.sequenceNumber, ins, entry.lengths);
final byte[] data = readStreamAsByteArray(snapshot.getInputStream(INDEX_DATA)); final int height = readStreamAsInt(snapshot.getInputStream(INDEX_HEIGHT)); final int width = readStreamAsInt(snapshot.getInputStream(INDEX_WIDTH)); if (data != null) { if (BuildConfig.DEBUG)Log.d(TAG, "Cache hit for tile " + key);
Log.i(getTag(), "Snapshot found sending"); if (snapshot != null) return (FileInputStream) snapshot.getInputStream(DISK_CACHE_INDEX); } catch (IOException e) { e.printStackTrace();
DiskLruCache.Snapshot item = diskCache.get(urlToDiskCacheKey(albumUrl)); if (item != null) { Bitmap result = BitmapFactory.decodeStream(item.getInputStream(0)); item.close(); MemoryCacheItem memItem = new MemoryCacheItem(); if (result != null) {
loggerHelper.logEntryForKeyIsOnDisk(key); try { diskResult = snapshotObject.getString(0); } catch (IOException e) { logger.logError(e);
loggerHelper.logEntryForKeyIsOnDisk(key); try { diskResult = snapshotObject.getString(0); } catch (IOException e) { logger.logError(e);
snapshot = mDiskCache.get(key); if (snapshot != null) { in = snapshot.getInputStream(INDEX_VALUE); byte[] bytes = IOUtils.toByteArray(in); value = mConverter.from(bytes);
String storedLength = snapshot.getString(0); if (!TextUtils.isEmpty(storedLength)) { length = Long.parseLong(storedLength); inputStream = snapshot.getInputStream(1); return new CacheEntry(length, new ByteArrayInputStream(IOUtils.toByteArray(inputStream))); } catch (Exception e) {
/** * 从本地缓存加载bitmap * @param url * @param reqWidth * @param reqHeight * @return * @throws IOException */ public Bitmap loadBitmapFromDiskCache(String url,int reqWidth,int reqHeight) throws IOException { if(mDiskLruCache ==null){ return null; } Bitmap bitmap=null; String key = MD5Utils.hashKeyFromUrl(url); DiskLruCache.Snapshot snapshot =mDiskLruCache.get(key); if(snapshot!=null){ FileInputStream fileInputStream =(FileInputStream)snapshot.getInputStream(DISK_CACHE_INDEX); if(reqWidth<=0||reqHeight<=0){ //不压缩图片 bitmap = BitmapFactory.decodeFileDescriptor(fileInputStream.getFD()); }else{ //按需求分辨率压缩图片 bitmap =BitmapUtils.getSmallBitmap(fileInputStream.getFD(),reqWidth,reqHeight); } } return bitmap; }
private Bitmap getDataFromDiskLruCache(String url) { FileDescriptor fileDescriptor = null; FileInputStream fileInputStream = null; DiskLruCache.Snapshot snapShot = null; try { /* 生成图片对应的 MD5 值 */ final String key = DiskCacheUtil.getMD5String(url); snapShot = mDiskLruCache.get(key); if (null != snapShot) { fileInputStream = (FileInputStream) snapShot.getInputStream(0); fileDescriptor = fileInputStream.getFD(); } /* 将缓存数据解析成 Bitmap 对象 */ Bitmap bitmap = null; if (null != fileDescriptor) { bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor); } return bitmap; } catch (IOException e) { e.printStackTrace(); } finally { CloseUtil.closeQuietly(fileInputStream); } return null; }
@Override protected void loadInBackground(LoaderWork.Manager manager, LoaderRequest request) throws IOException { Snapshot snapshot = getSnapshot(request); if (snapshot != null) { try { if (Logger.VERBOSE) Log.v(TAG, "Loaded " + request + " from disk"); Metadata metadata = readMetadata(snapshot); DiskInputSupplier input = new DiskInputSupplier(request, snapshot); manager.deliverStream(input, metadata); long expires = metadata.getExpires(); if (expires != Metadata.NEVER_EXPIRES && System.currentTimeMillis() > expires) { // Cache has expired if (Logger.VERBOSE) Log.v(TAG, request + " has expired, updating"); manager.next(request.withMetadata(metadata), new NextListener(request, manager)); } } finally { snapshot.close(); } } else { // We need to add the next loader manager.next(request, new NextListener(request, manager)); } }
@Override public <T> byte[] getFromCache(Request request) { String cacheKey = urlToKey(request.url().url()); byte[] memoryResponse = (byte[]) memoryCache.get(cacheKey); if(memoryResponse != null){ Log.d("SmartCall", "Memory "); return memoryResponse; } try { DiskLruCache.Snapshot cacheSnapshot = diskCache.get(cacheKey); if(cacheSnapshot != null){ Log.d("SmartCall", "Disk "); return cacheSnapshot.getString(0).getBytes(); }else{ Log.d("SmartCall", "Disk hit!"); return null; } }catch(IOException exc){ return null; } }
@Override public InputStream getInput() throws IOException { if (snapshot == null) { snapshot = cache.get(key); if (snapshot == null) { throw new IOException("Snapshot not available"); } } // Wrap input stream so we can close the snapshot return new FilterInputStream(snapshot.getInputStream(INPUT_IMAGE)) { @Override public void close() throws IOException { super.close(); snapshot.close(); snapshot = null; } }; } }
public Object get(String key) { DiskLruCache.Snapshot snapshot; try { snapshot = mDiskCache.get(key); ObjectInputStream in = new ObjectInputStream(snapshot.getInputStream(0)); return (Object) in.readObject(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } }
@Override public <T> byte[] getFromCache(Request request) { String cacheKey = urlToKey(request.url().url()); byte[] memoryResponse = (byte[]) memoryCache.get(cacheKey); if(memoryResponse != null){ Log.d("SmartCall", "Memory hit!"); return memoryResponse; } try { DiskLruCache.Snapshot cacheSnapshot = diskCache.get(cacheKey); if(cacheSnapshot != null){ Log.d("SmartCall", "Disk hit!"); return cacheSnapshot.getString(0).getBytes(); }else{ return null; } }catch(IOException exc){ return null; } }
@Override public <T> byte[] getFromCache(Request request) { String cacheKey = urlToKey(request.url().url()); byte[] memoryResponse = (byte[]) memoryCache.get(cacheKey); if(memoryResponse != null){ Log.d("SmartCall", "Memory hit!"); return memoryResponse; } try { DiskLruCache.Snapshot cacheSnapshot = diskCache.get(cacheKey); if(cacheSnapshot != null){ Log.d("SmartCall", "Disk hit!"); return cacheSnapshot.getString(0).getBytes(); }else{ return null; } }catch(IOException exc){ return null; } }
@Override public InputStream get(String key) { if (diskLruCache == null || TextUtils.isEmpty(key)) { return null; } try { String cacheKey = cacheKeyProvider.buildCacheKey(key); DiskLruCache.Snapshot snapshot = diskLruCache.get(cacheKey); if (snapshot != null) { return snapshot.getInputStream(0); } } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return null; }
/** * 获取 缓存数据的 InputStream * * @param key cache'key * @return InputStream */ private InputStream getCacheInputStream(String key) { key = SecretUtil.getMD5Result(key); InputStream in; DiskLruCache.Snapshot snapshot = snapshot(key); if (snapshot == null) { return null; } in = snapshot.getInputStream(0); return in; }
@Override public InputStream getInputStream() { try { DiskLruCache.Snapshot snapshot = mDiskCache.get(mKey); if (snapshot != null) { return snapshot.getInputStream(0); } } catch (IOException e) { Log.e(Constants.LOG_TAG, "Could open disk cache for url: " + mKey, e); } return null; } }