Skip to content

Commit

Permalink
migrate fields an params from java.io.File to java.nio.Path #77
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteph-de committed Apr 12, 2024
1 parent abe43d4 commit 1da5de2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -139,10 +140,10 @@ public final class DiskLruCache implements Closeable {
* it exists when the cache is opened.
*/

private final File directory;
private final File journalFile;
private final File journalFileTmp;
private final File journalFileBackup;
private final Path directory;
private final Path journalFile;
private final Path journalFileTmp;
private final Path journalFileBackup;
private final int appVersion;
private long maxSize;
private final int valueCount;
Expand Down Expand Up @@ -178,12 +179,12 @@ public Void call() throws Exception {
}
};

private DiskLruCache(File directory, int appVersion, int valueCount, long maxSize) {
private DiskLruCache(Path directory, int appVersion, int valueCount, long maxSize) {
this.directory = directory;
this.appVersion = appVersion;
this.journalFile = new File(directory, JOURNAL_FILE);
this.journalFileTmp = new File(directory, JOURNAL_FILE_TEMP);
this.journalFileBackup = new File(directory, JOURNAL_FILE_BACKUP);
this.journalFile = directory.resolve(JOURNAL_FILE);
this.journalFileTmp = directory.resolve(JOURNAL_FILE_TEMP);
this.journalFileBackup = directory.resolve(JOURNAL_FILE_BACKUP);
this.valueCount = valueCount;
this.maxSize = maxSize;
}
Expand All @@ -197,7 +198,7 @@ private DiskLruCache(File directory, int appVersion, int valueCount, long maxSiz
* @param maxSize the maximum number of bytes this cache should use to store
* @throws IOException if reading or writing the cache directory fails
*/
public static DiskLruCache open(File directory, int appVersion, int valueCount, long maxSize)
public static DiskLruCache open(Path directory, int appVersion, int valueCount, long maxSize)
throws IOException {
if (maxSize <= 0) {
throw new IllegalArgumentException("maxSize <= 0");
Expand All @@ -207,20 +208,20 @@ public static DiskLruCache open(File directory, int appVersion, int valueCount,
}

// If a bkp file exists, use it instead.
File backupFile = new File(directory, JOURNAL_FILE_BACKUP);
if (backupFile.exists()) {
File journalFile = new File(directory, JOURNAL_FILE);
Path backupFile = directory.resolve(JOURNAL_FILE_BACKUP);
if (backupFile.toFile().exists()) {
Path journalFile = directory.resolve(JOURNAL_FILE);
// If journal file also exists just delete backup file.
if (journalFile.exists()) {
backupFile.delete();
if (journalFile.toFile().exists()) {
backupFile.toFile().delete();
} else {
renameTo(backupFile, journalFile, false);
renameTo(backupFile.toFile(), journalFile.toFile(), false);
}
}

// Prefer to pick up where we left off.
DiskLruCache cache = new DiskLruCache(directory, appVersion, valueCount, maxSize);
if (cache.journalFile.exists()) {
if (cache.journalFile.toFile().exists()) {
try {
cache.readJournal();
cache.processJournal();
Expand All @@ -237,14 +238,14 @@ public static DiskLruCache open(File directory, int appVersion, int valueCount,
}

// Create a new empty cache.
directory.mkdirs();
directory.toFile().mkdirs();
cache = new DiskLruCache(directory, appVersion, valueCount, maxSize);
cache.rebuildJournal();
return cache;
}

private void readJournal() throws IOException {
StrictLineReader reader = new StrictLineReader(new FileInputStream(journalFile), DiskLruUtil.US_ASCII);
StrictLineReader reader = new StrictLineReader(new FileInputStream(journalFile.toFile()), DiskLruUtil.US_ASCII);
try {
String magic = reader.readLine();
String version = reader.readLine();
Expand Down Expand Up @@ -276,7 +277,7 @@ private void readJournal() throws IOException {
rebuildJournal();
} else {
journalWriter = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(journalFile, true), DiskLruUtil.US_ASCII));
new FileOutputStream(journalFile.toFile(), true), DiskLruUtil.US_ASCII));
}
} finally {
DiskLruUtil.closeQuietly(reader);
Expand Down Expand Up @@ -327,7 +328,7 @@ private void readJournalLine(String line) throws IOException {
* cache. Dirty entries are assumed to be inconsistent and will be deleted.
*/
private void processJournal() throws IOException {
deleteIfExists(journalFileTmp);
deleteIfExists(journalFileTmp.toFile());
for (Iterator<Entry> i = lruEntries.values().iterator(); i.hasNext();) {
Entry entry = i.next();
if (entry.currentEditor == null) {
Expand All @@ -337,8 +338,8 @@ private void processJournal() throws IOException {
} else {
entry.currentEditor = null;
for (int t = 0; t < valueCount; t++) {
deleteIfExists(entry.getCleanFile(t));
deleteIfExists(entry.getDirtyFile(t));
deleteIfExists(entry.getCleanFile(t).toFile());
deleteIfExists(entry.getDirtyFile(t).toFile());
}
i.remove();
}
Expand All @@ -355,7 +356,7 @@ private synchronized void rebuildJournal() throws IOException {
}

Writer writer = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(journalFileTmp), DiskLruUtil.US_ASCII));
new OutputStreamWriter(new FileOutputStream(journalFileTmp.toFile()), DiskLruUtil.US_ASCII));
try {
writer.write(MAGIC);
writer.write("\n");
Expand All @@ -378,14 +379,14 @@ private synchronized void rebuildJournal() throws IOException {
closeWriter(writer);
}

if (journalFile.exists()) {
renameTo(journalFile, journalFileBackup, true);
if (journalFile.toFile().exists()) {
renameTo(journalFile.toFile(), journalFileBackup.toFile(), true);
}
renameTo(journalFileTmp, journalFile, false);
journalFileBackup.delete();
renameTo(journalFileTmp.toFile(), journalFile.toFile(), false);
journalFileBackup.toFile().delete();

journalWriter = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(journalFile, true), DiskLruUtil.US_ASCII));
new OutputStreamWriter(new FileOutputStream(journalFile.toFile(), true), DiskLruUtil.US_ASCII));
}

private static void deleteIfExists(File file) throws IOException {
Expand Down Expand Up @@ -419,9 +420,9 @@ public synchronized Value get(String key) throws IOException {
return null;
}

for (File file : entry.cleanFiles) {
for (Path file : entry.cleanFiles) {
// A file must have been deleted manually!
if (!file.exists()) {
if (!file.toFile().exists()) {
return null;
}
}
Expand Down Expand Up @@ -473,7 +474,7 @@ private synchronized Editor edit(String key, long expectedSequenceNumber) throws
}

/** Returns the directory where this cache stores its data. */
public File getDirectory() {
public Path getDirectory() {
return directory;
}

Expand Down Expand Up @@ -516,18 +517,18 @@ private synchronized void completeEdit(Editor editor, boolean success) throws IO
editor.abort();
throw new IllegalStateException("Newly created entry didn't create value for index " + i);
}
if (!entry.getDirtyFile(i).exists()) {
if (!entry.getDirtyFile(i).toFile().exists()) {
editor.abort();
return;
}
}
}

for (int i = 0; i < valueCount; i++) {
File dirty = entry.getDirtyFile(i);
File dirty = entry.getDirtyFile(i).toFile();
if (success) {
if (dirty.exists()) {
File clean = entry.getCleanFile(i);
File clean = entry.getCleanFile(i).toFile();
dirty.renameTo(clean);
long oldLength = entry.lengths[i];
long newLength = clean.length();
Expand Down Expand Up @@ -590,7 +591,7 @@ public synchronized boolean remove(String key) throws IOException {
}

for (int i = 0; i < valueCount; i++) {
File file = entry.getCleanFile(i);
File file = entry.getCleanFile(i).toFile();
if (file.exists() && !file.delete()) {
throw new IOException("failed to delete " + file);
}
Expand Down Expand Up @@ -690,9 +691,9 @@ public final class Value {
private final String key;
private final long sequenceNumber;
private final long[] lengths;
private final File[] files;
private final Path[] files;

private Value(String key, long sequenceNumber, File[] files, long[] lengths) {
private Value(String key, long sequenceNumber, Path[] files, long[] lengths) {
this.key = key;
this.sequenceNumber = sequenceNumber;
this.files = files;
Expand All @@ -708,13 +709,13 @@ public Editor edit() throws IOException {
return DiskLruCache.this.edit(key, sequenceNumber);
}

public File getFile(int index) {
public Path getFile(int index) {
return files[index];
}

/** Returns the string value for {@code index}. */
public String getString(int index) throws IOException {
InputStream is = new FileInputStream(files[index]);
InputStream is = new FileInputStream(files[index].toFile());
return inputStreamToString(is);
}

Expand Down Expand Up @@ -748,7 +749,7 @@ private InputStream newInputStream(int index) throws IOException {
return null;
}
try {
return new FileInputStream(entry.getCleanFile(index));
return new FileInputStream(entry.getCleanFile(index).toFile());
} catch (FileNotFoundException e) {
return null;
}
Expand All @@ -764,16 +765,16 @@ public String getString(int index) throws IOException {
return in != null ? inputStreamToString(in) : null;
}

public File getFile(int index) throws IOException {
public Path getFile(int index) throws IOException {
synchronized (DiskLruCache.this) {
if (entry.currentEditor != this) {
throw new IllegalStateException();
}
if (!entry.readable) {
written[index] = true;
}
File dirtyFile = entry.getDirtyFile(index);
directory.mkdirs();
Path dirtyFile = entry.getDirtyFile(index);
directory.toFile().mkdirs();
return dirtyFile;
}
}
Expand All @@ -782,7 +783,7 @@ public File getFile(int index) throws IOException {
public void set(int index, String value) throws IOException {
Writer writer = null;
try {
OutputStream os = new FileOutputStream(getFile(index));
OutputStream os = new FileOutputStream(getFile(index).toFile());
writer = new OutputStreamWriter(os, DiskLruUtil.UTF_8);
writer.write(value);
} finally {
Expand Down Expand Up @@ -828,8 +829,8 @@ private final class Entry {
private final long[] lengths;

/** Memoized File objects for this entry to avoid char[] allocations. */
File[] cleanFiles;
File[] dirtyFiles;
Path[] cleanFiles;
Path[] dirtyFiles;

/** True if this entry has ever been published. */
private boolean readable;
Expand All @@ -843,17 +844,17 @@ private final class Entry {
private Entry(String key) {
this.key = key;
this.lengths = new long[valueCount];
cleanFiles = new File[valueCount];
dirtyFiles = new File[valueCount];
cleanFiles = new Path[valueCount];
dirtyFiles = new Path[valueCount];

// The names are repetitive so re-use the same builder to avoid allocations.
StringBuilder fileBuilder = new StringBuilder(key).append('.');
int truncateTo = fileBuilder.length();
for (int i = 0; i < valueCount; i++) {
fileBuilder.append(i);
cleanFiles[i] = new File(directory, fileBuilder.toString());
cleanFiles[i] = directory.resolve(fileBuilder.toString());
fileBuilder.append(".tmp");
dirtyFiles[i] = new File(directory, fileBuilder.toString());
dirtyFiles[i] = directory.resolve(fileBuilder.toString());
fileBuilder.setLength(truncateTo);
}
}
Expand Down Expand Up @@ -885,11 +886,11 @@ private IOException invalidLengths(String[] strings) throws IOException {
throw new IOException("unexpected journal line: " + java.util.Arrays.toString(strings));
}

public File getCleanFile(int i) {
public Path getCleanFile(int i) {
return cleanFiles[i];
}

public File getDirtyFile(int i) {
public Path getDirtyFile(int i) {
return dirtyFiles[i];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.Reader;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.nio.file.Path;

final class DiskLruUtil {
static final Charset US_ASCII = Charset.forName("US-ASCII");
Expand All @@ -48,14 +49,14 @@ static String readFully(Reader reader) throws IOException {
* Deletes the contents of {@code dir}. Throws an IOException if any file
* could not be deleted, or if {@code dir} is not a readable directory.
*/
static void deleteContents(File dir) throws IOException {
File[] files = dir.listFiles();
static void deleteContents(Path dir) throws IOException {
File[] files = dir.toFile().listFiles();
if (files == null) {
throw new IOException("not a readable directory: " + dir);
}
for (File file : files) {
if (file.isDirectory()) {
deleteContents(file);
deleteContents(file.toPath());
}
if (!file.delete()) {
throw new IOException("failed to delete file: " + file);
Expand Down

0 comments on commit 1da5de2

Please sign in to comment.