Class ResourceUtils


  • public class ResourceUtils
    extends java.lang.Object
    A class to simplify access to ResourceUtils through the classloader.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String CLASSPATH_URL_PREFIX
      Pseudo URL prefix for loading from the class path: "classpath:".
      static java.lang.String FILE_URL_PREFIX
      URL prefix for loading from the file system: "file:".
      static java.lang.String JAR_FILE_EXTENSION
      File extension for a regular jar file: ".jar".
      static java.lang.String JAR_URL_PREFIX
      URL prefix for loading from a jar file: "jar:".
      static java.lang.String JAR_URL_SEPARATOR
      Separator between JAR URL and file path within the JAR: "!/".
      static java.lang.String REGULAR_FILE_SEPARATOR  
      static char REGULAR_FILE_SEPARATOR_CHAR  
      static java.lang.String URL_PROTOCOL_FILE
      URL protocol for a file in the file system: "file".
      static java.lang.String URL_PROTOCOL_JAR
      URL protocol for an entry from a jar file: "jar".
      static java.lang.String URL_PROTOCOL_VFS
      URL protocol for a general JBoss VFS resource: "vfs".
      static java.lang.String URL_PROTOCOL_VFSFILE
      URL protocol for a JBoss file system resource: "vfsfile".
      static java.lang.String URL_PROTOCOL_VFSZIP
      URL protocol for an entry from a JBoss jar file: "vfszip".
      static java.lang.String URL_PROTOCOL_WAR
      URL protocol for an entry from a war file: "war".
      static java.lang.String URL_PROTOCOL_WSJAR
      URL protocol for an entry from a WebSphere jar file: "wsjar".
      static java.lang.String URL_PROTOCOL_ZIP
      URL protocol for an entry from a zip file: "zip".
      static java.lang.String WAR_URL_PREFIX
      URL prefix for loading from a war file on Tomcat: "war:".
      static java.lang.String WAR_URL_SEPARATOR
      Special separator between WAR URL and jar part on Tomcat.
    • Constructor Summary

      Constructors 
      Constructor Description
      ResourceUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.net.URL extractArchiveURL​(java.net.URL jarUrl)
      Extract the URL for the outermost archive from the given jar/war URL (which may point to a resource in a jar file or to a jar file itself).
      static java.net.URL extractJarFileURL​(java.net.URL jarUrl)
      Extract the URL for the actual jar file from the given URL (which may point to a resource in a jar file or to a jar file itself).
      static java.io.File getFile​(java.lang.String resourceLocation)
      Resolve the given resource location to a java.io.File, i.e.
      static java.io.File getFile​(java.lang.String resourceLocation, java.lang.ClassLoader classLoader)
      Resolve the given resource location to a java.io.File, i.e.
      static java.io.File getFile​(java.net.URI resourceUri)
      Resolve the given resource URI to a java.io.File, i.e.
      static java.io.File getFile​(java.net.URI resourceUri, java.lang.String description)
      Resolve the given resource URI to a java.io.File, i.e.
      static java.io.File getFile​(java.net.URL resourceUrl)
      Resolve the given resource URL to a java.io.File, i.e.
      static java.io.File getFile​(java.net.URL resourceUrl, java.lang.String description)
      Resolve the given resource URL to a java.io.File, i.e.
      static java.io.Reader getReader​(java.io.File file, java.lang.String encoding)
      Returns a Reader for reading the specified file.
      static java.io.Reader getReader​(java.net.URL url, java.lang.String encoding)
      Returns a Reader for reading the specified url.
      static java.net.URL getResource​(java.lang.String resource)
      Returns the URL of the resource on the classpath.
      static java.net.URL getResource​(java.lang.String resource, java.lang.ClassLoader classLoader)
      Returns the URL of the resource on the classpath.
      static java.io.File getResourceAsFile​(java.lang.String resource)  
      static java.io.File getResourceAsFile​(java.lang.String resource, java.lang.ClassLoader classLoader)  
      static java.io.InputStream getResourceAsStream​(java.lang.String resource)
      Returns a resource on the classpath as a Stream object.
      static java.io.InputStream getResourceAsStream​(java.lang.String resource, java.lang.ClassLoader classLoader)
      Returns a resource on the classpath as a Stream object.
      static java.net.URL getURL​(java.lang.String resourceLocation)
      Resolve the given resource location to a java.net.URL.
      static java.net.URL getURL​(java.lang.String resourceLocation, java.lang.ClassLoader classLoader)
      Resolve the given resource location to a java.net.URL.
      static boolean isFileURL​(java.net.URL url)
      Determine whether the given URL points to a resource in the file system, i.e.
      static boolean isJarFileURL​(java.net.URL url)
      Determine whether the given URL points to a jar file itself, that is, has protocol "file" and ends with the ".jar" extension.
      static boolean isJarURL​(java.net.URL url)
      Determine whether the given URL points to a resource in a jar file.
      static boolean isUrl​(java.lang.String resourceLocation)
      Return whether the given resource location is a URL: either a special "classpath" pseudo URL or a standard URL.
      static java.lang.String read​(java.io.File file, java.lang.String encoding)
      Returns a string from the specified file.
      static java.lang.String read​(java.io.Reader reader)
      Returns a string from the specified Reader object.
      static java.lang.String read​(java.net.URL url, java.lang.String encoding)
      Returns a string from the specified url.
      static java.net.URI toURI​(java.lang.String location)
      Create a URI instance for the given location String, replacing spaces with "%20" URI encoding first.
      static java.net.URI toURI​(java.net.URL url)
      Create a URI instance for the given URL, replacing spaces with "%20" URI encoding first.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • CLASSPATH_URL_PREFIX

        public static final java.lang.String CLASSPATH_URL_PREFIX
        Pseudo URL prefix for loading from the class path: "classpath:".
        See Also:
        Constant Field Values
      • FILE_URL_PREFIX

        public static final java.lang.String FILE_URL_PREFIX
        URL prefix for loading from the file system: "file:".
        See Also:
        Constant Field Values
      • JAR_URL_PREFIX

        public static final java.lang.String JAR_URL_PREFIX
        URL prefix for loading from a jar file: "jar:".
        See Also:
        Constant Field Values
      • WAR_URL_PREFIX

        public static final java.lang.String WAR_URL_PREFIX
        URL prefix for loading from a war file on Tomcat: "war:".
        See Also:
        Constant Field Values
      • URL_PROTOCOL_FILE

        public static final java.lang.String URL_PROTOCOL_FILE
        URL protocol for a file in the file system: "file".
        See Also:
        Constant Field Values
      • URL_PROTOCOL_JAR

        public static final java.lang.String URL_PROTOCOL_JAR
        URL protocol for an entry from a jar file: "jar".
        See Also:
        Constant Field Values
      • URL_PROTOCOL_WAR

        public static final java.lang.String URL_PROTOCOL_WAR
        URL protocol for an entry from a war file: "war".
        See Also:
        Constant Field Values
      • URL_PROTOCOL_ZIP

        public static final java.lang.String URL_PROTOCOL_ZIP
        URL protocol for an entry from a zip file: "zip".
        See Also:
        Constant Field Values
      • URL_PROTOCOL_WSJAR

        public static final java.lang.String URL_PROTOCOL_WSJAR
        URL protocol for an entry from a WebSphere jar file: "wsjar".
        See Also:
        Constant Field Values
      • URL_PROTOCOL_VFSZIP

        public static final java.lang.String URL_PROTOCOL_VFSZIP
        URL protocol for an entry from a JBoss jar file: "vfszip".
        See Also:
        Constant Field Values
      • URL_PROTOCOL_VFSFILE

        public static final java.lang.String URL_PROTOCOL_VFSFILE
        URL protocol for a JBoss file system resource: "vfsfile".
        See Also:
        Constant Field Values
      • URL_PROTOCOL_VFS

        public static final java.lang.String URL_PROTOCOL_VFS
        URL protocol for a general JBoss VFS resource: "vfs".
        See Also:
        Constant Field Values
      • JAR_FILE_EXTENSION

        public static final java.lang.String JAR_FILE_EXTENSION
        File extension for a regular jar file: ".jar".
        See Also:
        Constant Field Values
      • JAR_URL_SEPARATOR

        public static final java.lang.String JAR_URL_SEPARATOR
        Separator between JAR URL and file path within the JAR: "!/".
        See Also:
        Constant Field Values
      • WAR_URL_SEPARATOR

        public static final java.lang.String WAR_URL_SEPARATOR
        Special separator between WAR URL and jar part on Tomcat.
        See Also:
        Constant Field Values
      • REGULAR_FILE_SEPARATOR

        public static final java.lang.String REGULAR_FILE_SEPARATOR
        See Also:
        Constant Field Values
      • REGULAR_FILE_SEPARATOR_CHAR

        public static final char REGULAR_FILE_SEPARATOR_CHAR
        See Also:
        Constant Field Values
    • Constructor Detail

      • ResourceUtils

        public ResourceUtils()
    • Method Detail

      • isUrl

        public static boolean isUrl​(@Nullable
                                    java.lang.String resourceLocation)
        Return whether the given resource location is a URL: either a special "classpath" pseudo URL or a standard URL.
        Parameters:
        resourceLocation - the location String to check
        Returns:
        whether the location qualifies as a URL
        See Also:
        CLASSPATH_URL_PREFIX, URL
      • getURL

        public static java.net.URL getURL​(java.lang.String resourceLocation)
                                   throws java.io.FileNotFoundException
        Resolve the given resource location to a java.net.URL.

        Does not check whether the URL actually exists; simply returns the URL that the given location would correspond to.

        Parameters:
        resourceLocation - the resource location to resolve: either a "classpath:" pseudo URL, a "file:" URL, or a plain file path
        Returns:
        a corresponding URL object
        Throws:
        java.io.FileNotFoundException - if the resource cannot be resolved to a URL
      • getURL

        public static java.net.URL getURL​(java.lang.String resourceLocation,
                                          java.lang.ClassLoader classLoader)
                                   throws java.io.FileNotFoundException
        Resolve the given resource location to a java.net.URL.

        Does not check whether the URL actually exists; simply returns the URL that the given location would correspond to.

        Parameters:
        resourceLocation - the resource location to resolve: either a "classpath:" pseudo URL, a "file:" URL, or a plain file path
        classLoader - the class loader
        Returns:
        a corresponding URL object
        Throws:
        java.io.FileNotFoundException - if the resource cannot be resolved to a URL
      • getFile

        public static java.io.File getFile​(java.lang.String resourceLocation)
                                    throws java.io.FileNotFoundException
        Resolve the given resource location to a java.io.File, i.e. to a file in the file system.

        Does not check whether the file actually exists; simply returns the File that the given location would correspond to.

        Parameters:
        resourceLocation - the resource location to resolve: either a "classpath:" pseudo URL, a "file:" URL, or a plain file path
        Returns:
        a corresponding File object
        Throws:
        java.io.FileNotFoundException - if the resource cannot be resolved to a file in the file system
      • getFile

        public static java.io.File getFile​(java.lang.String resourceLocation,
                                           java.lang.ClassLoader classLoader)
                                    throws java.io.FileNotFoundException
        Resolve the given resource location to a java.io.File, i.e. to a file in the file system.

        Does not check whether the file actually exists; simply returns the File that the given location would correspond to.

        Parameters:
        resourceLocation - the resource location to resolve: either a "classpath:" pseudo URL, a "file:" URL, or a plain file path
        classLoader - the class loader
        Returns:
        a corresponding File object
        Throws:
        java.io.FileNotFoundException - if the resource cannot be resolved to a file in the file system
      • getFile

        public static java.io.File getFile​(java.net.URL resourceUrl)
                                    throws java.io.FileNotFoundException
        Resolve the given resource URL to a java.io.File, i.e. to a file in the file system.
        Parameters:
        resourceUrl - the resource URL to resolve
        Returns:
        a corresponding File object
        Throws:
        java.io.FileNotFoundException - if the URL cannot be resolved to a file in the file system
      • getFile

        public static java.io.File getFile​(java.net.URL resourceUrl,
                                           java.lang.String description)
                                    throws java.io.FileNotFoundException
        Resolve the given resource URL to a java.io.File, i.e. to a file in the file system.
        Parameters:
        resourceUrl - the resource URL to resolve
        description - a description of the original resource that the URL was created for (for example, a class path location)
        Returns:
        a corresponding File object
        Throws:
        java.io.FileNotFoundException - if the URL cannot be resolved to a file in the file system
      • getFile

        public static java.io.File getFile​(java.net.URI resourceUri)
                                    throws java.io.FileNotFoundException
        Resolve the given resource URI to a java.io.File, i.e. to a file in the file system.
        Parameters:
        resourceUri - the resource URI to resolve
        Returns:
        a corresponding File object
        Throws:
        java.io.FileNotFoundException - if the URL cannot be resolved to a file in the file system
      • getFile

        public static java.io.File getFile​(java.net.URI resourceUri,
                                           java.lang.String description)
                                    throws java.io.FileNotFoundException
        Resolve the given resource URI to a java.io.File, i.e. to a file in the file system.
        Parameters:
        resourceUri - the resource URI to resolve
        description - a description of the original resource that the URI was created for (for example, a class path location)
        Returns:
        a corresponding File object
        Throws:
        java.io.FileNotFoundException - if the URL cannot be resolved to a file in the file system
      • isFileURL

        public static boolean isFileURL​(java.net.URL url)
        Determine whether the given URL points to a resource in the file system, i.e. has protocol "file", "vfsfile" or "vfs".
        Parameters:
        url - the URL to check
        Returns:
        whether the URL has been identified as a file system URL
      • isJarURL

        public static boolean isJarURL​(java.net.URL url)
        Determine whether the given URL points to a resource in a jar file. i.e. has protocol "jar", "war, ""zip", "vfszip" or "wsjar".
        Parameters:
        url - the URL to check
        Returns:
        whether the URL has been identified as a JAR URL
      • isJarFileURL

        public static boolean isJarFileURL​(java.net.URL url)
        Determine whether the given URL points to a jar file itself, that is, has protocol "file" and ends with the ".jar" extension.
        Parameters:
        url - the URL to check
        Returns:
        whether the URL has been identified as a JAR file URL
      • extractJarFileURL

        public static java.net.URL extractJarFileURL​(java.net.URL jarUrl)
                                              throws java.net.MalformedURLException
        Extract the URL for the actual jar file from the given URL (which may point to a resource in a jar file or to a jar file itself).
        Parameters:
        jarUrl - the original URL
        Returns:
        the URL for the actual jar file
        Throws:
        java.net.MalformedURLException - if no valid jar file URL could be extracted
      • extractArchiveURL

        public static java.net.URL extractArchiveURL​(java.net.URL jarUrl)
                                              throws java.net.MalformedURLException
        Extract the URL for the outermost archive from the given jar/war URL (which may point to a resource in a jar file or to a jar file itself).

        In the case of a jar file nested within a war file, this will return a URL to the war file since that is the one resolvable in the file system.

        Parameters:
        jarUrl - the original URL
        Returns:
        the URL for the actual jar file
        Throws:
        java.net.MalformedURLException - if no valid jar file URL could be extracted
        See Also:
        extractJarFileURL(URL)
      • toURI

        public static java.net.URI toURI​(java.net.URL url)
                                  throws java.net.URISyntaxException
        Create a URI instance for the given URL, replacing spaces with "%20" URI encoding first.
        Parameters:
        url - the URL to convert into a URI instance
        Returns:
        the URI instance
        Throws:
        java.net.URISyntaxException - if the URL wasn't a valid URI
        See Also:
        URL.toURI()
      • toURI

        public static java.net.URI toURI​(java.lang.String location)
                                  throws java.net.URISyntaxException
        Create a URI instance for the given location String, replacing spaces with "%20" URI encoding first.
        Parameters:
        location - the location String to convert into a URI instance
        Returns:
        the URI instance
        Throws:
        java.net.URISyntaxException - if the location wasn't a valid URI
      • getResource

        public static java.net.URL getResource​(java.lang.String resource)
                                        throws java.io.IOException
        Returns the URL of the resource on the classpath.
        Parameters:
        resource - the resource to find
        Returns:
        URL object for reading the resource; null if the resource could not be found
        Throws:
        java.io.IOException - if the resource cannot be found or read
      • getResource

        public static java.net.URL getResource​(java.lang.String resource,
                                               java.lang.ClassLoader classLoader)
                                        throws java.io.IOException
        Returns the URL of the resource on the classpath.
        Parameters:
        classLoader - the class loader used to load the resource
        resource - the resource to find
        Returns:
        URL object for reading the resource; null if the resource could not be found
        Throws:
        java.io.IOException - if the resource cannot be found or read
      • getResourceAsFile

        public static java.io.File getResourceAsFile​(java.lang.String resource)
                                              throws java.io.IOException
        Throws:
        java.io.IOException
      • getResourceAsFile

        public static java.io.File getResourceAsFile​(java.lang.String resource,
                                                     java.lang.ClassLoader classLoader)
                                              throws java.io.IOException
        Throws:
        java.io.IOException
      • getResourceAsStream

        public static java.io.InputStream getResourceAsStream​(java.lang.String resource)
                                                       throws java.io.IOException
        Returns a resource on the classpath as a Stream object.
        Parameters:
        resource - the resource to find
        Returns:
        an input stream for reading the resource; null if the resource could not be found
        Throws:
        java.io.IOException - if the resource cannot be found or read
      • getResourceAsStream

        public static java.io.InputStream getResourceAsStream​(java.lang.String resource,
                                                              java.lang.ClassLoader classLoader)
                                                       throws java.io.IOException
        Returns a resource on the classpath as a Stream object.
        Parameters:
        resource - the resource to find
        classLoader - the class loader
        Returns:
        an input stream for reading the resource; null if the resource could not be found
        Throws:
        java.io.IOException - if the resource cannot be found or read
      • getReader

        public static java.io.Reader getReader​(java.io.File file,
                                               java.lang.String encoding)
                                        throws java.io.IOException
        Returns a Reader for reading the specified file.
        Parameters:
        file - the file
        encoding - the encoding
        Returns:
        the reader instance
        Throws:
        java.io.IOException - if an error occurred when reading resources using any I/O operations
      • getReader

        public static java.io.Reader getReader​(java.net.URL url,
                                               java.lang.String encoding)
                                        throws java.io.IOException
        Returns a Reader for reading the specified url.
        Parameters:
        url - the url
        encoding - the encoding
        Returns:
        the reader instance
        Throws:
        java.io.IOException - if an error occurred when reading resources using any I/O operations
      • read

        public static java.lang.String read​(java.io.File file,
                                            java.lang.String encoding)
                                     throws java.io.IOException
        Returns a string from the specified file.
        Parameters:
        file - the file
        encoding - the encoding
        Returns:
        the reader instance
        Throws:
        java.io.IOException - if an error occurred when reading resources using any I/O operations
      • read

        public static java.lang.String read​(java.net.URL url,
                                            java.lang.String encoding)
                                     throws java.io.IOException
        Returns a string from the specified url.
        Parameters:
        url - the url
        encoding - the encoding
        Returns:
        the string
        Throws:
        java.io.IOException - if an error occurred when reading resources using any I/O operations
      • read

        public static java.lang.String read​(java.io.Reader reader)
                                     throws java.io.IOException
        Returns a string from the specified Reader object.
        Parameters:
        reader - the reader
        Returns:
        the string
        Throws:
        java.io.IOException - if an error occurred when reading resources using any I/O operations