Class InMemoryFileAccess

java.lang.Object
dev.jcputney.elearning.parser.impl.access.InMemoryFileAccess
All Implemented Interfaces:
FileAccess, AutoCloseable

public final class InMemoryFileAccess extends Object implements FileAccess, AutoCloseable
An implementation of the FileAccess interface for accessing files from an in-memory ZIP archive. This class allows file existence checks, file listing, and retrieving file contents from a ZIP file that is loaded into memory.

This implementation is useful for serverless environments or situations where file system access is restricted or not available. The ZIP content is read once during construction and cached in memory.

Usage example:


 byte[] zipData = // ... obtain ZIP file data from network, database, etc.
 try (InMemoryFileAccess fileAccess = new InMemoryFileAccess(zipData)) {
     ModuleParser parser = ModuleParserFactory.forZipData(zipData);
     ModuleMetadata metadata = parser.parse();
 }
 
  • Constructor Details

    • InMemoryFileAccess

      public InMemoryFileAccess(byte[] zipData) throws IOException
      Constructs a new InMemoryFileAccess instance from a byte array containing ZIP data.
      Parameters:
      zipData - The ZIP file data as a byte array.
      Throws:
      IOException - If the ZIP data can't be read or is invalid.
      IllegalArgumentException - if zipData is null.
    • InMemoryFileAccess

      public InMemoryFileAccess(InputStream zipInputStream) throws IOException
      Constructs a new InMemoryFileAccess instance from an InputStream containing ZIP data.
      Parameters:
      zipInputStream - The InputStream containing ZIP data.
      Throws:
      IOException - If the ZIP data can't be read or is invalid.
      IllegalArgumentException - if zipInputStream is null.
  • Method Details

    • getRootPath

      public String getRootPath()
      Retrieves the root path for the in-memory file system. The root path is typically determined during the initialization of the instance and provides the base directory for all file operations within the system.
      Specified by:
      getRootPath in interface FileAccess
      Returns:
      The root path as a string.
    • fileExistsInternal

      public boolean fileExistsInternal(String path)
      Checks if a file specified by the given path exists in the in-memory file system.
      Specified by:
      fileExistsInternal in interface FileAccess
      Parameters:
      path - The relative or absolute path of the file to check. Must not be null.
      Returns:
      true if the file exists; false otherwise.
    • listFilesInternal

      public List<String> listFilesInternal(String directoryPath) throws IOException
      Lists all files available within the specified directory in the in-memory file system. If the directory path is an empty string, all files in the system are returned.
      Specified by:
      listFilesInternal in interface FileAccess
      Parameters:
      directoryPath - The path of the directory to list files from. If empty, all files will be included in the result. The path should be relative or normalized.
      Returns:
      A list of file paths that exist within the specified directory.
      Throws:
      IOException - If an error occurs while retrieving file information.
    • getFileContentsInternal

      public InputStream getFileContentsInternal(String path) throws IOException
      Retrieves the contents of a file as an InputStream. This method internally delegates to an overloaded version that allows optional progress tracking.
      Specified by:
      getFileContentsInternal in interface FileAccess
      Parameters:
      path - The path of the file to retrieve contents from. Must not be null.
      Returns:
      An InputStream of the file contents.
      Throws:
      IOException - If the file cannot be found or read.
    • getFileContentsInternal

      public InputStream getFileContentsInternal(String path, StreamingProgressListener progressListener) throws IOException
      Retrieves the contents of a file as an InputStream with optional progress tracking.
      Parameters:
      path - The path to retrieve contents from (guaranteed to be non-null).
      progressListener - Optional progress listener for tracking large file operations.
      Returns:
      An InputStream of the file contents.
      Throws:
      IOException - if the file can't be found.
    • getAllFiles

      public List<String> getAllFiles() throws IOException
      Retrieves a list of all file paths stored in memory.
      Specified by:
      getAllFiles in interface FileAccess
      Returns:
      A list of strings representing the relative paths of all files in the in-memory file system.
      Throws:
      IOException - If an error occurs while accessing the file information.
    • getTotalSize

      public long getTotalSize() throws IOException
      Retrieves the total size of all files stored in the in-memory file system.
      Specified by:
      getTotalSize in interface FileAccess
      Returns:
      The total size of all files in bytes.
      Throws:
      IOException - If an error occurs while calculating the total size.
    • close

      public void close()
      No-op for in-memory implementation since there are no external resources to close. Provided for AutoCloseable compatibility with other FileAccess implementations.
      Specified by:
      close in interface AutoCloseable
    • getFileCount

      public int getFileCount()
      Gets the number of files loaded in memory.
      Returns:
      The number of files.
    • getDirectoryCount

      public int getDirectoryCount()
      Gets the number of directories detected in the ZIP structure.
      Returns:
      The number of directories.