Class InMemoryFileAccess
java.lang.Object
dev.jcputney.elearning.parser.impl.access.InMemoryFileAccess
- All Implemented Interfaces:
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 Summary
ConstructorsConstructorDescriptionInMemoryFileAccess(byte[] zipData) Constructs a newInMemoryFileAccessinstance from a byte array containing ZIP data.InMemoryFileAccess(InputStream zipInputStream) Constructs a newInMemoryFileAccessinstance from an InputStream containing ZIP data. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()No-op for in-memory implementation since there are no external resources to close.booleanfileExistsInternal(String path) Checks if a file specified by the given path exists in the in-memory file system.Retrieves a list of all file paths stored in memory.intGets the number of directories detected in the ZIP structure.Retrieves the contents of a file as an InputStream.getFileContentsInternal(String path, StreamingProgressListener progressListener) Retrieves the contents of a file as an InputStream with optional progress tracking.intGets the number of files loaded in memory.Retrieves the root path for the in-memory file system.longRetrieves the total size of all files stored in the in-memory file system.listFilesInternal(String directoryPath) Lists all files available within the specified directory in the in-memory file system.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
checkForRootPath, clearCaches, fileExists, fileExistsBatch, fullPath, getFileContents, listFiles, prefetchCommonFiles
-
Constructor Details
-
InMemoryFileAccess
Constructs a newInMemoryFileAccessinstance 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
Constructs a newInMemoryFileAccessinstance 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
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:
getRootPathin interfaceFileAccess- Returns:
- The root path as a string.
-
fileExistsInternal
Checks if a file specified by the given path exists in the in-memory file system.- Specified by:
fileExistsInternalin interfaceFileAccess- Parameters:
path- The relative or absolute path of the file to check. Must not be null.- Returns:
trueif the file exists;falseotherwise.
-
listFilesInternal
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:
listFilesInternalin interfaceFileAccess- 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
Retrieves the contents of a file as an InputStream. This method internally delegates to an overloaded version that allows optional progress tracking.- Specified by:
getFileContentsInternalin interfaceFileAccess- 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
Retrieves a list of all file paths stored in memory.- Specified by:
getAllFilesin interfaceFileAccess- 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
Retrieves the total size of all files stored in the in-memory file system.- Specified by:
getTotalSizein interfaceFileAccess- 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:
closein interfaceAutoCloseable
-
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.
-