Package com.codevasp.lazysodium.utils
Class ResourceLoader
java.lang.Object
com.codevasp.lazysodium.utils.ResourceLoader
- Direct Known Subclasses:
FileLoader,SharedLibraryLoader
Loads resources from a relative or absolute path
even if the file is in a JAR.
-
Method Summary
Modifier and TypeMethodDescriptioncopyToTempDirectory(String relativePath, Class outsideClass) Copies a file into a temporary directory regardless of if it is in a JAR or not.static FileCreates the main temporary directory for resource-loader.extractFromWithinAJarFile(URL jarPath, File mainTempDir, String relativePath) static URLGets the base location of the given class.protected booleanIs the system we're running on Posix compliant?voidrequestDeletion(File file) Mark the file or directory as "to be deleted".setPermissions(File file) Sets permissions on a file or directory.setPermissions(File file, Set<PosixFilePermission> filePermissions) Sets a file or directory's permissions.static FileConverts the given URL string to its correspondingFile.static File
-
Method Details
-
copyToTempDirectory
public File copyToTempDirectory(String relativePath, Class outsideClass) throws IOException, URISyntaxException Copies a file into a temporary directory regardless of if it is in a JAR or not.- Parameters:
relativePath- A relative path to a file or directory relative to the resources folder.- Returns:
- The file or directory you want to load.
- Throws:
IOException- If at any point processing of the resource file fails.URISyntaxException- If cannot find the resource file.
-
extractFromWithinAJarFile
public File extractFromWithinAJarFile(URL jarPath, File mainTempDir, String relativePath) throws IOException, URISyntaxException - Throws:
IOExceptionURISyntaxException
-
createMainTempDirectory
Creates the main temporary directory for resource-loader.- Returns:
- A directory that you can store temporary resources in
- Throws:
IOException- Could not create a temporary directory
-
setPermissions
Sets permissions on a file or directory. This allows all users to read, write and execute.- Parameters:
file- A file to set global permissions on- Returns:
- The file with the global permissions set
- Throws:
IOException- Could not set permissions- See Also:
-
setPermissions
Sets a file or directory's permissions. @{code filePermissions} can be null, in that case then global read, wrote and execute permissions will be set, so use with caution.- Parameters:
file- The file to set new permissions on.filePermissions- New permissions.- Returns:
- The file with correct permissions set.
- Throws:
IOException
-
requestDeletion
Mark the file or directory as "to be deleted".- Parameters:
file- The file or directory to be deleted.
-
isPosixCompliant
protected boolean isPosixCompliant()Is the system we're running on Posix compliant?- Returns:
- True if posix compliant.
-
getThePathToTheJarWeAreIn
Gets the base location of the given class. From SciJava class.If the class is directly on the file system (e.g., "/path/to/my/package/MyClass.class") then it will return the base directory (e.g., "file:/path/to").
If the class is within a JAR file (e.g., "/path/to/my-jar.jar!/my/package/MyClass.class") then it will return the path to the JAR (e.g., "file:/path/to/my-jar.jar").
- Parameters:
c- The class whose location is desired.
-
urlToFile
Converts the givenURLto its correspondingFile. From SciJava class.This method is similar to calling
new File(url.toURI())except that it also handles "jar:file:" URLs, returning the path to the JAR file.- Parameters:
url- The URL to convert.- Returns:
- A file path suitable for use with e.g.
FileInputStream - Throws:
IllegalArgumentException- if the URL does not correspond to a file.
-
urlToFile
Converts the given URL string to its correspondingFile. From SciJava class.- Parameters:
url- The URL to convert.- Returns:
- A file path suitable for use with e.g.
FileInputStream - Throws:
IllegalArgumentException- if the URL does not correspond to a file.
-