public String getstartCursor(String key) { String startCursor = null; try { DiskLruCache.Snapshot snapShot = diskCache.get(key); if (snapShot != null) { InputStream in = snapShot.getInputStream(0); ByteArrayOutputStream bos = new ByteArrayOutputStream(); // 读取缓存 byte[] buffer = new byte[2048]; int length = 0; while ((length = in.read(buffer)) != -1) { bos.write(buffer, 0, length); // 写入输出流 } in.close(); // 读取完毕,关闭输入流 startCursor = bos.toString(); // System.out.println("getstartCursor "+key+"|"+startCursor); } } catch (IOException e) { e.printStackTrace(); } return startCursor; }
public Bitmap getBitmap(String key) { if (key == null) { return null; } Bitmap bitmap = getBitmapFromMemCache(key); if (bitmap != null) { return bitmap; } DiskLruCache.Snapshot snapshot = null; try { snapshot = _diskCache.get(key); if (snapshot == null) { return null; } final InputStream in = snapshot.getInputStream(0); if (in != null) { final BufferedInputStream buffIn = new BufferedInputStream(in, Utilities.IO_BUFFER_SIZE); bitmap = BitmapFactory.decodeStream(buffIn); } } catch (IOException e) { e.printStackTrace(); } finally { if (snapshot != null) { snapshot.close(); } } return bitmap; }
public ArrayList<FavURLShow> getFavurlshowlist(String key) { ArrayList<FavURLShow> list = null; try { DiskLruCache.Snapshot snapShot = diskCache.get(key); if (snapShot != null) { ObjectInputStream in = new ObjectInputStream(snapShot.getInputStream(0)); list = (ArrayList<FavURLShow>) in.readObject(); } } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; }
@Override public Bitmap getBitmap(String url) { Bitmap bitmap = null; if (mMemoryCache.get(url) != null) { bitmap = mMemoryCache.get(url); } else { String key = CommonUtils.encryptionWithMD5(url); try { if (mDiskLruCache.get(key) != null) { CommonUtils.log("hehe"); DiskLruCache.Snapshot snapShot = mDiskLruCache.get(key); InputStream is = snapShot.getInputStream(0); bitmap = BitmapFactory.decodeStream(is); } } catch (IOException e) { e.printStackTrace(); } } return bitmap; }
public boolean containsKey(String key) { if (key == null) { return false; } boolean contained = false; DiskLruCache.Snapshot snapshot = null; try { snapshot = _diskCache.get(key); contained = snapshot != null; } catch (IOException e) { e.printStackTrace(); } finally { if (snapshot != null) { snapshot.close(); } } return contained; }
public CacheItem getFromDiskCache(final String url, boolean checkExpiration) { CacheItem result = null; if (null != mDiskCache) { checkNotOnMainThread(); try { final String key = transformUrlForDiskCacheKey(url); DiskLruCache.Snapshot snapshot = mDiskCache.get(key); if (null != snapshot) { Object value = readValueFromDisk(snapshot.getInputStream(0)); DateTime expiresAt = new DateTime(readExpirationFromDisk(snapshot.getInputStream(1))); if (value != null) { if (checkExpiration && expiresAt.isBeforeNow()) { mDiskCache.remove(key); scheduleDiskCacheFlush(); } else { result = new CacheItem(value, expiresAt); if (null != mMemoryCache) { mMemoryCache.put(url, result); } } } else { // If we get here, the file in the cache can't be // decoded. Remove it and schedule a flush. mDiskCache.remove(key); scheduleDiskCacheFlush(); } } } catch (IOException e) { Timber.e(e, "getFromDiskCache failed."); } } return result; }