public class ResourceLoader
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
private java.util.List<java.lang.String> |
allSearchDirs |
protected java.lang.String |
baseDir |
protected java.lang.ClassLoader |
loader |
protected static char |
pathSep |
protected java.util.List<java.lang.String> |
searchDirs |
Constructor and Description |
---|
ResourceLoader() |
ResourceLoader(java.lang.Class resourcePeerClass,
java.lang.String pathRelativeToClassPackage)
Create a new ResourceLoader and set its resource directory based on the
package name of the specified class by converting each dot (.) in the package
name to a slash (/).
|
ResourceLoader(java.lang.String resourceBaseDir) |
ResourceLoader(java.lang.String resourceBaseDir,
java.lang.ClassLoader loader) |
Modifier and Type | Method and Description |
---|---|
void |
addSearchDir(java.lang.String dir)
Add a sub-directory (relative to the resource base directory) that will be
searched to find requested resources.
|
void |
addSearchDirs(java.lang.String... dirs)
Add a list of sub-directories (relative to the resource base directory) that will be
searched to find requested resources.
|
private static java.io.IOException |
createNotFoundException(java.lang.String path) |
java.util.List<java.lang.String> |
getAllSearchDirs() |
java.lang.String |
getBaseDir() |
static java.lang.String |
getDirFromPackage(java.lang.Class<?> c,
java.lang.String relativeSubDir) |
static java.lang.String |
getDirFromPackage(java.lang.Package p,
java.lang.String relativeSubDir) |
java.lang.String |
getResourceLocation(java.lang.String resourceName)
Returns the full path to a resource if it is found, or null otherwise.
|
java.util.List<java.lang.String> |
getSearchDirs() |
java.io.InputStream |
getStream(java.lang.String resourceName)
Searches for the specified resource in all search directories and returns an
an InputStream representing the resource.
|
java.io.InputStream |
getStream(java.lang.String resourceName,
boolean ignoreMissing) |
boolean |
hasResource(java.lang.String resourceName)
Determine whether the specified resource exists.
|
void |
setBaseDir(java.lang.Class<?> resourcePeerClass,
java.lang.String pathRelativeToClassPackage)
Set the resource directory based on the package name of the specified class
by converting each dot (.) in the package name to a slash (/).
|
void |
setBaseDir(java.lang.String pathRelativeToClassPath) |
void |
setLoader(java.lang.ClassLoader c) |
java.io.InputStream |
tryGetStream(java.lang.String resourceName)
Searches for the specified resource in all search directories and returns either
an InputStream (if the resource is found) or null (if it cannot be found).
|
private java.util.List<java.lang.String> allSearchDirs
protected java.lang.String baseDir
protected java.lang.ClassLoader loader
protected static final char pathSep
protected java.util.List<java.lang.String> searchDirs
public ResourceLoader()
public ResourceLoader(java.lang.Class resourcePeerClass, java.lang.String pathRelativeToClassPackage)
ResourceLoader(my.package.MyClass.class, "resources")
would set the
resource base directory to my/package/resources
.resourcePeerClass
- The Class from which to obtain the package name.pathRelativeToClassPackage
- This specifies a sub-directory relative to the
package-derived directory from which resources can be obtained. (This can be
null or empty, in which case resources will be served directly from the
package-derived directory).public ResourceLoader(java.lang.String resourceBaseDir)
public ResourceLoader(java.lang.String resourceBaseDir, java.lang.ClassLoader loader)
public void addSearchDir(java.lang.String dir)
dir
- The subdirectory to add. If the directory starts with a slash (/)
it indicates that it is an absolute directory instead of being
relative to the resource base directory.public void addSearchDirs(java.lang.String... dirs)
dirs
- A list of subdirectories to add. If a directory starts with a slash (/)
it indicates that it is an absolute directory instead of being
relative to the resource base directory.private static java.io.IOException createNotFoundException(java.lang.String path)
public java.util.List<java.lang.String> getAllSearchDirs()
public java.lang.String getBaseDir()
public static java.lang.String getDirFromPackage(@NotNull java.lang.Class<?> c, java.lang.String relativeSubDir)
public static java.lang.String getDirFromPackage(@NotNull java.lang.Package p, java.lang.String relativeSubDir)
public java.lang.String getResourceLocation(java.lang.String resourceName)
public java.util.List<java.lang.String> getSearchDirs()
public java.io.InputStream getStream(java.lang.String resourceName) throws java.io.IOException
resourceName
- The name of the resource to find.java.io.IOException
- - The specified resource was not found or could not be loaded.public java.io.InputStream getStream(java.lang.String resourceName, boolean ignoreMissing) throws java.io.IOException
java.io.IOException
public boolean hasResource(java.lang.String resourceName)
resourceName
- The resource to locate.public void setBaseDir(@NotNull java.lang.Class<?> resourcePeerClass, java.lang.String pathRelativeToClassPackage)
ResourceLoader(my.package.MyClass.class, "resources")
would set the
resource base directory to my/package/resources
.resourcePeerClass
- The Class from which to obtain the package name.pathRelativeToClassPackage
- This specifies a sub-directory relative to the
package-derived directory from which resources can be obtained. (This can be
null or empty, in which case resources will be served directly from the
package-derived directory).public void setBaseDir(java.lang.String pathRelativeToClassPath)
public void setLoader(@Nullable java.lang.ClassLoader c)
public java.io.InputStream tryGetStream(java.lang.String resourceName)
resourceName
- The name of the resource to find.