- java.lang.Object
-
- org.arakhne.afc.vmutil.Resources
-
public final class Resources extends Object
This utility class provides to load resources. The following heuristics are applied:- search the resource in class paths;
- search the resource in ./resources subdirectory in class paths.
- Since:
- 4.2
- Version:
- 17.0 2020-01-04 14:41:35
- Author:
- Stéphane GALLAND
- Maven Group Id:
- org.arakhne.afc.core
- Maven Artifact Id:
- vmutils
-
-
Field Summary
Fields Modifier and Type Field Description static StringNAME_SEPARATORCharacter used to separate paths on an resource name.
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static URLgetPropertyFile(Class<?> classname, Locale locale)Replies the URL of a property resource that is associated to the given class.static URLgetPropertyFile(ClassLoader classLoader, Class<?> classname, Locale locale)Deprecated.since 17.0.static URLgetResource(Class<?> classname, String path)Replies the URL of a resource.static URLgetResource(ClassLoader classLoader, Package packagename, String path)Deprecated.since 17.0.static URLgetResource(ClassLoader classLoader, String path)Deprecated.since 17.0.static URLgetResource(String path)Replies the URL of a resource.static InputStreamgetResourceAsStream(Class<?> classname, String path)Replies the input stream of a resource.static InputStreamgetResourceAsStream(ClassLoader classLoader, Package packagename, String path)Deprecated.since 17.0.static InputStreamgetResourceAsStream(ClassLoader classLoader, String path)Deprecated.since 17.0.static InputStreamgetResourceAsStream(String path)Replies the input stream of a resource.static StringtranslateResourceName(String resourceName)Translate the given resource name according to the current JVM standard.
-
-
-
Field Detail
-
NAME_SEPARATOR
public static final String NAME_SEPARATOR
Character used to separate paths on an resource name.- See Also:
- Constant Field Values
-
-
Method Detail
-
getResource
@Pure public static URL getResource(String path)
Replies the URL of a resource.You may use Unix-like syntax to write the resource path, ie. you may use slashes to separate filenames.
This function tries to use the class loader of the
Caller.getCallerClass()calling class} for retrieving the resource. If the calling class cannot be obtained or accessed, the class loader replied byClassLoaderFinderis used. If it isnull, the class loader of the Resources class is used.- Parameters:
path- is the absolute path of the resource.- Returns:
- the url of the resource or
nullif the resource was not found in class paths.
-
getResource
@Pure @Deprecated(since="17.0") public static URL getResource(ClassLoader classLoader, Package packagename, String path)
Deprecated.since 17.0. According to the new module path management (since Java 9), it is preferable to use the class for searching a resource. SeegetResource(Class, String).Replies the URL of a resource.You may use Unix-like syntax to write the resource path, ie. you may use slashes to separate filenames.
The name of
packagenameis translated into a resource path (by replacing the dots by slashes) and the given path is append to. For example, the two following codes are equivalent:Resources.getResources(Package.getPackage("org.arakhne.afc"), "/a/b/c/d.png"); Resources.getResources("org/arakhne/afc/a/b/c/d.png");This function tries to use the class loader of the
Caller.getCallerClass()calling class} for retrieving the resource. If the calling class cannot be obtained or accessed, the class loader replied byClassLoaderFinderis used. If this last isnull, the class loader of the Resources class is used.- Parameters:
classLoader- is the research scope. Ifnull, the class loader replied byClassLoaderFinderis used.packagename- is the package in which the resource should be located.path- is the relative path of the resource in the package.- Returns:
- the url of the resource or
nullif the resource was not found in class paths. - Since:
- 6.2
-
getResource
@Pure public static URL getResource(Class<?> classname, String path)
Replies the URL of a resource.You may use Unix-like syntax to write the resource path, ie. you may use slashes to separate filenames.
The name of
classnameis translated into a resource path (by remove the name of the class and replacing the dots by slashes) and the given path is append to. For example, the two following codes are equivalent:Resources.getResource(Resources.class, "/a/b/c/d.png"); Resources.getResource("org/arakhne/vmutil/a/b/c/d.png");The class loader of the given class is used. If it is
null, the class loader replied byClassLoaderFinderis used. If it is alsonull, the class loader of this Resources class is used.- Parameters:
classname- is located in the package in which the resource should be also located.path- is the absolute path of the resource.- Returns:
- the url of the resource or
nullif the resource was not found in class paths.
-
getResource
@Pure @Deprecated(since="17.0") public static URL getResource(ClassLoader classLoader, String path)
Deprecated.since 17.0. According to the new module path management (since Java 9), it is preferable to use the class for searching a resource. SeegetResource(Class, String).Replies the URL of a resource.You may use Unix-like syntax to write the resource path, ie. you may use slashes to separate filenames.
If the
classLoaderparameter isnull, the class loader replied byClassLoaderFinderis used. If this last isnull, the class loader of the Resources class is used.- Parameters:
classLoader- is the research scope. Ifnull, the class loader replied byClassLoaderFinderis used.path- is the absolute path of the resource.- Returns:
- the url of the resource or
nullif the resource was not found in class paths.
-
getResourceAsStream
@Pure public static InputStream getResourceAsStream(String path)
Replies the input stream of a resource.You may use Unix-like syntax to write the resource path, ie. you may use slashes to separate filenames, and may not start the path with a slash.
The class loader replied by
ClassLoaderFinderis used. If it isnull, the class loader of the Resources class is used.- Parameters:
path- is the absolute path of the resource.- Returns:
- the url of the resource or
nullif the resource was not found in class paths.
-
getResourceAsStream
@Pure @Deprecated(since="17.0") public static InputStream getResourceAsStream(ClassLoader classLoader, Package packagename, String path)
Deprecated.since 17.0. According to the new module path management (since Java 9), it is preferable to use the class for searching a resource. SeegetResourceAsStream(Class, String).Replies the input stream of a resource.You may use Unix-like syntax to write the resource path, ie. you may use slashes to separate filenames.
The name of
packagenameis translated into a resource path (by replacing the dots by slashes) and the given path is append to. For example, the two following codes are equivalent:Resources.getResources(Package.getPackage("org.arakhne.afc"), "/a/b/c/d.png"); Resources.getResources("org/arakhne/afc/a/b/c/d.png");If the
classLoaderparameter isnull, the class loader replied byClassLoaderFinderis used. If this last isnull, the class loader of the Resources class is used.- Parameters:
classLoader- is the research scope. Ifnull, the class loader replied byClassLoaderFinderis used.packagename- is the package in which the resource should be located.path- is the relative path of the resource in the package.- Returns:
- the url of the resource or
nullif the resource was not found in class paths. - Since:
- 6.2
-
getResourceAsStream
@Pure public static InputStream getResourceAsStream(Class<?> classname, String path)
Replies the input stream of a resource.You may use Unix-like syntax to write the resource path, ie. you may use slashes to separate filenames.
The name of
classnameis translated into a resource path (by remove the name of the class and replacing the dots by slashes) and the given path is append to. For example, the two following codes are equivalent:Resources.getResources(Resources.class, "/a/b/c/d.png"); Resources.getResources("org/arakhne/vmutil/a/b/c/d.png");The class loader of the given class is used. If it is
null, the class loader replied byClassLoaderFinderis used. If it is alsonull, the class loader of this Resources class is used.- Parameters:
classname- is located in the package in which the resource should be also located.path- is the absolute path of the resource.- Returns:
- the url of the resource or
nullif the resource was not found in class paths.
-
getResourceAsStream
@Pure @Deprecated(since="17.0") public static InputStream getResourceAsStream(ClassLoader classLoader, String path)
Deprecated.since 17.0. According to the new module path management (since Java 9), it is preferable to use the class for searching a resource. SeegetResourceAsStream(Class, String).Replies the input stream of a resource.You may use Unix-like syntax to write the resource path, ie. you may use slashes to separate filenames, and may not start the path with a slash.
If the
classLoaderparameter isnull, the class loader replied byClassLoaderFinderis used. If this last isnull, the class loader of the Resources class is used.- Parameters:
classLoader- is the research scope. Ifnull, the class loader replied byClassLoaderFinderis used.path- is the absolute path of the resource.- Returns:
- the url of the resource or
nullif the resource was not found in class paths.
-
getPropertyFile
@Pure public static URL getPropertyFile(Class<?> classname, Locale locale)
Replies the URL of a property resource that is associated to the given class.- Parameters:
classname- is the class for which the property resource should be replied.locale- is the expected localization of the resource file; ornullfor the default.- Returns:
- the url of the property resource or
nullif the resource was not found in class paths. - Since:
- 7.0
-
getPropertyFile
@Pure @Deprecated(since="17.0") public static URL getPropertyFile(ClassLoader classLoader, Class<?> classname, Locale locale)
Deprecated.since 17.0. According to the new module path management (since Java 9), it is preferable to use the class for searching a resource. SeegetPropertyFile(Class, Locale).Replies the URL of a property resource that is associated to the given class.- Parameters:
classLoader- is the research scope. Ifnull, the class loader replied byClassLoaderFinderis used.classname- is the class for which the property resource should be replied.locale- is the expected localization of the resource file; ornullfor the default.- Returns:
- the url of the property resource or
nullif the resource was not found in class paths.
-
translateResourceName
@Pure public static String translateResourceName(String resourceName)
Translate the given resource name according to the current JVM standard.The
resourceNameargument should be a fully qualified class name. However, for compatibility with earlier versions, Sun's Java SE Runtime Environments do not verify this, and so it is possible to accessPropertyResourceBundles by specifying a path name (using "/") instead of a fully qualified class name (using "."). In several VM, such as Dalvik, the translation from "." to "/" is not automatically done by the VM to retreive the file.- Parameters:
resourceName- the name to translate.- Returns:
- the translated resource name.
- Since:
- 7.0
-
-