public void put(String key, Bitmap image) {
    if (key == null || image == null) {
      return;
    }

    addBitmapToMemoryCache(key, image);

    DiskLruCache.Editor editor = null;
    try {
      editor = _diskCache.edit(key);
      if (editor == null || image == null) {
        return;
      }

      if (writeBitmapToFile(image, editor)) {
        _diskCache.flush();
        editor.commit();
      } else {
        editor.abort();
      }
    } catch (IOException e) {
      try {
        if (editor != null) {
          editor.abort();
        }
      } catch (IOException ignored) {
      }
    }
  }
  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 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;
  }
예제 #4
0
  public CacheItem put(final String url, final Object obj, DateTime expiresAt) {

    if (obj == null) {
      return null;
    }

    Timber.d(String.format("put(%s)", url));
    CacheItem d = new CacheItem(obj, expiresAt);

    if (null != mMemoryCache) {
      mMemoryCache.put(url, d);
    }

    if (null != mDiskCache) {
      checkNotOnMainThread();

      final String key = transformUrlForDiskCacheKey(url);
      final ReentrantLock lock = getLockForDiskCacheEdit(key);
      lock.lock();
      try {
        DiskLruCache.Editor editor = mDiskCache.edit(key);
        writeValueToDisk(editor.newOutputStream(0), obj);
        writeExpirationToDisk(editor.newOutputStream(1), expiresAt);
        editor.commit();
      } catch (IOException e) {
        e.printStackTrace();
      } finally {
        lock.unlock();
        scheduleDiskCacheFlush();
      }
    }

    return d;
  }
 public void clearCache() {
   try {
     _diskCache.delete();
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
예제 #6
0
    public void run() {
      // Make sure we're running with a background priority
      Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);

      try {
        mDiskCache.flush();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
 @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;
 }
 private Snapshot getDiskLruCacheSnapshot(String key) {
   if (mDiskLruCache != null) {
     key = generateDiskLruCacheKey(key);
     try {
       return mDiskLruCache.get(key);
     } catch (IOException e) {
       e.printStackTrace();
     }
   }
   return null;
 }
 private DiskLruCache.Editor getDiskEditor(String key) {
   if (mDiskLruCache != null) {
     try {
       key = generateDiskLruCacheKey(key);
       return mDiskLruCache.edit(key);
     } catch (IOException e) {
       e.printStackTrace();
     }
   }
   return null;
 }
예제 #10
0
 @Override
 public boolean clear() {
   if (mDiskLruCache != null) {
     try {
       mDiskLruCache.delete();
       return true;
     } catch (IOException e) {
       e.printStackTrace();
     }
   }
   return false;
 }
예제 #11
0
  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;
  }
예제 #12
0
  public boolean containsInDiskCache(String url) {
    if (null != mDiskCache) {
      checkNotOnMainThread();

      try {
        return null != mDiskCache.get(transformUrlForDiskCacheKey(url));
      } catch (IOException e) {
        e.printStackTrace();
      }
    }

    return false;
  }
  public ImageCache(Context context) {
    try {
      _memoryCache =
          new LruCache<String, Bitmap>(MEMORY_CACHE_SIZE) {
            protected int sizeOf(String key, Bitmap value) {
              return value.getRowBytes() * value.getHeight();
            }
          };

      final File diskCacheDir = getDiskCacheDir(context, DISK_CACHE_SUBDIR);
      _diskCache = DiskLruCache.open(diskCacheDir, APP_VERSION, VALUE_COUNT, DISK_CACHE_SIZE);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  public FavurlShowDiskCache(Context context) {

    File cacheDir = CommonUtils.getDiskCacheDir(context, Constants.FAVURL_CACHE_DIR);
    if (!cacheDir.exists()) {
      cacheDir.mkdirs();
    }

    try {
      diskCache =
          DiskLruCache.open(
              cacheDir, CommonUtils.getAppVersion(context), 1, Constants.BITMAP_CACHE_MAX_SIZE);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
예제 #15
0
  public void remove(String url) {
    if (null != mMemoryCache) {
      mMemoryCache.remove(url);
    }

    if (null != mDiskCache) {
      checkNotOnMainThread();

      try {
        mDiskCache.remove(transformUrlForDiskCacheKey(url));
        scheduleDiskCacheFlush();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
  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;
  }
  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 CustomImageCache() {
   int maxSize = (int) Runtime.getRuntime().maxMemory() / 8;
   mMemoryCache =
       new LruCache<String, Bitmap>(maxSize) {
         @Override
         protected int sizeOf(String key, Bitmap value) {
           // 这个方法一定要重写,不然缓存没有效果
           return value.getHeight() * value.getRowBytes();
         }
       };
   try {
     // 获取图片缓存路径
     File cacheDir = FileUtils.getDiskCacheDir("thumb");
     if (!cacheDir.exists()) {
       cacheDir.mkdirs();
     }
     // 创建DiskLruCache实例,初始化缓存数据
     mDiskLruCache =
         DiskLruCache.open(cacheDir, CommonUtils.getAppVersion(), 1, DISK_CACHE_MAX_SIZE);
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
예제 #19
0
 do {
     if (var3_4 < this.valueCount) {
         var1_1 = var8_3.getDirtyFile(var3_4);
         if (var2_2) {
             if (var1_1.exists()) {
                 var9_7 = var8_3.getCleanFile(var3_4);
                 var1_1.renameTo(var9_7);
                 var4_5 = Entry.access$1000(var8_3)[var3_4];
                 Entry.access$1000((Entry)var8_3)[var3_4] = var6_6 = var9_7.length();
                 this.size = this.size - var4_5 + var6_6;
             }
         } else {
             DiskLruCache.deleteIfExists((File)var1_1);
         }
     } else {
         ++this.redundantOpCount;
         Entry.access$702(var8_3, null);
         if (Entry.access$600(var8_3) | var2_2) {
             Entry.access$602(var8_3, true);
             this.journalWriter.write("CLEAN " + Entry.access$1100(var8_3) + var8_3.getLengths() + '\n');
             if (var2_2) {
                 var4_5 = this.nextSequenceNumber;
                 this.nextSequenceNumber = 1 + var4_5;
                 Entry.access$1202(var8_3, var4_5);
             }
         } else {
             this.lruEntries.remove(Entry.access$1100(var8_3));
             this.journalWriter.write("REMOVE " + Entry.access$1100(var8_3) + '\n');
         }
         this.journalWriter.flush();
         if (this.size <= this.maxSize && !this.journalRebuildRequired()) ** continue;
         this.executorService.submit(this.cleanupCallable);
         return;
     }
     ++var3_4;
 } while (true);
예제 #20
0
  private void initDiskCache(String path, boolean autoCreate, long maxSize) {
    if (mDiskLruCache == null && !TextUtils.isEmpty(path)) {
      File f = new File(path);
      if (!f.exists() || !f.isDirectory()) {
        boolean b = false;
        if (autoCreate && f.mkdirs()) {
          b = true;
        }
        if (!b) {
          return;
        }
      }

      if (maxSize <= 0) {
        return;
      }
      try {
        mDiskLruCache = DiskLruCache.open(f, 1, 1, maxSize);
      } catch (IOException e) {
        e.printStackTrace();
        return;
      }
    }
  }
 public File getCacheFolder() {
   return _diskCache.getDirectory();
 }
예제 #22
0
 public String getString(int n2) throws IOException {
     return DiskLruCache.inputStreamToString(this.getInputStream(n2));
 }