Class FileHelper
java.lang.Object
org.sentrysoftware.metricshub.engine.common.helpers.FileHelper
Utility class for common file-related operations.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidfileSystemTask(@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> TfileSystemTask(@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 PathfindConnectorsDirectory(URI zipUri) Return the path to the connectors directory if thePathin parameter is a path containing a "connectors" folder.static StringgetBaseName(String filename) Extracts the filename without its extension.static StringgetExtension(String filename) Extracts the extension of a provided filename.static longgetLastModifiedTime(@NonNull Path path) Returns the time of last modification of the specified Path in milliseconds since EPOCH.static StringreadFileContent(Path filePath) Utility method to read the content of a file specified by a URI.
-
Constructor Details
-
FileHelper
public FileHelper()
-
-
Method Details
-
getLastModifiedTime
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
Return the path to the connectors directory if thePathin parameter is a path containing a "connectors" folder.- Parameters:
zipUri- The path where to look for the connectors directory- Returns:
- The
Pathof the connector directory
-
fileSystemTask
public static void fileSystemTask(@NonNull @NonNull URI uri, @NonNull @NonNull Map<String, ?> env, @NonNull @NonNull Runnable runnable) throws IOExceptionExecutes 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 ExceptionExecutes 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
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.
-
getExtension
Extracts the extension of a provided filename.- Parameters:
filename- The filename from which to extract the extension.- Returns:
- The extension of the file (E.g .awk)or an empty string if no extension exists.
-
getBaseName
Extracts the filename without its extension.- Parameters:
filename- The filename from which to remove the extension.- Returns:
- The filename without its extension.
-