Exemplo n.º 1
0
 /**
  * 添加 数据到sdcard缓存中
  *
  * @param url url地址
  * @param data 数据信息
  */
 public void addToDiskCache(String url, byte[] data) {
   if (mDiskCache == null || url == null || data == null) {
     return;
   }
   // Add to disk cache
   byte[] key = Utils.makeKey(url);
   long cacheKey = Utils.crc64Long(key);
   ByteBuffer buffer = ByteBuffer.allocate(key.length + data.length);
   buffer.put(key);
   buffer.put(data);
   synchronized (mDiskCache) {
     try {
       mDiskCache.insert(cacheKey, buffer.array());
     } catch (IOException ex) {
       // ignore.
     }
   }
 }
Exemplo n.º 2
0
  /**
   * 从sdcard中获取内存缓存
   *
   * @param url 图片url地址
   * @param buffer 填充缓存区
   * @return 是否获得图片
   */
  public boolean getImageData(String url, BytesBuffer buffer) {
    if (mDiskCache == null) return false;

    byte[] key = Utils.makeKey(url);
    long cacheKey = Utils.crc64Long(key);
    try {
      LookupRequest request = new LookupRequest();
      request.key = cacheKey;
      request.buffer = buffer.data;
      synchronized (mDiskCache) {
        if (!mDiskCache.lookup(request)) return false;
      }
      if (Utils.isSameKey(key, request.buffer)) {
        buffer.data = request.buffer;
        buffer.offset = key.length;
        buffer.length = request.length - buffer.offset;
        return true;
      }
    } catch (IOException ex) {
      // ignore.
    }
    return false;
  }