Class ResourceUtils.SafeFileAccess

java.lang.Object
dev.jcputney.elearning.parser.util.ResourceUtils.SafeFileAccess
All Implemented Interfaces:
FileAccess
Enclosing class:
ResourceUtils

public static class ResourceUtils.SafeFileAccess extends Object implements FileAccess
Wraps a FileAccess to ensure streams are properly closed. This prevents resource leaks when parsers don't close streams properly.
  • Constructor Details

    • SafeFileAccess

      public SafeFileAccess(FileAccess delegate)
      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

      public String getRootPath()
      Gets the underlying FileAccess instance.
      Specified by:
      getRootPath in interface FileAccess
      Returns:
      The wrapped FileAccess instance
    • fileExistsInternal

      public boolean fileExistsInternal(String path)
      Checks if a file exists at the specified path using the wrapped FileAccess implementation.
      Specified by:
      fileExistsInternal in interface FileAccess
      Parameters:
      path - The path of the file to check.
      Returns:
      True if the file exists at the specified path, false otherwise.
    • listFilesInternal

      public List<String> listFilesInternal(String directoryPath) throws IOException
      Lists all files in the specified directory path by delegating the operation to the underlying FileAccess implementation.
      Specified by:
      listFilesInternal in interface FileAccess
      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

      public InputStream getFileContentsInternal(String path) throws IOException
      Retrieves the contents of a file as an InputStream while ensuring proper resource management. This method wraps the returned InputStream in a CloseLoggingInputStream to log a warning if the caller does not properly close the stream.
      Specified by:
      getFileContentsInternal in interface FileAccess
      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.