Package org.geotools.util
Class URLs
- Object
-
- URLs
-
public class URLs extends Object
Utilities for manipulating and converting to and fromURL
s.urlToFile(URL)
andfileToUrl(File)
andgetParentUrl(URL)
are used to work with files across platforms
-
-
Constructor Summary
Constructors Constructor Description URLs()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static URL
changeUrlExt(URL url, String postfix)
Changes the ending (e.g. ".sld") of aURL
static URL
extendUrl(URL base, String extension)
Extends aURL
.static URL
fileToUrl(File file)
A replacement forFile.toURL()
andFile.toURI().toURL()
.static URL
getParentUrl(URL url)
The function is supposed to be equivalent toFile.getParent()
.static File
urlToFile(URL url)
Takes a URL and converts it to a File.
-
-
-
Method Detail
-
changeUrlExt
public static URL changeUrlExt(URL url, String postfix) throws IllegalArgumentException
Changes the ending (e.g. ".sld") of aURL
- Parameters:
url
-URL
likefile:/sds/a.bmp
orhttp://www.some.org/foo/bar.shp
postfix
- New file extension for theURL
without.
- Returns:
- A new
URL
with new extension. - Throws:
IllegalArgumentException
-
extendUrl
public static URL extendUrl(URL base, String extension) throws MalformedURLException
Extends aURL
.- Parameters:
base
- Has to be aURL
pointing to a directory. If it doesn't end with a/
it will be added automatically.extension
- The part that will be added to theURL
- Throws:
MalformedURLException
- if the newURL
can not be created.
-
fileToUrl
public static URL fileToUrl(File file)
A replacement forFile.toURL()
andFile.toURI().toURL()
.File.toURL()
does not percent-encode characters andFile.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 toFile.getParent()
. TheURL
is converted to a String, truncated to the last / and then recreated as a new URL.- Throws:
MalformedURLException
-
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
-
-