Skip to content

Commit

Permalink
more sophisticated temporary file api
Browse files Browse the repository at this point in the history
  • Loading branch information
DasBabyPixel committed Jul 9, 2024
1 parent f00c6c8 commit 7d30bc8
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions common/src/main/java/eu/cloudnetservice/common/io/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,14 +241,32 @@ public static void delete(@Nullable Path path) {
}

/**
* Resolves a random path in the temp directory of the cloud and creates all needed parents directories for a
* temporary file including the {@link FileUtil#TEMP_DIR}.
* Resolves a random path in the temp directory of the cloud and creates all needed parent directories for a temporary
* file including the {@link FileUtil#TEMP_DIR}. This method is equivalent to
* {@code FileUtil.createTempFile(UUID.randomUUID().toString())}
*
* @return the path to the temporary file.
* @see #createTempFile(String)
*/
public static @NonNull Path createTempFile() {
return createTempFile(UUID.randomUUID().toString());
}

/**
* Resolves a path in the temp directory of the cloud and creates all needed parent directories for a temporary file
* including the {@link FileUtil#TEMP_DIR}. The final name might not be the argument if the file already exists.
*
* @param name the preferred name of the temporary file.
* @return the path to the temporary file.
*/
public static @NonNull Path createTempFile(String name) {
createDirectory(TEMP_DIR);
return TEMP_DIR.resolve(UUID.randomUUID().toString());
int id = 0;
Path path;
do {
path = TEMP_DIR.resolve(id++ == 0 ? name : name + "-" + id);
} while (Files.exists(path));
return path;
}

/**
Expand Down

0 comments on commit 7d30bc8

Please sign in to comment.