From 5ab1fe04b5cb1054e7913e7648511b30a333f09b Mon Sep 17 00:00:00 2001 From: Konloch Date: Wed, 2 Oct 2024 11:54:23 -0600 Subject: [PATCH] Temp File API Improvements --- .../club/bytecodeviewer/util/TempFile.java | 47 ++++++++++++------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/util/TempFile.java b/src/main/java/the/bytecode/club/bytecodeviewer/util/TempFile.java index f372ec9c..f51cd668 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/util/TempFile.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/util/TempFile.java @@ -11,16 +11,16 @@ import static the.bytecode.club.bytecodeviewer.Constants.TEMP_DIRECTORY; */ public class TempFile { - private final File parent; + private File parent; private final File file; - private final String filePath; + private final String uniqueName; private final HashSet createdFilePaths = new HashSet<>(); - public TempFile(File file) + public TempFile(File file, String uniqueName) { this.parent = file.getParentFile(); this.file = file; - this.filePath = file.getAbsolutePath(); + this.uniqueName = uniqueName; this.createdFilePaths.add(file.getAbsolutePath()); } @@ -34,9 +34,14 @@ public class TempFile return file; } - public String getFilePath() + public String getUniqueName() { - return filePath; + return uniqueName; + } + + public void setParent(File parent) + { + this.parent = parent; } public void delete() @@ -57,12 +62,22 @@ public class TempFile } public File createFileFromExtension(String extension) + { + return createFileFromExtension(true, false, extension); + } + + public File createFileFromExtension(boolean newUniqueName, boolean canExist, String extension) { File file; + String uniqueName = newUniqueName ? MiscUtils.getUniqueName("", extension) : this.uniqueName + extension; + //String uniqueName = this.uniqueName + extension; + //generate a new name until the directory no longer exists - while((file = new File(parent, MiscUtils.getUniqueName("", extension))).exists()) + while((file = new File(parent, uniqueName)).exists()) { + if(canExist) + break; } this.createdFilePaths.add(file.getAbsolutePath()); @@ -75,23 +90,23 @@ public class TempFile //generate a new temporary parent directory File parent = newDirectory ? createTempDirectory() : new File(TEMP_DIRECTORY); - return new TempFile(createTempFile(parent, extension)); - } - - private static File createTempFile(File parent, String extension) - { //make the parent directories parent.mkdirs(); - //return the temporary file - File file; + //create the temporary variables + String uniqueName; + File file = null; //generate a new name until the directory no longer exists - while((file = new File(parent, MiscUtils.getUniqueName("", extension))).exists()) + while((uniqueName = MiscUtils.getUniqueName("", extension)) != null && + (file = new File(parent, uniqueName)).exists()) { } - return file; + if(uniqueName != null) + uniqueName = uniqueName.substring(0, uniqueName.length() - extension.length()); + + return new TempFile(file, uniqueName); } private static File createTempDirectory()