Rectification attempt at cache migration issues with nio File

This commit is contained in:
Amos Ng 2020-06-12 20:54:40 +08:00
parent 32a2fecfd0
commit 4b7c8bb0cb
No known key found for this signature in database
GPG key ID: 89086414F634D123

View file

@ -34,6 +34,10 @@ import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.Writer; import java.io.Writer;
import java.nio.charset.StandardCharsets; 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.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
@ -994,15 +998,35 @@ public final class DiskLruCache implements Closeable {
public File getCleanFile(int i) { public File getCleanFile(int i) {
// Move files to new caching tree if exists // Move files to new caching tree if exists
File old_cache = new File(directory, key + "." + i); Path old_cache = Paths.get(directory + File.separator + key + "." + i);
File new_cache = new File(directory + subkeypath, key + "." + i); Path new_cache = Paths.get(directory + subkeypath + File.separator + key + "." + i);
if (old_cache.exists()) { if (Files.exists(old_cache)) {
old_cache.renameTo(new_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) { 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"); return new File(directory + subkeypath, key + "." + i + ".tmp");
} }
} }