Class URLs


  • public class URLs
    extends java.lang.Object
    Various URL manipulating functions.
    Author:
    Garret Wilson
    See Also:
    URL
    • Constructor Summary

      Constructors 
      Constructor Description
      URLs()  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static java.net.URL createURL​(java.lang.Object contextObject, java.lang.String filename)
      Deprecated. 
      static java.net.URL getDirectoryURL​(java.net.URL url)
      Deprecated. 
      static java.io.File getFile​(java.net.URL url)
      Retrieves a File representing the file of the URL.
      static java.lang.String getFileName​(java.net.URL url)
      Retrieves the file name of the URL.
      static java.io.InputStream getInputStream​(java.net.URL url)
      Returns an input stream from given URL by establishing a connection to the requested URL.
      static ContentType getMediaType​(java.net.URL url)
      Returns the media type for the specified URL based on its file extension.
      static java.lang.String getRelativePath​(java.net.URL contextURL, java.net.URL url)
      Deprecated. 
      static <T> T read​(java.net.URL url, IO<T> io)
      Reads an object from a URL using the given I/O support.
      static byte[] readBytes​(java.net.URL url)
      Loads the contents of a URL into an array of bytes.
      static java.lang.String readString​(java.net.URL url, java.nio.charset.Charset charset)
      Loads the contents of a URL into a string.
      static java.net.URI toURI​(java.net.URL url)
      Converts a URL to a URI.
      static void write​(java.net.URL url, java.io.OutputStream outputStream)
      Stores the contents of a URL in an output stream.
      • Methods inherited from class java.lang.Object

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

      • URLs

        public URLs()
    • Method Detail

      • getFile

        public static java.io.File getFile​(java.net.URL url)
        Retrieves a File representing the file of the URL.
        Parameters:
        url - The URL for which to return a file.
        Returns:
        A File object representing the file path of the URL with no query string.
      • getFileName

        public static java.lang.String getFileName​(java.net.URL url)
        Retrieves the file name of the URL.
        Parameters:
        url - The URL for which to return a file name.
        Returns:
        The name of the file in the URL.
        See Also:
        getFile(URL)
      • getMediaType

        public static ContentType getMediaType​(java.net.URL url)
        Returns the media type for the specified URL based on its file extension.
        Parameters:
        url - The URL for which to return a media type.
        Returns:
        The default media type for the URL's file extension, or null if no known media type is associated with this URL's extension.
      • createURL

        public static java.net.URL createURL​(java.lang.Object contextObject,
                                             java.lang.String filename)
                                      throws java.net.MalformedURLException
        Deprecated.
        Creates a URL from the given filename relative to the given context object. This correctly creates URLs when the filename is a fragment beginning with "#".
        Parameters:
        contextObject - The source context, such as a URL or File, or null if the filename should not be referenced from any object.
        filename - The name of the file, either relative or absolute, or a URL fragment beginning with "#".
        Returns:
        A URL constructed from the filename and context object, or null if no URL could be constructed.
        Throws:
        java.net.MalformedURLException - Thrown if the filename is not a valid filename or URL name.
        See Also:
        File, URL
      • getDirectoryURL

        public static java.net.URL getDirectoryURL​(java.net.URL url)
                                            throws java.net.MalformedURLException
        Deprecated.
        Returns a URL representing the directory of the given file URL. (It is assumed that the given URL represents a file.)
        Parameters:
        url - The URL of a file.
        Returns:
        A URL of the file's directory, ending with '/'.
        Throws:
        java.net.MalformedURLException - Thrown if a directory URL cannot be created.
      • getInputStream

        public static java.io.InputStream getInputStream​(java.net.URL url)
                                                  throws java.io.IOException
        Returns an input stream from given URL by establishing a connection to the requested URL.

        This method knows how to follow HTTP redirects.

        This method fulfills the requirements of InputStreamLocator.

        Parameters:
        url - A complete URL to a file.
        Returns:
        An input stream to the contents of the file represented by the given URL.
        Throws:
        java.io.IOException - Thrown if an I/O error occurred.
      • getRelativePath

        public static java.lang.String getRelativePath​(java.net.URL contextURL,
                                                       java.net.URL url)
                                                throws java.net.MalformedURLException
        Deprecated.
        Returns a relative path to the URL from the given context URL. This version requires the file to be on the same branch of the context path (e.g. "http://abc.de/a/c/d.html" is not on the same branch of "http://abc.de/a/b").
        Parameters:
        contextURL - The reference URL to use in making the relative path.
        url - The URL for which a relative path should be returned, in relation to the context URL.
        Returns:
        A relative path to the URL in relation to the context URL.
        Throws:
        java.net.MalformedURLException - Thrown if a relative URL cannot be determined from the context URL.
      • read

        public static <T> T read​(java.net.URL url,
                                 IO<T> io)
                          throws java.io.IOException
        Reads an object from a URL using the given I/O support.
        Type Parameters:
        T - The type of the object to be read.
        Parameters:
        url - The URL from which to read.
        io - The I/O support for reading the object.
        Returns:
        The object read from the URL.
        Throws:
        java.lang.NullPointerException - if the given URL and/or I/O support is null.
        java.lang.IllegalArgumentException - if the given URL not formatted strictly according to to RFC 2396 and cannot be converted to a URI.
        java.io.IOException - if there is an error reading the data.
      • readBytes

        public static byte[] readBytes​(java.net.URL url)
                                throws java.io.IOException
        Loads the contents of a URL into an array of bytes.
        Parameters:
        url - The URL from which to read.
        Returns:
        An array of bytes from the URL.
        Throws:
        java.io.IOException - Thrown if there is an error loading the bytes.
        See Also:
        InputStreams.getBytes(InputStream)
      • readString

        public static java.lang.String readString​(java.net.URL url,
                                                  java.nio.charset.Charset charset)
                                           throws java.io.IOException
        Loads the contents of a URL into a string.
        Parameters:
        url - The URL from which to read.
        charset - The charset used to store the string.
        Returns:
        A string containing the contents of the URL.
        Throws:
        java.io.IOException - if there is an error loading the bytes.
      • toURI

        public static java.net.URI toURI​(java.net.URL url)
        Converts a URL to a URI.

        This method functions similar to URL.toURI(), except that for those few URLs which are not conforming URIs will result in a IllegalArgumentException being thrown. In addition, the resulting URI is made to truly conform to RFC 3986 by making sure any non-ASCII characters are encoded.

        Parameters:
        url - The URI to convert to a URI.
        Returns:
        The URL as a URI.
        Throws:
        java.lang.IllegalArgumentException - if the given URL not formatted strictly according to to RFC 2396 and cannot be converted to a URI.
        See Also:
        URL.toURI(), URIs.canonicalize(URI)
      • write

        public static void write​(java.net.URL url,
                                 java.io.OutputStream outputStream)
                          throws java.io.IOException
        Stores the contents of a URL in an output stream.
        Parameters:
        url - The URL to copy.
        outputStream - The destination of the URL contents.
        Throws:
        java.io.IOException - Thrown if there is an error copying the URL.