Skip to content

Commit 05ccf43

Browse files
committed
unify getHeapFile() and getCacheFile()
1 parent f089e9e commit 05ccf43

File tree

1 file changed

+18
-18
lines changed
  • visualvm/libs.profiler/lib.profiler.heap/src/org/graalvm/visualvm/lib/jfluid/heap

1 file changed

+18
-18
lines changed

visualvm/libs.profiler/lib.profiler.heap/src/org/graalvm/visualvm/lib/jfluid/heap/CacheDirectory.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -85,29 +85,15 @@ boolean isTemporary() {
8585
}
8686

8787
File getCacheFile(String fileName) throws FileNotFoundException {
88-
File f = new File(fileName);
89-
if (isFileRW(f)) {
90-
return f;
91-
}
92-
// try to find file in cache directory
93-
f = new File(cacheDirectory, f.getName());
94-
if (isFileRW(f)) {
95-
return f;
88+
File file = lookupFile(fileName, false);
89+
if (file.canWrite()) {
90+
return file;
9691
}
9792
throw new FileNotFoundException(fileName);
9893
}
9994

10095
File getHeapFile(String fileName) throws FileNotFoundException {
101-
File f = new File(fileName);
102-
if (isFileR(f)) {
103-
return f;
104-
}
105-
// try to find heap dump file next to cache directory
106-
f = new File(cacheDirectory.getParentFile(), f.getName());
107-
if (isFileR(f)) {
108-
return f;
109-
}
110-
throw new FileNotFoundException(fileName);
96+
return lookupFile(fileName, true);
11197
}
11298

11399
void deleteAllCachedFiles() {
@@ -163,6 +149,20 @@ NumberList createNumberList(int idSize) throws IOException {
163149
return new NumberList(idSize, this);
164150
}
165151

152+
private File lookupFile(String fileName, boolean checkParent) throws FileNotFoundException {
153+
File f = new File(fileName);
154+
if (isFileR(f)) {
155+
return f;
156+
}
157+
// try to find file next to cache directory or in cache directory
158+
File dir = checkParent ? cacheDirectory.getParentFile() : cacheDirectory;
159+
f = new File(dir, f.getName());
160+
if (isFileR(f)) {
161+
return f;
162+
}
163+
throw new FileNotFoundException(fileName);
164+
}
165+
166166
private static boolean isFileR(File f) {
167167
return f.exists() && f.isFile() && f.canRead();
168168
}

0 commit comments

Comments
 (0)