Class 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 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 by ClassLoaderFinder is used. If it is null, 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 null if 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. See getResource(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 packagename is 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 by ClassLoaderFinder is used. If this last is null, the class loader of the Resources class is used.

        Parameters:
        classLoader - is the research scope. If null, the class loader replied by ClassLoaderFinder is 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 null if 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 classname is 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 by ClassLoaderFinder is used. If it is also null, 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 null if 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. See getResource(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 classLoader parameter is null, the class loader replied by ClassLoaderFinder is used. If this last is null, the class loader of the Resources class is used.

        Parameters:
        classLoader - is the research scope. If null, the class loader replied by ClassLoaderFinder is used.
        path - is the absolute path of the resource.
        Returns:
        the url of the resource or null if 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 ClassLoaderFinder is used. If it is null, 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 null if 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. See getResourceAsStream(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 packagename is 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 classLoader parameter is null, the class loader replied by ClassLoaderFinder is used. If this last is null, the class loader of the Resources class is used.

        Parameters:
        classLoader - is the research scope. If null, the class loader replied by ClassLoaderFinder is 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 null if 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 classname is 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 by ClassLoaderFinder is used. If it is also null, 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 null if 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. See getResourceAsStream(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 classLoader parameter is null, the class loader replied by ClassLoaderFinder is used. If this last is null, the class loader of the Resources class is used.

        Parameters:
        classLoader - is the research scope. If null, the class loader replied by ClassLoaderFinder is used.
        path - is the absolute path of the resource.
        Returns:
        the url of the resource or null if 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; or null for the default.
        Returns:
        the url of the property resource or null if 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. See getPropertyFile(Class, Locale).
        Replies the URL of a property resource that is associated to the given class.
        Parameters:
        classLoader - is the research scope. If null, the class loader replied by ClassLoaderFinder is used.
        classname - is the class for which the property resource should be replied.
        locale - is the expected localization of the resource file; or null for the default.
        Returns:
        the url of the property resource or null if 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 resourceName argument 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 access PropertyResourceBundles 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