Class ResourceUtils.SafeFileAccess
java.lang.Object
dev.jcputney.elearning.parser.util.ResourceUtils.SafeFileAccess
- All Implemented Interfaces:
FileAccess
- Enclosing class:
- ResourceUtils
Wraps a FileAccess to ensure streams are properly closed. This prevents resource leaks when
parsers don't close streams properly.
-
Constructor Summary
ConstructorsConstructorDescriptionSafeFileAccess(FileAccess delegate) Constructs a SafeFileAccess that wraps the provided FileAccess instance. -
Method Summary
Modifier and TypeMethodDescriptionbooleanfileExistsInternal(String path) Checks if a file exists at the specified path using the wrapped FileAccess implementation.Retrieves the contents of a file as an InputStream while ensuring proper resource management.Gets the underlying FileAccess instance.listFilesInternal(String directoryPath) Lists all files in the specified directory path by delegating the operation to the underlying FileAccess implementation.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface dev.jcputney.elearning.parser.api.FileAccess
fileExists, fullPath, getFileContents, listFiles
-
Constructor Details
-
SafeFileAccess
Constructs a SafeFileAccess that wraps the provided FileAccess instance.- Parameters:
delegate- The FileAccess instance to wrap- Throws:
IllegalArgumentException- if delegate is null
-
-
Method Details
-
getRootPath
Gets the underlying FileAccess instance.- Specified by:
getRootPathin interfaceFileAccess- Returns:
- The wrapped FileAccess instance
-
fileExistsInternal
Checks if a file exists at the specified path using the wrapped FileAccess implementation.- Specified by:
fileExistsInternalin interfaceFileAccess- Parameters:
path- The path of the file to check.- Returns:
- True if the file exists at the specified path, false otherwise.
-
listFilesInternal
Lists all files in the specified directory path by delegating the operation to the underlying FileAccess implementation.- Specified by:
listFilesInternalin interfaceFileAccess- Parameters:
directoryPath- The path of the directory from which files will be listed.- Returns:
- A list of file paths within the specified directory.
- Throws:
IOException- If an error occurs while accessing the directory or retrieving its contents.
-
getFileContentsInternal
Retrieves the contents of a file as an InputStream while ensuring proper resource management. This method wraps the returned InputStream in aCloseLoggingInputStreamto log a warning if the caller does not properly close the stream.- Specified by:
getFileContentsInternalin interfaceFileAccess- Parameters:
path- The path of the file to retrieve contents from. Must not be null.- Returns:
- An InputStream to read the file's contents.
- Throws:
IOException- If an error occurs while accessing the file.IllegalArgumentException- If the provided path is null.
-