Ejemplo n.º 1
0
 @Override
 public void clear() {
   try {
     cache.delete();
   } catch (IOException e) {
     L.e(e);
   } finally {
     initCache(cache.getDirectory(), reserveCacheDir, cache.getMaxSize(), cache.getMaxFileCount());
   }
 }
Ejemplo n.º 2
0
 @Override
 public boolean remove(String imageUri) {
   try {
     return cache.remove(getKey(imageUri));
   } catch (IOException e) {
     L.e(e);
     return false;
   }
 }
Ejemplo n.º 3
0
 @Override
 public void close() {
   try {
     cache.close();
   } catch (IOException e) {
     L.e(e);
   }
   cache = null;
 }
Ejemplo n.º 4
0
 @Override
 public File get(String imageUri) {
   try {
     DiskLruCache.Snapshot snapshot = cache.get(getKey(imageUri));
     return snapshot == null ? null : snapshot.getFile(0);
   } catch (IOException e) {
     L.e(e);
     return null;
   }
 }
Ejemplo n.º 5
0
 private void initCache(
     File cacheDir, File reserveCacheDir, long cacheMaxSize, int cacheMaxFileCount) {
   try {
     cache = DiskLruCache.open(cacheDir, 1, 1, cacheMaxSize, cacheMaxFileCount);
   } catch (IOException e) {
     L.e(e);
     if (reserveCacheDir != null) {
       initCache(reserveCacheDir, null, cacheMaxSize, cacheMaxFileCount);
     }
   }
 }
Ejemplo n.º 6
0
  /**
   * Opens the cache in {@code directory}, creating a cache if none exists there.
   *
   * @param directory a writable directory
   * @param valueCount the number of values per cache entry. Must be positive.
   * @param maxSize the maximum number of bytes this cache should use to store
   * @param maxFileCount the maximum file count this cache should store
   * @throws java.io.IOException if reading or writing the cache directory fails
   */
  public static DiskLruCache open(
      File directory, int appVersion, int valueCount, long maxSize, int maxFileCount)
      throws IOException {
    if (maxSize <= 0) {
      throw new IllegalArgumentException("maxSize <= 0");
    }
    if (maxFileCount <= 0) {
      throw new IllegalArgumentException("maxFileCount <= 0");
    }
    if (valueCount <= 0) {
      throw new IllegalArgumentException("valueCount <= 0");
    }

    // If a bkp file exists, use it instead.
    File backupFile = new File(directory, JOURNAL_FILE_BACKUP);
    if (backupFile.exists()) {
      File journalFile = new File(directory, JOURNAL_FILE);
      // If journal file also exists just delete backup file.
      if (journalFile.exists()) {
        backupFile.delete();
      } else {
        renameTo(backupFile, journalFile, false);
      }
    }

    // Prefer to pick up where we left off.
    DiskLruCache cache = new DiskLruCache(directory, appVersion, valueCount, maxSize, maxFileCount);
    if (cache.journalFile.exists()) {
      try {
        cache.readJournal();
        cache.processJournal();
        cache.journalWriter =
            new BufferedWriter(
                new OutputStreamWriter(
                    new FileOutputStream(cache.journalFile, true), Util.US_ASCII));
        return cache;
      } catch (IOException journalIsCorrupt) {
        System.out.println(
            "DiskLruCache "
                + directory
                + " is corrupt: "
                + journalIsCorrupt.getMessage()
                + ", removing");
        cache.delete();
      }
    }

    // Create a new empty cache.
    directory.mkdirs();
    cache = new DiskLruCache(directory, appVersion, valueCount, maxSize, maxFileCount);
    cache.rebuildJournal();
    return cache;
  }
Ejemplo n.º 7
0
  @Override
  public boolean save(String imageUri, Bitmap bitmap) throws IOException {
    DiskLruCache.Editor editor = cache.edit(getKey(imageUri));
    if (editor == null) {
      return false;
    }

    OutputStream os = new BufferedOutputStream(editor.newOutputStream(0), bufferSize);
    boolean savedSuccessfully = false;
    try {
      savedSuccessfully = bitmap.compress(compressFormat, compressQuality, os);
    } finally {
      IoUtils.closeSilently(os);
    }
    if (savedSuccessfully) {
      editor.commit();
    } else {
      editor.abort();
    }
    return savedSuccessfully;
  }
Ejemplo n.º 8
0
  @Override
  public boolean save(String imageUri, InputStream imageStream, IoUtils.CopyListener listener)
      throws IOException {
    DiskLruCache.Editor editor = cache.edit(getKey(imageUri));
    if (editor == null) {
      return false;
    }

    OutputStream os = new BufferedOutputStream(editor.newOutputStream(0), bufferSize);
    boolean copied = false;
    try {
      copied = IoUtils.copyStream(imageStream, os, listener, bufferSize);
    } finally {
      IoUtils.closeSilently(os);
      if (copied) {
        editor.commit();
      } else {
        editor.abort();
      }
    }
    return copied;
  }
Ejemplo n.º 9
0
 @Override
 public File getDirectory() {
   return cache.getDirectory();
 }