Class InMemoryFileAccess
java.lang.Object
dev.jcputney.elearning.parser.api.AbstractArchiveFileAccess
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();
}
-
Field Summary
Fields inherited from class dev.jcputney.elearning.parser.api.AbstractArchiveFileAccess
rootPath -
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.Provides all file paths from storage for root path detection.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 dev.jcputney.elearning.parser.api.AbstractArchiveFileAccess
checkForRootPath, detectRootPath, getRootPath, initializeRootPath, normalizeDirectory, stripRootPathMethods 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
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
-
fileExistsInternal
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:
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.- 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.- 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.- 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.- 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- Overrides:
closein classAbstractArchiveFileAccess
-
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
Provides all file paths from storage for root path detection.- Specified by:
getStorageFilePathsin classAbstractArchiveFileAccess- Returns:
- An iterable of all file paths in storage format
-