Class ResourceUtils
java.lang.Object
dev.jcputney.elearning.parser.util.ResourceUtils
Utility class for managing resources safely, ensuring proper cleanup of ZipFileAccess and other
AutoCloseable resources.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classWraps a FileAccess to ensure streams are properly closed.static interfaceFunctional interface for reading from a stream. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcloseIfCloseable(FileAccess fileAccess) Ensures a FileAccess instance is properly closed if it's AutoCloseable.static <M extends PackageManifest>
ModuleMetadata<M>parseZipModule(String zipPath, Function<FileAccess, ModuleParser<M>> parserFactory) Parses a module from a ZIP file path, ensuring the ZipFileAccess is properly closed.static <T> TsafeRead(InputStream stream, ResourceUtils.StreamReader<T> reader) Safely reads content from an InputStream, ensuring it's closed properly.
-
Method Details
-
parseZipModule
public static <M extends PackageManifest> ModuleMetadata<M> parseZipModule(String zipPath, Function<FileAccess, ModuleParser<M>> parserFactory) throws ModuleParsingException, IOExceptionParses a module from a ZIP file path, ensuring the ZipFileAccess is properly closed.- Type Parameters:
M- The type of package manifest- Parameters:
zipPath- Path to the ZIP fileparserFactory- Function to create a parser from FileAccess- Returns:
- The parsed module metadata
- Throws:
ModuleParsingException- if parsing failsIOException- if file access fails
-
safeRead
public static <T> T safeRead(InputStream stream, ResourceUtils.StreamReader<T> reader) throws IOException Safely reads content from an InputStream, ensuring it's closed properly.- Type Parameters:
T- The return type- Parameters:
stream- The input stream to readreader- Function to process the stream- Returns:
- The result of the reader function
- Throws:
IOException- if reading fails
-
closeIfCloseable
Ensures a FileAccess instance is properly closed if it's AutoCloseable. This is useful for conditional cleanup when the FileAccess type is unknown.- Parameters:
fileAccess- The FileAccess instance to potentially close
-