public static synchronized void deleteExpired() {

    Date expiryDate;
    long freeSpace = getFreeUserSpace();
    if (freeSpace < Globals.DISK_FREE_THRESHOLD)
      expiryDate = new Date(new Date().getTime() - Globals.FILE_KEEP_IN_CACHE_DURATION_MS_MINIMAL);
    else expiryDate = new Date(new Date().getTime() - Globals.FILE_KEEP_IN_CACHE_DURATION_MS);

    Log.warn(
        "running Delete Expired job",
        "expiryDate=" + expiryDate.toString() + " , freeSpace=" + freeSpace);
    File folder = new File(Config.cachePath);
    for (final File file : folder.listFiles()) {
      if (file.isFile() && file.getName().endsWith(Globals.FILE_EXT_DATA)) {
        BasicFileAttributes attrs;
        try {
          attrs = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
          Date fileLastAccessTime = new Date(attrs.lastAccessTime().toMillis());
          if (fileLastAccessTime.before(expiryDate)) deleteDataAndHeaderFiles(file);
        } catch (Exception e) {
          Log.error("Cannot read file attributes", e.getCause() + " : " + e.getMessage());
          e.printStackTrace();
        }
      }
    }
  }
  private static void deleteFile(File file) {

    try {

      if (file.exists()) {
        file.delete();
        Log.info("File " + file.getName() + " is deleted.");
      }

    } catch (Exception e) {
      Log.info("Error deleting file " + file.getName() + ". Error:" + e.getMessage());
    }
  }
 public static synchronized void deleteIncompleteDownloads() {
   try {
     File directory = new File(Config.downloadPath);
     File[] files = directory.listFiles();
     for (File file : files) {
       try {
         if (!file.delete()) Log.info("Cannnot delete temp file: " + file.getName());
       } catch (Exception e) {
         Log.info("Cannnot delete temp file: " + file.getName() + ". Error:" + e.getMessage());
       }
     }
   } catch (Exception e) {
     Log.error("Error in deleteIncompleteDownloads()", e.getCause() + ": " + e.getMessage());
   }
 }
 public static long getFreeUserSpace() {
   File folder = new File(Config.cachePath);
   try {
     // return (int)((folder.getUsableSpace() * 100.0f) / folder.getTotalSpace());
     return folder.getUsableSpace();
   } catch (Exception e) {
     Log.error("Cannot calculate free space percentage", e.getCause() + ": " + e.getMessage());
     e.printStackTrace();
     return 0;
   }
 }
  public static synchronized void moveDownloadedFileToCache(
      String name, ArrayList<String> headers) {
    int version = getCurrentVersion(name, null);
    version++;

    Path src = Paths.get(Config.downloadPath, name);
    Path dest = Paths.get(Config.cachePath, getDataFileName(name, version));
    Path destHeaders = Paths.get(Config.cachePath, getHeadersFileName(name, version));

    try {
      Files.move(src, dest);
      saveHeaders(destHeaders, headers);
    } catch (IOException e) {
      Log.error(
          "Error moving file: IOException", "From " + src + " to " + dest + ". " + e.getMessage());
      e.printStackTrace();
    }

    if (version != 0) deleteFileDuplicates(name);
  }
  public static synchronized FileInfo getFileInfo(String name) {

    int version = getCurrentVersion(name, null);

    Path dataFilePath = Paths.get(Config.cachePath, getDataFileName(name, version));
    Path headerFilePath = Paths.get(Config.cachePath, getHeadersFileName(name, version));

    Date fileLastModifiedTime;
    BasicFileAttributes attrs;

    try {
      attrs = Files.readAttributes(dataFilePath, BasicFileAttributes.class);
      fileLastModifiedTime = new Date(attrs.lastModifiedTime().toMillis());
    } catch (Exception e) {
      Log.error("Cannot read file attributes", e.getCause() + ": " + e.getMessage());
      e.printStackTrace();
      return null;
    }

    // read headers

    ArrayList<String> headers = loadHeaders(headerFilePath.toFile());

    String url = null;
    String etag = null;

    for (String header : headers) {
      if (header.startsWith(HttpConstants.HEADER_FILE_URL + ":")) {
        url = header.split(":", 2)[1].trim();
      } else if (header.startsWith(HttpConstants.HEADER_ETAG + ":")) {
        etag = header.split(":", 2)[1].trim();
      }
    }

    if (url != null) return new FileInfo(url, fileLastModifiedTime, etag);
    else return null;
  }