Class InMemoryFileAccess

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

public final class InMemoryFileAccess extends AbstractArchiveFileAccess
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

    • fileExistsInternal

      public boolean fileExistsInternal(String path)
      Checks if a file specified by the given path exists in the in-memory file system.
      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.
      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.
      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.
      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.
      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
      Overrides:
      close in class AbstractArchiveFileAccess
    • 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.
    • getStorageFilePaths

      protected Iterable<String> getStorageFilePaths()
      Provides all file paths from storage for root path detection.
      Specified by:
      getStorageFilePaths in class AbstractArchiveFileAccess
      Returns:
      An iterable of all file paths in storage format