From 4b7c8bb0cba29997623a0e32fb65c2e2770f476e Mon Sep 17 00:00:00 2001 From: Amos Ng Date: Fri, 12 Jun 2020 20:54:40 +0800 Subject: [PATCH] Rectification attempt at cache migration issues with nio File --- src/main/java/mdnet/cache/DiskLruCache.java | 34 ++++++++++++++++++--- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/src/main/java/mdnet/cache/DiskLruCache.java b/src/main/java/mdnet/cache/DiskLruCache.java index 2b86b0c..5154493 100644 --- a/src/main/java/mdnet/cache/DiskLruCache.java +++ b/src/main/java/mdnet/cache/DiskLruCache.java @@ -34,6 +34,10 @@ import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Writer; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.FileAlreadyExistsException; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedHashMap; @@ -994,15 +998,35 @@ public final class DiskLruCache implements Closeable { public File getCleanFile(int i) { // Move files to new caching tree if exists - File old_cache = new File(directory, key + "." + i); - File new_cache = new File(directory + subkeypath, key + "." + i); - if (old_cache.exists()) { - old_cache.renameTo(new_cache); + Path old_cache = Paths.get(directory + File.separator + key + "." + i); + Path new_cache = Paths.get(directory + subkeypath + File.separator + key + "." + i); + if (Files.exists(old_cache)) { + try { + Files.move(old_cache, new_cache); + } catch (FileAlreadyExistsException faee) { + try { + Files.delete(old_cache); + } catch (IOException ex) {} + } catch (IOException ex) {} } - return new_cache; + + return new File(directory + subkeypath, key + "." + i); } public File getDirtyFile(int i) { + // Move files to new caching tree if exists + Path old_cache = Paths.get(directory + File.separator + key + "." + i + ".tmp"); + Path new_cache = Paths.get(directory + subkeypath + File.separator + key + "." + i + ".tmp"); + if (Files.exists(old_cache)) { + try { + Files.move(old_cache, new_cache); + } catch (FileAlreadyExistsException faee) { + try { + Files.delete(old_cache); + } catch (IOException ex) {} + } catch (IOException ex) {} + } + return new File(directory + subkeypath, key + "." + i + ".tmp"); } }