public final class URL extends Object implements Serializable
Constructor and Description |
---|
URL(String spec)
Creates a new URL instance by parsing the string
spec . |
URL(String protocol,
String host,
int port,
String file)
Creates a new URL instance using the given arguments.
|
URL(String protocol,
String host,
int port,
String file,
URLStreamHandler handler)
Creates a new URL instance using the given arguments.
|
URL(String protocol,
String host,
String file)
Creates a new URL instance using the given arguments.
|
URL(URL context,
String spec)
Creates a new URL to the specified resource
spec . |
URL(URL context,
String spec,
URLStreamHandler handler)
Creates a new URL to the specified resource
spec . |
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object o)
Compares this URL instance with the given argument
o and
determines if both are equal. |
String |
getAuthority()
Gets the value of the authority part of this URL.
|
Object |
getContent()
Gets the content of the resource which is referred by this URL.
|
Object |
getContent(Class[] types)
Gets the content of the resource which is referred by this URL.
|
int |
getDefaultPort()
Gets the default port number of the protocol used by this URL.
|
String |
getFile()
Gets the value of the file part of this URL.
|
String |
getHost()
Gets the value of the host part of this URL.
|
String |
getPath()
Gets the value of the path part of this URL.
|
int |
getPort()
Gets the port number of this URL or
-1 if the port is not set. |
String |
getProtocol()
Gets the protocol of this URL.
|
String |
getQuery()
Gets the value of the query part of this URL.
|
String |
getRef()
Gets the value of the reference part of this URL.
|
String |
getUserInfo()
Gets the value of the user-info part of this URL.
|
int |
hashCode()
Gets the hashcode value of this URL instance.
|
URLConnection |
openConnection()
Opens a connection to the remote resource specified by this URL.
|
InputStream |
openStream()
Opens an InputStream to read the resource referred by this URL.
|
boolean |
sameFile(URL otherURL)
Returns whether this URL refers to the same resource as the given
argument
otherURL . |
protected void |
set(String protocol,
String host,
int port,
String file,
String ref)
Sets the properties of this URL using the provided arguments.
|
protected void |
set(String protocol,
String host,
int port,
String authority,
String userInfo,
String path,
String query,
String ref)
Sets the properties of this URL using the provided arguments.
|
static void |
setURLStreamHandlerFactory(URLStreamHandlerFactory streamFactory)
Sets the
URLStreamHandlerFactory which creates protocol specific
stream handlers. |
String |
toExternalForm()
Returns a string containing a concise, human-readable representation of
this URL.
|
String |
toString()
Returns a string containing a concise, human-readable representation of
this URL.
|
public URL(String spec) throws MalformedURLException
spec
.spec
- the URL string representation which has to be parsed.MalformedURLException
- if the given string spec
could not be parsed as a
URL.public URL(URL context, String spec) throws MalformedURLException
spec
. This URL is
relative to the given context
. If the protocol of the parsed URL
does not match with the protocol of the context URL, then the newly
created URL is absolute and bases only on the given URL represented by
spec
. Otherwise the protocol is defined by the context URL.context
- the URL which is used as the context.spec
- the URL string representation which has to be parsed.MalformedURLException
- if the given string spec
could not be parsed as a URL
or an invalid protocol has been found.public URL(URL context, String spec, URLStreamHandler handler) throws MalformedURLException
spec
. This URL is
relative to the given context
. The handler
will be used
to parse the URL string representation. If this argument is null
the default URLStreamHandler
will be used. If the protocol of the
parsed URL does not match with the protocol of the context URL, then the
newly created URL is absolute and bases only on the given URL represented
by spec
. Otherwise the protocol is defined by the context URL.context
- the URL which is used as the context.spec
- the URL string representation which has to be parsed.handler
- the specific stream handler to be used by this URL.MalformedURLException
- if the given string spec
could not be parsed as a URL
or an invalid protocol has been found.public URL(String protocol, String host, String file) throws MalformedURLException
protocol
- the protocol of the new URL.host
- the host name or IP address of the new URL.file
- the name of the resource.MalformedURLException
- if the combination of all arguments do not represent a valid
URL or the protocol is invalid.public URL(String protocol, String host, int port, String file) throws MalformedURLException
protocol
- the protocol of the new URL.host
- the host name or IP address of the new URL.port
- the specific port number of the URL. -1
represents the
default port of the protocol.file
- the name of the resource.MalformedURLException
- if the combination of all arguments do not represent a valid
URL or the protocol is invalid.public URL(String protocol, String host, int port, String file, URLStreamHandler handler) throws MalformedURLException
protocol
- the protocol of the new URL.host
- the host name or IP address of the new URL.port
- the specific port number of the URL. -1
represents the
default port of the protocol.file
- the name of the resource.handler
- the stream handler to be used by this URL.MalformedURLException
- if the combination of all arguments do not represent a valid
URL or the protocol is invalid.SecurityException
- if handler
is non-null
, and a security
manager is installed that disallows user-defined protocol
handlers.public static void setURLStreamHandlerFactory(URLStreamHandlerFactory streamFactory)
URLStreamHandlerFactory
which creates protocol specific
stream handlers. This method can be invoked only once during an
application's lifetime. If the URLStreamHandlerFactory
is already
set an Error
will be thrown.
A security check is performed to verify whether the current policy allows to set the stream handler factory.
streamFactory
- the factory to be used for creating stream protocol handlers.protected void set(String protocol, String host, int port, String file, String ref)
URLStreamHandler
can use this method to set fields of the
existing URL instance. A URL is generally constant.protocol
- the protocol to be set.host
- the host name to be set.port
- the port number to be set.file
- the file to be set.ref
- the reference to be set.public boolean equals(Object o)
o
and
determines if both are equal. Two URL instances are equal if all single
parts are identical in their meaning. Compares the argument to the
receiver, and returns true if they represent the same URL. Two URLs are
equal if they have the same file, host, port, protocol, and reference
components.equals
in class Object
o
- the URL this instance has to be compared with.true
if both instances represents the same URL, false
otherwise.hashCode()
public boolean sameFile(URL otherURL)
otherURL
. All URL components except the reference field
are compared.otherURL
- the URL to compare against.true
if both instances refer to the same resource,
false
otherwise.public int hashCode()
public final Object getContent() throws IOException
InputStream
for all other dataIOException
- if an error occurs obtaining the content.public final Object getContent(Class[] types) throws IOException
types
is an array of allowed or expected object types.
null
will be returned if the obtained object type does not match
with one from this list. Otherwise the first type that matches will be
used.types
- the list of allowed or expected object types.null
if the content does not match to a specified content
type.IOException
- if an error occurs obtaining the content.public final InputStream openStream() throws IOException
IOException
- if an error occurs while opening the InputStream.public URLConnection openConnection() throws IOException
IOException
- if an error occurs while opening the connection.public String toString()
toExternalForm()
.public String toExternalForm()
public String getFile()
public String getHost()
public int getPort()
-1
if the port is not set.public String getProtocol()
public String getRef()
public String getQuery()
public String getPath()
public String getUserInfo()
public String getAuthority()
protected void set(String protocol, String host, int port, String authority, String userInfo, String path, String query, String ref)
URLStreamHandler
can use this method to set fields of the
existing URL instance. A URL is generally constant.protocol
- the protocol to be set.host
- the host name to be set.port
- the port number to be set.authority
- the authority to be set.userInfo
- the user-info to be set.path
- the path to be set.query
- the query to be set.ref
- the reference to be set.public int getDefaultPort()
URLStreamHandler
,
-1
will be returned.URLStreamHandler.getDefaultPort()