Android Open Source - WebImageView Base File Cache






From Project

Back to project page WebImageView.

License

The source code is released under:

MIT License

If you think the Android project WebImageView listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.rsv.cache;
/* www .j  a  va  2  s  .  c  om*/
import java.io.File;

import com.rsv.utils.FileUtils;

abstract class BaseFileCache {

  protected final File cacheDir;

  public BaseFileCache(final File cacheDir) {

    if (!cacheDir.exists()) {
      throw new IllegalArgumentException(cacheDir + " not exists");
    }

    if (!cacheDir.canWrite()) {
      throw new IllegalArgumentException(cacheDir + " not writable");
    }

    this.cacheDir = cacheDir;
  }

  /**
   * @param key
   *            the identify string, will be converted to a file name
   * @param file
   *            the original file
   * 
   * @return the cached file
   */
  public File put(String key, File file) {
    File tFile = getTargetFile(key);

    if (!file.equals(tFile)) {
      // well, this file is not the target cache file
      FileUtils.copyFile(file, tFile);
    }

    return tFile;
  }

  /**
   * delete the file
   * 
   * @param key
   */
  public void remove(String key) {

    File file = this.get(key);

    if (file != null) {
      file.delete();
    }
  }

  /**
   * Given a string key, return the targeted cache file
   * 
   * @param key
   * @return
   */
  public File getTargetFile(String key) {
    return new File(cacheDir, keyToFileName(key));
  }

  public File get(String key) {
    File file = this.getTargetFile(key);

    if (!file.exists())
      return null;

    return file;
  }

  /**
   * Remote all files in this cacheDir
   */
  public void clear() {

    File[] files = cacheDir.listFiles();
    if (files != null) {
      for (File f : files) {
        f.delete();
      }
    }
  }

  protected File getCacheDir() {
    return cacheDir;
  }

  protected String keyToFileName(String key) {
    return "c" + Integer.toHexString(key.hashCode());
  }
}




Java Source Code List

com.rsv.cache.BaseFileCache.java
com.rsv.cache.BaseMemCache.java
com.rsv.cache.LmtSizeMemCache.java
com.rsv.cache.LmtSpaceFileCache.java
com.rsv.comp.IProgressListener.java
com.rsv.comp.ImageLoader.java
com.rsv.config.ConfigReader.java
com.rsv.config.Constants.java
com.rsv.utils.FileUtils.java
com.rsv.utils.HttpClientUtils.java
com.rsv.utils.IOUtils.java
com.rsv.utils.ImgUtils.java
com.rsv.utils.LogUtils.java
com.rsv.utils.OSUtils.java
com.rsv.utils.StorageUtils.java
com.rsv.widget.WebImageView.java