Package org.drasyl.crypto.loader
Class DrasylResourceLoader
- java.lang.Object
-
- org.drasyl.crypto.loader.DrasylResourceLoader
-
- Direct Known Subclasses:
DrasylSharedLibraryLoader
public class DrasylResourceLoader extends Object
Loads resources from a relative or absolute path even if the file is in a JAR.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description File
copyToTempDirectory(String relativePath, Class outsideClass)
Copies a file into a temporary directory regardless of if it is in a JAR or not.static File
createMainTempDirectory()
Creates the main temporary directory for resource-loader.File
extractFromWithinAJarFile(URL jarPath, File mainTempDir, String relativePath)
static URL
getThePathToTheJarWeAreIn(Class<?> c)
Gets the base location of the given class.protected boolean
isPosixCompliant()
Is the system we're running on Posix compliant?void
requestDeletion(File file)
Mark the file or directory as "to be deleted".File
setPermissions(File file)
Sets permissions on a file or directory.File
setPermissions(File file, Set<PosixFilePermission> filePermissions)
Sets a file or directory's permissions.static File
urlToFile(String url)
Converts the given URL string to its correspondingFile
.static File
urlToFile(URL url)
-
-
-
Method Detail
-
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:
IOException
URISyntaxException
-
createMainTempDirectory
public static File createMainTempDirectory() throws IOException
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
public File setPermissions(File file) throws IOException
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(File, Set)
-
setPermissions
public File setPermissions(File file, Set<PosixFilePermission> filePermissions) throws IOException
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
public void requestDeletion(File file)
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
public static URL getThePathToTheJarWeAreIn(Class<?> c)
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
public static File urlToFile(URL url)
Converts the givenURL
to 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
public static File urlToFile(String url)
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.
-
-