Class URLUtils

    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void attachURLStreamHandlerFactory​(java.net.URLStreamHandlerFactory factory)
      Set the specified URLStreamHandlerFactory for URL's if not set before, otherwise, add it into CompositeURLStreamHandlerFactory that will be set.
      static java.lang.String buildMatrixString​(java.lang.String name, java.lang.String... values)
      Build the Matrix String
      static java.lang.String buildMatrixString​(java.util.Map<java.lang.String,​java.util.List<java.lang.String>> matrixParameters)
      Build the Matrix String
      protected static java.lang.String buildString​(java.lang.String name, java.lang.String[] values, char separator, char joiner)  
      static java.lang.String buildURI​(java.lang.String... paths)
      Build multiple paths to URI
      protected static void clearURLStreamHandlerFactory()  
      static void close​(java.net.URLConnection conn)
      Closes a URLConnection.
      static java.lang.String decode​(java.lang.String value)
      decode(String, String) with "UTF-8" encoding
      static java.lang.String decode​(java.lang.String value, java.lang.String encoding)
      Decodes a application/x-www-form-urlencoded string using a specific encoding scheme.
      protected static java.lang.String doResolveArchiveEntryPath​(java.lang.String path)  
      protected static java.io.File doResolveArchiveFile​(java.net.URL url)  
      static java.lang.String encode​(java.lang.String value)
      encode(String, String) with "UTF-8" encoding
      static java.lang.String encode​(java.lang.String value, java.lang.String encoding)
      Translates a string into application/x-www-form-urlencoded format using a specific encoding scheme.
      protected static java.lang.String getFirst​(java.util.Map<java.lang.String,​java.util.List<java.lang.String>> parameters, java.lang.String name)  
      protected static MutableURLStreamHandlerFactory getMutableURLStreamHandlerFactory()  
      protected static MutableURLStreamHandlerFactory getMutableURLStreamHandlerFactory​(boolean createIfAbsent)  
      static java.lang.String getSubProtocol​(java.lang.String url)  
      static java.net.URLStreamHandlerFactory getURLStreamHandlerFactory()  
      protected static int indexOfMatrixString​(java.lang.String value)  
      static boolean isArchiveURL​(java.net.URL url)
      Is an archive URL?
      static boolean isDirectoryURL​(java.net.URL url)
      Is directory URL?
      static boolean isJarURL​(java.net.URL url)
      Is Jar URL?
      static java.lang.String normalizePath​(java.lang.String path)
      Normalize Path(maybe from File or URL), will remove duplicated slash or backslash from path.
      static java.net.URL ofURL​(java.lang.String url)
      Convert the url to URL
      protected static java.lang.String reformProtocol​(java.lang.String protocol, java.lang.String spec)  
      protected static java.lang.String reformProtocol​(java.lang.String protocol, java.util.List<java.lang.String> subProtocols)  
      static void registerURLStreamHandler​(ExtendableProtocolURLStreamHandler handler)
      Register an instance of ExtendableProtocolURLStreamHandler
      static void registerURLStreamHandler​(java.lang.String protocol, java.net.URLStreamHandler handler)
      Register an instance of URLStreamHandler with the specified protocol
      protected static java.io.File resolveArchiveDirectory​(java.net.URL resourceURL)  
      static java.lang.String resolveArchiveEntryPath​(java.net.URL archiveFileURL)
      Resolve the entry path from Archive File URL
      static java.io.File resolveArchiveFile​(java.net.URL resourceURL)
      Resolve archive file
      static java.lang.String resolveAuthority​(java.lang.String authority)  
      static java.lang.String resolveAuthority​(java.net.URL url)  
      static java.lang.String resolveBasePath​(java.net.URL url)
      Resolve base path from the specified URL
      static java.util.Map<java.lang.String,​java.util.List<java.lang.String>> resolveMatrixParameters​(java.lang.String url)
      Resolve the matrix parameters Map from specified URL,The parameter name as key ,parameter value list as key
      protected static java.util.Map<java.lang.String,​java.util.List<java.lang.String>> resolveParameters​(java.lang.String paramsString, char separatorChar)  
      static java.lang.String resolvePath​(java.net.URL url)  
      static java.lang.String resolveProtocol​(java.lang.String url)
      Resolve the protocol from the specified URL string
      static java.util.Map<java.lang.String,​java.util.List<java.lang.String>> resolveQueryParameters​(java.lang.String url)
      Resolve the query parameters Map from specified URL,The parameter name as key ,parameter value list as key
      static java.util.List<java.lang.String> resolveSubProtocols​(java.lang.String url)  
      static java.util.List<java.lang.String> resolveSubProtocols​(java.net.URL url)  
      static java.lang.String toExternalForm​(java.net.URL url)
      Converts a URL of a specific protocol to a String.
      static java.util.jar.JarFile toJarFile​(java.net.URL url)  
      protected static java.lang.String truncateMatrixString​(java.lang.String value)  
      • Methods inherited from class java.lang.Object

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

      • DEFAULT_ENCODING

        public static final java.lang.String DEFAULT_ENCODING
        The default encoding : "UTF-8"
      • EMPTY_URL_ARRAY

        public static final java.net.URL[] EMPTY_URL_ARRAY
        The empty array of URL
      • FILE_URL_PREFIX

        public static final java.lang.String FILE_URL_PREFIX
        The prefix of URL for file protocol : "file:/"
        See Also:
        Constant Field Values
      • HANDLER_PACKAGES_PROPERTY_NAME

        public static final java.lang.String HANDLER_PACKAGES_PROPERTY_NAME
        The property which specifies the package prefix list to be scanned for protocol handlers. The value of this property (if any) should be a vertical bar delimited list of package names to search through for a protocol handler to load. The policy of this class is that all protocol handlers will be in a class called .Handler, and each package in the list is examined in turn for a matching handler. If none are found (or the property is not specified), the default package prefix, sun.net.www.protocol, is used. The search proceeds from the first package in the list to the last and stops when a match is found.
        See Also:
        Constant Field Values
      • DEFAULT_HANDLER_PACKAGE_PREFIX

        public static final java.lang.String DEFAULT_HANDLER_PACKAGE_PREFIX
        The prefix of package for Handlers
        See Also:
        Constant Field Values
      • HANDLER_PACKAGES_SEPARATOR_CHAR

        public static final char HANDLER_PACKAGES_SEPARATOR_CHAR
        The separator character of Handlers' packages.
        See Also:
        Constant Field Values
      • HANDLER_CONVENTION_CLASS_NAME

        public static final java.lang.String HANDLER_CONVENTION_CLASS_NAME
        The convention class name of Handler.
        See Also:
        Constant Field Values
      • SUB_PROTOCOL_MATRIX_NAME

        public static final java.lang.String SUB_PROTOCOL_MATRIX_NAME
        The matrix name for the URLs' sub-protocol
        See Also:
        Constant Field Values
    • Method Detail

      • ofURL

        public static java.net.URL ofURL​(java.lang.String url)
        Convert the url to URL
        Parameters:
        url -
        Returns:
        non-null
        Throws:
        java.lang.IllegalArgumentException - if url is malformed
      • resolveArchiveEntryPath

        public static java.lang.String resolveArchiveEntryPath​(java.net.URL archiveFileURL)
                                                        throws java.lang.NullPointerException
        Resolve the entry path from Archive File URL
        Parameters:
        archiveFileURL - Archive File URL
        Returns:
        Relative path in archive
        Throws:
        java.lang.NullPointerException - archiveFileURL is null
      • doResolveArchiveEntryPath

        protected static java.lang.String doResolveArchiveEntryPath​(java.lang.String path)
      • resolveBasePath

        public static java.lang.String resolveBasePath​(java.net.URL url)
                                                throws java.lang.NullPointerException
        Resolve base path from the specified URL
        Parameters:
        url - the specified URL
        Returns:
        base path
        Throws:
        java.lang.NullPointerException - if url is null
      • resolveArchiveFile

        public static java.io.File resolveArchiveFile​(java.net.URL resourceURL)
                                               throws java.lang.NullPointerException
        Resolve archive file
        Parameters:
        resourceURL - the URL of resource
        Returns:
        Resolve archive file If exists
        Throws:
        java.lang.NullPointerException
      • doResolveArchiveFile

        protected static java.io.File doResolveArchiveFile​(java.net.URL url)
                                                    throws java.lang.NullPointerException
        Throws:
        java.lang.NullPointerException
      • resolveArchiveDirectory

        protected static java.io.File resolveArchiveDirectory​(java.net.URL resourceURL)
      • resolveQueryParameters

        @Nonnull
        public static java.util.Map<java.lang.String,​java.util.List<java.lang.String>> resolveQueryParameters​(java.lang.String url)
        Resolve the query parameters Map from specified URL,The parameter name as key ,parameter value list as key
        Parameters:
        url - URL
        Returns:
        Non-null and Read-only Map , the order of parameters is determined by query string
      • resolveMatrixParameters

        @Nonnull
        public static java.util.Map<java.lang.String,​java.util.List<java.lang.String>> resolveMatrixParameters​(java.lang.String url)
        Resolve the matrix parameters Map from specified URL,The parameter name as key ,parameter value list as key
        Parameters:
        url - URL
        Returns:
        Non-null and Read-only Map , the order of parameters is determined by matrix string
      • normalizePath

        public static java.lang.String normalizePath​(java.lang.String path)
        Normalize Path(maybe from File or URL), will remove duplicated slash or backslash from path. For example,

        resolvePath("C:\\Windows\\\\temp") == "C:/Windows/temp"; resolvePath("C:\\\\\Windows\\/temp") == "C:/Windows/temp"; resolvePath("/home/////index.html") == "/home/index.html";

        Parameters:
        path - Path
        Returns:
        a newly resolved path
      • encode

        public static java.lang.String encode​(java.lang.String value,
                                              java.lang.String encoding)
                                       throws java.lang.IllegalArgumentException
        Translates a string into application/x-www-form-urlencoded format using a specific encoding scheme. This method uses the supplied encoding scheme to obtain the bytes for unsafe characters.

        Note: The World Wide Web Consortium Recommendation states that UTF-8 should be used. Not doing so may introduce incompatibilites.

        Parameters:
        value - String to be translated.
        encoding - The name of a supported character encoding.
        Returns:
        the translated String.
        Throws:
        java.lang.IllegalArgumentException - If the named encoding is not supported
        See Also:
        URLDecoder.decode(String, String)
      • encode

        public static java.lang.String encode​(java.lang.String value)
        encode(String, String) with "UTF-8" encoding
        Parameters:
        value - the String to decode
        Returns:
        the newly encoded String
      • decode

        public static java.lang.String decode​(java.lang.String value)
        decode(String, String) with "UTF-8" encoding
        Parameters:
        value - the String to decode
        Returns:
        the newly decoded String
      • decode

        public static java.lang.String decode​(java.lang.String value,
                                              java.lang.String encoding)
                                       throws java.lang.IllegalArgumentException
        Decodes a application/x-www-form-urlencoded string using a specific encoding scheme. The supplied encoding is used to determine what characters are represented by any consecutive sequences of the form "%xy".

        Note: The World Wide Web Consortium Recommendation states that UTF-8 should be used. Not doing so may introduce incompatibilites.

        Parameters:
        value - the String to decode
        encoding - The name of a supported encoding
        Returns:
        the newly decoded String
        Throws:
        java.lang.IllegalArgumentException - If character encoding needs to be consulted, but named character encoding is not supported
      • isDirectoryURL

        public static boolean isDirectoryURL​(java.net.URL url)
        Is directory URL?
        Parameters:
        url - URL
        Returns:
        if directory , return true
      • isJarURL

        public static boolean isJarURL​(java.net.URL url)
        Is Jar URL?
        Parameters:
        url - URL
        Returns:
        If jar , return true
      • isArchiveURL

        public static boolean isArchiveURL​(java.net.URL url)
        Is an archive URL?
        Parameters:
        url - URL
        Returns:
        If an archive , return true
      • toJarFile

        public static java.util.jar.JarFile toJarFile​(java.net.URL url)
      • buildURI

        public static java.lang.String buildURI​(java.lang.String... paths)
        Build multiple paths to URI
        Parameters:
        paths - multiple paths
        Returns:
        URI
      • buildMatrixString

        public static java.lang.String buildMatrixString​(java.util.Map<java.lang.String,​java.util.List<java.lang.String>> matrixParameters)
        Build the Matrix String
        Parameters:
        matrixParameters - the Map of matrix parameters
        Returns:
        the Matrix String
      • buildMatrixString

        public static java.lang.String buildMatrixString​(java.lang.String name,
                                                         java.lang.String... values)
        Build the Matrix String
        Parameters:
        name - the name of matrix parameter
        values - the values of matrix parameter
        Returns:
        the Matrix String
      • toExternalForm

        public static java.lang.String toExternalForm​(java.net.URL url)
                                               throws java.lang.NullPointerException
        Converts a URL of a specific protocol to a String.
        Parameters:
        url - URL
        Returns:
        non-null
        Throws:
        java.lang.NullPointerException - If url is null
      • getSubProtocol

        public static java.lang.String getSubProtocol​(java.lang.String url)
      • resolveSubProtocols

        public static java.util.List<java.lang.String> resolveSubProtocols​(java.net.URL url)
      • resolveSubProtocols

        public static java.util.List<java.lang.String> resolveSubProtocols​(java.lang.String url)
      • resolveProtocol

        public static java.lang.String resolveProtocol​(java.lang.String url)
        Resolve the protocol from the specified URL string
        Parameters:
        url - the URL string
        Returns:
        null if can't be resolved
      • resolveAuthority

        public static java.lang.String resolveAuthority​(java.net.URL url)
      • resolveAuthority

        public static java.lang.String resolveAuthority​(java.lang.String authority)
      • resolvePath

        public static java.lang.String resolvePath​(java.net.URL url)
      • truncateMatrixString

        protected static java.lang.String truncateMatrixString​(java.lang.String value)
      • indexOfMatrixString

        protected static int indexOfMatrixString​(java.lang.String value)
      • attachURLStreamHandlerFactory

        public static void attachURLStreamHandlerFactory​(java.net.URLStreamHandlerFactory factory)
        Set the specified URLStreamHandlerFactory for URL's if not set before, otherwise, add it into CompositeURLStreamHandlerFactory that will be set.
        Parameters:
        factory - URLStreamHandlerFactory
      • getURLStreamHandlerFactory

        public static java.net.URLStreamHandlerFactory getURLStreamHandlerFactory()
      • registerURLStreamHandler

        public static void registerURLStreamHandler​(java.lang.String protocol,
                                                    java.net.URLStreamHandler handler)
        Register an instance of URLStreamHandler with the specified protocol
        Parameters:
        protocol - the specified protocol of URL
        handler - URLStreamHandler
      • close

        public static void close​(java.net.URLConnection conn)
        Closes a URLConnection.
        Parameters:
        conn - the connection to close.
      • clearURLStreamHandlerFactory

        protected static void clearURLStreamHandlerFactory()
      • reformProtocol

        protected static java.lang.String reformProtocol​(java.lang.String protocol,
                                                         java.lang.String spec)
      • reformProtocol

        protected static java.lang.String reformProtocol​(java.lang.String protocol,
                                                         java.util.List<java.lang.String> subProtocols)
      • resolveParameters

        protected static java.util.Map<java.lang.String,​java.util.List<java.lang.String>> resolveParameters​(java.lang.String paramsString,
                                                                                                                  char separatorChar)
      • buildString

        protected static java.lang.String buildString​(java.lang.String name,
                                                      java.lang.String[] values,
                                                      char separator,
                                                      char joiner)
      • getFirst

        protected static java.lang.String getFirst​(java.util.Map<java.lang.String,​java.util.List<java.lang.String>> parameters,
                                                   java.lang.String name)