Class URL
- All Implemented Interfaces:
Serializable
It could do with a lot more trimming, and use of native js encodeURIComponent.
Any volunteers with some existing unit tests who'd like to improve client side URL performance?
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]convertFromIPv4MappedAddress(byte[] addr) Convert IPv4-Mapped address to IPv4 address.booleanCompares this URL for equality with another object.Gets the authority part of thisURL.intGets the default port number of the protocol associated with thisURL.getFile()Gets the file name of thisURL.getHost()Gets the host name of thisURL, if applicable.getPath()Gets the path part of thisURL.intgetPort()Gets the port number of thisURL.Gets the protocol name of thisURL.getQuery()Gets the query part of thisURL.getRef()Gets the anchor (also known as the "reference") of thisURL.Gets the userInfo part of thisURL.inthashCode()Creates an integer suitable for hash table indexing.protected voidParses the string representation of aURLinto aURLobject.protected voidSets the fields of the URL.protected voidset(String protocol, String host, int port, String authority, String userInfo, String path, String query, String ref) Sets the specified 8 fields of the URL.static byte[]Convert IPv4-Mapped address to IPv4 address.Constructs a string representation of thisURL.toString()Constructs a string representation of thisURL.
-
Constructor Details
-
URL
Creates aURLobject from theStringrepresentation.This constructor is equivalent to a call to the two-argument constructor with a
nullfirst argument.- Parameters:
spec- theStringto parse as a URL.- Throws:
MalformedURLException- If the string specifies an unknown protocol.- See Also:
-
URL
Creates a URL by parsing the given spec within a specified context. The new URL is created from the given context URL and the spec argument as described in RFC2396 "Uniform Resource Identifiers : Generic * Syntax" :
The reference is parsed into the scheme, authority, path, query and fragment parts. If the path component is empty and the scheme, authority, and query components are undefined, then the new URL is a reference to the current document. Otherwise, the fragment and query parts present in the spec are used in the new URL.<scheme>://<authority><path>?<query>#<fragment>If the scheme component is defined in the given spec and does not match the scheme of the context, then the new URL is created as an absolute URL based on the spec alone. Otherwise the scheme component is inherited from the context URL.
If the authority component is present in the spec then the spec is treated as absolute and the spec authority and path will replace the context authority and path. If the authority component is absent in the spec then the authority of the new URL will be inherited from the context.
If the spec's path component begins with a slash character "/" then the path is treated as absolute and the spec path replaces the context path.
Otherwise, the path is treated as a relative path and is appended to the context path, as described in RFC2396. Also, in this case, the path is canonicalized through the removal of directory changes made by occurences of ".." and ".".
For a more detailed description of URL parsing, refer to RFC2396.
- Parameters:
context- the context in which to parse the specification.spec- theStringto parse as a URL.- Throws:
MalformedURLException- if no protocol is specified, or an unknown protocol is found.- See Also:
-
-
Method Details
-
parseURL
Parses the string representation of aURLinto aURLobject.If there is any inherited context, then it has already been copied into the
URLargument.The
parseURLmethod ofURLStreamHandlerparses the string representation as if it were anhttpspecification. Most URL protocol families have a similar parsing. A stream protocol handler for a protocol that has a different syntax must override this routine.- Parameters:
purl- theURLto receive the result of parsing the spec.pspec- theStringrepresenting the URL that must be parsed.pstart- the character index at which to begin parsing. This is just past the ':' (if there is one) that specifies the determination of the protocol name.plimit- the character position to stop parsing at. This is the end of the string or the position of the "#" character, if present. All information after the sharp sign indicates an anchor.
-
textToNumericFormatV4
Convert IPv4-Mapped address to IPv4 address. Both input and returned value are in network order binary form.- Parameters:
src- a string representing an IPv4-Mapped address in textual format- Returns:
- a byte array representing the IPv4 numeric address
-
convertFromIPv4MappedAddress
public static byte[] convertFromIPv4MappedAddress(byte[] addr) Convert IPv4-Mapped address to IPv4 address. Both input and returned value are in network order binary form.- Parameters:
addr- a representing an IPv4-Mapped address in textual format- Returns:
- a byte array representing the IPv4 numeric address
-
set
Sets the fields of the URL. This is not a public method so that only URLStreamHandlers can modify URL fields. URLs are otherwise constant.- Parameters:
protocol- the name of the protocol to usehost- the name of the hostport- the port number on the hostfile- the file on the hostref- the internal reference in the URL
-
set
protected void set(String protocol, String host, int port, String authority, String userInfo, String path, String query, String ref) Sets the specified 8 fields of the URL. This is not a public method so that only URLStreamHandlers can modify URL fields. URLs are otherwise constant.- Parameters:
protocol- the name of the protocol to usehost- the name of the hostport- the port number on the hostauthority- the authority part for the urluserInfo- the username and passwordpath- the file on the hostref- the internal reference in the URLquery- the query part of this URL- Since:
- 1.3
-
getQuery
Gets the query part of thisURL.- Returns:
- the query part of this
URL, ornullif one does not exist - Since:
- 1.3
-
getPath
Gets the path part of thisURL.- Returns:
- the path part of this
URL, or an empty string if one does not exist - Since:
- 1.3
-
getUserInfo
Gets the userInfo part of thisURL.- Returns:
- the userInfo part of this
URL, ornullif one does not exist - Since:
- 1.3
-
getAuthority
Gets the authority part of thisURL.- Returns:
- the authority part of this
URL - Since:
- 1.3
-
getPort
public int getPort()Gets the port number of thisURL.- Returns:
- the port number, or -1 if the port is not set
-
getDefaultPort
public int getDefaultPort()Gets the default port number of the protocol associated with thisURL. If the URL scheme or the URLStreamHandler for the URL do not define a default port number, then -1 is returned.- Returns:
- the port number
- Since:
- 1.4
-
getProtocol
Gets the protocol name of thisURL.- Returns:
- the protocol of this
URL.
-
getHost
Gets the host name of thisURL, if applicable. The format of the host conforms to RFC 2732, i.e. for a literal IPv6 address, this method will return the IPv6 address enclosed in square brackets ('['and']').- Returns:
- the host name of this
URL.
-
getFile
Gets the file name of thisURL. The returned file portion will be the same asgetPath(), plus the concatenation of the value ofgetQuery(), if any. If there is no query portion, this method andgetPath()will return identical results.- Returns:
- the file name of this
URL, or an empty string if one does not exist
-
getRef
Gets the anchor (also known as the "reference") of thisURL.- Returns:
- the anchor (also known as the "reference") of this
URL, ornullif one does not exist
-
equals
Compares this URL for equality with another object.If the given object is not a URL then this method immediately returns
false.Two URL objects are equal if they have the same protocol, reference equivalent hosts, have the same port number on the host, and the same file and fragment of the file.
Two hosts are considered equivalent if both host names can be resolved into the same IP addresses; else if either host name can't be resolved, the host names must be equal without regard to case; or both host names equal to null.
Since hosts comparison requires name resolution, this operation is a blocking operation.
Note: The defined behavior for
equalsis known to be inconsistent with virtual hosting in HTTP. -
hashCode
public int hashCode()Creates an integer suitable for hash table indexing.The hash code is based upon all the URL components relevant for URL comparison. As such, this operation is a blocking operation.
-
openStream
-
toString
Constructs a string representation of thisURL. The string is created by calling thetoExternalFormmethod of the stream protocol handler for this object. -
toExternalForm
Constructs a string representation of thisURL. The string is created by calling thetoExternalFormmethod of the stream protocol handler for this object.- Returns:
- a string representation of this object.
- See Also:
-
openConnection
- Throws:
IOException
-