Class FileHelper

java.lang.Object
de.craftsblock.craftsnet.utils.FileHelper

public class FileHelper extends Object
A utility class that helps manage the creation of temporary files in the file system. It determines where to store temporary files, either in the system's default temporary folder or a custom directory based on available space or specific configurations.

This class is useful for handling file operations that require temporary storage during runtime.

Since:
3.1.0-SNAPSHOT
  • Constructor Details

    • FileHelper

      public FileHelper(CraftsNet craftsNet, boolean forceNormalFileSystem)
      Constructs a FileHelper instance that sets up a directory for temporary files. The directory is determined based on available disk space and an optional configuration flag.

      If forceNormalFileSystem is true, temporary files will be stored in a "temp" folder in the current working directory. Otherwise, the system's default temporary file location will be used, unless there is less than 250 MB of available space, in which case the "temp" folder will be used instead.

      Parameters:
      craftsNet - the main application instance providing the logger for warnings.
      forceNormalFileSystem - if true, forces the use of the "temp" folder in the current directory.
  • Method Details

    • createTempFile

      public Path createTempFile(String prefix, String suffix, FileAttribute<?>... attrs) throws IOException
      Creates a temporary file with the given prefix and suffix in the appropriate directory. If a custom directory was selected during initialization, the file will be created there. Otherwise, the system's default temporary file directory is used.

      If no POSIX file permissions are explicitly provided, and it is supported by the file system, the file will be created with default POSIX permissions: read and write access for the owner.

      Parameters:
      prefix - the prefix string to be used in generating the file's name.
      suffix - the suffix string to be used in generating the file's name.
      attrs - an optional list of file attributes to set atomically when creating the file.
      Returns:
      the path to the created temporary file.
      Throws:
      IOException - if an I/O error occurs while creating the file.
    • getJarFileAt

      public JarFile getJarFileAt(Path path) throws IOException
      Creates and returns a JarFile instance from the provided directory or file path.

      If the provided path is a regular file, a JarFile is created directly from the path. If the path is a directory, a temporary jar file is created by adding all files in the directory to the jar. The temporary jar file is deleted when the JVM exits.

      Parameters:
      path - The path to the directory or file.
      Returns:
      A JarFile instance representing the contents of the specified file or directory.
      Throws:
      IOException - If an I/O error occurs while reading the file or creating the jar file.
      Since:
      3.2.0-SNAPSHOT