package cn.net.caoyao.cpzshandset.common.bitmap;

import java.io.File;

public class FileCache
{
  private String cachePath;

  public FileCache(String cachePath)
  {
    this.cachePath = cachePath;
    createCacheDir();
  }

  public String getSavePath(String url) {
    String filename = String.valueOf(url.hashCode());
    return this.cachePath + "/" + filename;
  }

  public File getFile(String url) {
    File f = new File(getSavePath(url));
    return f;
  }

  public void clear()
  {
    deleteCache();
  }

  private void deleteCache()
  {
    File cacheFile = new File(this.cachePath);
    if (cacheFile.isDirectory()) {
      for (File file : cacheFile.listFiles()) {
        file.delete();
      }
      cacheFile.delete();
    }
  }

  private void createCacheDir()
  {
    File file = new File(this.cachePath);
    if (!file.exists())
      file.mkdirs();
  }
}