java.lang.Object
org.sentrysoftware.metricshub.engine.common.helpers.FileHelper

public class FileHelper extends Object
Utility class for common file-related operations.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    fileSystemTask(@NonNull URI uri, @NonNull Map<String,?> env, @NonNull Runnable runnable)
    Executes a file system task using the provided URI, environment map, and a runnable task within a try-with-resources block.
    static <T> T
    fileSystemTask(@NonNull URI uri, @NonNull Map<String,?> env, @NonNull Callable<T> callable)
    Executes a file system task using the provided URI, environment map, and a callable task within a try-with-resources block.
    static Path
    Return the path to the connectors directory if the Path in parameter is a path containing a "connectors" folder.
    static long
    getLastModifiedTime(@NonNull Path path)
    Returns the time of last modification of the specified Path in milliseconds since EPOCH.
    static String
    Utility method to read the content of a file specified by a URI.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • FileHelper

      public FileHelper()
  • Method Details

    • getLastModifiedTime

      public static long getLastModifiedTime(@NonNull @NonNull Path path)
      Returns the time of last modification of the specified Path in milliseconds since EPOCH.
      Parameters:
      path - The path to the file.
      Returns:
      Milliseconds since EPOCH, or 0 (zero) if the file does not exist.
      Throws:
      IllegalArgumentException - If the specified path is null.
    • findConnectorsDirectory

      public static Path findConnectorsDirectory(URI zipUri)
      Return the path to the connectors directory if the Path in parameter is a path containing a "connectors" folder.
      Parameters:
      zipUri - The path where to look for the connectors directory
      Returns:
      The Path of the connector directory
    • fileSystemTask

      public static void fileSystemTask(@NonNull @NonNull URI uri, @NonNull @NonNull Map<String,?> env, @NonNull @NonNull Runnable runnable) throws IOException
      Executes a file system task using the provided URI, environment map, and a runnable task within a try-with-resources block. This method creates a new file system based on the specified URI and the provided environment map. It then executes the provided runnable task within the context of this file system. The file system is automatically closed when the task completes or if an exception is thrown.
      Parameters:
      uri - The non-null URI specifying the file system to be created.
      env - The non-null map of file system provider-specific properties and options.
      runnable - The non-null task to be executed within the created file system.
      Throws:
      IOException - If an I/O error occurs while creating or operating on the file system.
    • fileSystemTask

      public static <T> T fileSystemTask(@NonNull @NonNull URI uri, @NonNull @NonNull Map<String,?> env, @NonNull @NonNull Callable<T> callable) throws Exception
      Executes a file system task using the provided URI, environment map, and a callable task within a try-with-resources block. This method creates a new file system based on the specified URI and the provided environment map. It then executes the provided callable task within the context of this file system. The file system is automatically closed when the task completes or if an exception is thrown.
      Type Parameters:
      T -
      Parameters:
      uri - The non-null URI specifying the file system to be created.
      env - The non-null map of file system provider-specific properties and options.
      callable - The non-null task to be executed within the created file system.
      Returns:
      Throws:
      IOException - If an I/O error occurs while creating or operating on the file system.
      Exception
    • readFileContent

      public static String readFileContent(Path filePath) throws IOException
      Utility method to read the content of a file specified by a URI. The file content is read line by line and joined into a single string.
      Parameters:
      filePath - The path of the file to be read.
      Returns:
      A string representing the content of the file.
      Throws:
      IOException - If an I/O error occurs while reading the file.