Class URLs

Object
URLs

public class URLs extends Object
Utilities for manipulating and converting to and from URLs.

urlToFile(URL) and fileToUrl(File) and getParentUrl(URL) are used to work with files across platforms

  • Constructor Details

    • URLs

      public URLs()
  • Method Details

    • changeUrlExt

      public static URL changeUrlExt(URL url, String postfix) throws IllegalArgumentException
      Changes the ending (e.g. ".sld") of a URL
      Parameters:
      url - URL like file:/sds/a.bmp or http://www.some.org/foo/bar.shp
      postfix - New file extension for the URL without .
      Returns:
      A new URL with new extension.
      Throws:
      IllegalArgumentException
    • extendUrl

      public static URL extendUrl(URL base, String extension) throws MalformedURLException
      Extends a URL.
      Parameters:
      base - Has to be a URL pointing to a directory. If it doesn't end with a / it will be added automatically.
      extension - The part that will be added to the URL
      Throws:
      MalformedURLException - if the new URL can not be created.
    • fileToUrl

      public static URL fileToUrl(File file)
      A replacement for File.toURL() and File.toURI().toURL().

      File.toURL() does not percent-encode characters and File.toURI().toURL() does not percent-encode non-ASCII characters. This method ensures that URL characters are correctly percent-encoded, and works around the reported misbehaviour of some Java implementations on Mac.

      Returns:
      URL
    • getParentUrl

      public static URL getParentUrl(URL url) throws MalformedURLException
      The function is supposed to be equivalent to File.getParent(). The URL is converted to a String, truncated to the last / and then recreated as a new URL.
      Throws:
      MalformedURLException - if the parent URL can not be created.
    • urlToFile

      public static File urlToFile(URL url)
      Takes a URL and converts it to a File. The attempts to deal with Windows UNC format specific problems, specifically files located on network shares and different drives.

      If the URL.getAuthority() returns null or is empty, then only the url's path property is used to construct the file. Otherwise, the authority is prefixed before the path.

      It is assumed that url.getProtocol returns "file".

      Authority is the drive or network share the file is located on. Such as "C:", "E:", "\\fooServer"

      Parameters:
      url - a URL object that uses protocol "file"
      Returns:
      a File that corresponds to the URL's location