Index (Frames) | Index (No Frames) | Package | Package Tree | Tree
java.net

Class URL

java.lang.Object
|
+--java.net.URL

All Implemented Interfaces:

Serializable


public final class URL

extends Object

implements Serializable

This final class represents an Internet Uniform Resource Locator (URL). For details on the syntax of URL's and what they can be used for, refer to RFC 1738, available from http://ds.internic.net/rfcs/rfc1738.txt

There are a great many protocols supported by URL's such as "http", "ftp", and "file". This object can handle any arbitrary URL for which a URLStreamHandler object can be written. Default protocol handlers are provided for the "http" and "ftp" protocols. Additional protocols handler implementations may be provided in the future. In any case, an application or applet can install its own protocol handlers that can be "chained" with other protocol hanlders in the system to extend the base functionality provided with this class. (Note, however, that unsigned applets cannot access properties by default or install their own protocol handlers).

This chaining is done via the system property java.protocol.handler.pkgs If this property is set, it is assumed to be a "|" separated list of package names in which to attempt locating protocol handlers. The protocol handler is searched for by appending the string "..Handler" to each packed in the list until a hander is found. If a protocol handler is not found in this list of packages, or if the property does not exist, then the default protocol handler of "gnu.java.net..Handler" is tried. If this is unsuccessful, a MalformedURLException is thrown.

All of the constructor methods of URL attempt to load a protocol handler and so any needed protocol handlers must be installed when the URL is constructed.

Here is an example of how URL searches for protocol handlers. Assume the value of java.protocol.handler.pkgs is "com.foo|com.bar" and the URL is "news://comp.lang.java.programmer". URL would looking the following places for protocol handlers:

 com.foo.news.Handler
 com.bar.news.Handler
 gnu.java.net.news.Handler
 

If the protocol handler is not found in any of those locations, a MalformedURLException would be thrown.

Please note that a protocol handler must be a subclass of URLStreamHandler.

Normally, this class caches protocol handlers. Once it finds a handler for a particular protocol, it never tries to look up a new handler again. However, if the system property gnu.java.net.nocache_protocol_handlers is set, then this caching behavior is disabled. This property is specific to this implementation. Sun's JDK may or may not do protocol caching, but it almost certainly does not examine this property.

Please also note that an application can install its own factory for loading protocol handlers (see setURLStreamHandlerFactory). If this is done, then the above information is superseded and the behavior of this class in loading protocol handlers is dependent on that factory.

Authors:See Also:

Constructor Summary

URL(java.lang.String protocol, java.lang.String host, int port, java.lang.String file)

Constructs a URL and loads a protocol handler for the values passed as arguments.
URL(java.lang.String protocol, java.lang.String host, java.lang.String file)

Constructs a URL and loads a protocol handler for the values passed in as arugments.
URL(java.lang.String protocol, java.lang.String host, int port, java.lang.String file, java.net.URLStreamHandler handler)

This method initializes a new instance of URL with the specified protocol, host, port, and file.
URL(java.lang.String spec)

Initializes a URL from a complete string specification such as "http://www.urbanophile.com/arenn/".
URL(java.net.URL context, java.lang.String spec)

URL(java.net.URL context, java.lang.String spec, java.net.URLStreamHandler handler)

Creates an URL from given arguments This method parses a String representation of a URL within the context of an existing URL.

Method Summary

booleanequals(java.lang.Object obj)

Test another URL for equality with this one.
java.lang.StringgetAuthority()

Returns the authority of the URL
java.lang.ObjectgetContent()

Returns the contents of this URL as an object by first opening a connection, then calling the getContent() method against the connection
java.lang.ObjectgetContent(java.lang.Class classes)

Gets the contents of this URL
intgetDefaultPort()

Returns the default port of the URL.
java.lang.StringgetFile()

Returns the file portion of the URL.
java.lang.StringgetHost()

Returns the host of the URL
java.lang.StringgetPath()

Returns the path of the URL.
intgetPort()

Returns the port number of this URL or -1 if the default port number is being used.
java.lang.StringgetProtocol()

Returns the protocol of the URL
java.lang.StringgetQuery()

Returns the query of the URL.
java.lang.StringgetRef()

Returns the ref (sometimes called the "# reference" or "anchor") portion of the URL.
java.lang.StringgetUserInfo()

Returns the user information of the URL.
inthashCode()

Returns a hashcode computed by the URLStreamHandler of this URL
java.net.URLConnectionopenConnection()

Returns a URLConnection object that represents a connection to the remote object referred to by the URL.
java.io.InputStreamopenStream()

Opens a connection to this URL and returns an InputStream for reading from that connection
booleansameFile(java.net.URL other)

Tests whether or not another URL refers to the same "file" as this one.
voidset(java.lang.String protocol, java.lang.String host, int port, java.lang.String file, java.lang.String ref)

Sets the specified fields of the URL.
voidset(java.lang.String protocol, java.lang.String host, int port, java.lang.String authority, java.lang.String userInfo, java.lang.String path, java.lang.String query, java.lang.String ref)

Sets the specified fields of the URL.
static synchronized voidsetURLStreamHandlerFactory(java.net.URLStreamHandlerFactory fac)

Sets the URLStreamHandlerFactory for this class.
java.lang.StringtoExternalForm()

Returns a String representing this URL.
java.lang.StringtoString()

Returns a String representing this URL.

Constructor Details

URL

public URL(java.lang.String spec)

Initializes a URL from a complete string specification such as "http://www.urbanophile.com/arenn/". First the protocol name is parsed out of the string. Then a handler is located for that protocol and the parseURL() method of that protocol handler is used to parse the remaining fields.

Parameters:

Throws:


URL

public URL(java.lang.String protocol, java.lang.String host, int port, java.lang.String file)

Constructs a URL and loads a protocol handler for the values passed as arguments.

Parameters:

Throws:


URL

public URL(java.lang.String protocol, java.lang.String host, int port, java.lang.String file, java.net.URLStreamHandler handler)

This method initializes a new instance of URL with the specified protocol, host, port, and file. Additionally, this method allows the caller to specify a protocol handler to use instead of the default. If this handler is specified, the caller must have the "specifyStreamHandler" permission (see NetPermission) or a SecurityException will be thrown.

Since:Parameters:

Throws:


URL

public URL(java.lang.String protocol, java.lang.String host, java.lang.String file)

Constructs a URL and loads a protocol handler for the values passed in as arugments. Uses the default port for the protocol.

Parameters:

Throws:


URL

public URL(java.net.URL context, java.lang.String spec)

Parameters:


URL

public URL(java.net.URL context, java.lang.String spec, java.net.URLStreamHandler handler)

Creates an URL from given arguments This method parses a String representation of a URL within the context of an existing URL. Principally this means that any fields not present the URL are inheritied from the context URL. This allows relative URL's to be easily constructed. If the context argument is null, then a complete URL must be specified in the URL string. If the protocol parsed out of the URL is different from the context URL's protocol, then then URL String is also expected to be a complete URL.

Additionally, this method allows the caller to specify a protocol handler to use instead of the default. If this handler is specified, the caller must have the "specifyStreamHandler" permission (see NetPermission) or a SecurityException will be thrown.

Since:Parameters:

Throws:


Method Details

equals

public boolean equals(java.lang.Object obj)

Test another URL for equality with this one. This will be true only if the argument is non-null and all of the fields in the URL's match exactly (ie, protocol, host, port, file, and ref). Overrides Object.equals(), implemented by calling the equals method of the handler.

Parameters:

Returns:


getAuthority

public String getAuthority()

Returns the authority of the URL

Since:

getContent

public final Object getContent()

Returns the contents of this URL as an object by first opening a connection, then calling the getContent() method against the connection

Since:Returns:

Throws:


getContent

public final Object getContent(java.lang.Class classes)

Gets the contents of this URL

Parameters:

Throws:


getDefaultPort

public int getDefaultPort()

Returns the default port of the URL. If the StreamHandler for the URL protocol does not define a default port it returns -1.


getFile

public String getFile()

Returns the file portion of the URL. Defined as path[?query]. Returns the empty string if there is no file portion.


getHost

public String getHost()

Returns the host of the URL


getPath

public String getPath()

Returns the path of the URL. This is the part of the file before any '?' character.

Since:

getPort

public int getPort()

Returns the port number of this URL or -1 if the default port number is being used.

Returns:

See Also:


getProtocol

public String getProtocol()

Returns the protocol of the URL


getQuery

public String getQuery()

Returns the query of the URL. This is the part of the file before the '?'.


getRef

public String getRef()

Returns the ref (sometimes called the "# reference" or "anchor") portion of the URL.

Returns:


getUserInfo

public String getUserInfo()

Returns the user information of the URL. This is the part of the host name before the '@'.

Returns:


hashCode

public int hashCode()

Returns a hashcode computed by the URLStreamHandler of this URL


openConnection

public URLConnection openConnection()

Returns a URLConnection object that represents a connection to the remote object referred to by the URL. The URLConnection is created by calling the openConnection() method of the protocol handler

Returns:

Throws:


openStream

public final InputStream openStream()

Opens a connection to this URL and returns an InputStream for reading from that connection

Throws:


sameFile

public boolean sameFile(java.net.URL other)

Tests whether or not another URL refers to the same "file" as this one. This will be true if and only if the passed object is not null, is a URL, and matches all fields but the ref (ie, protocol, host, port, and file);

Parameters:

Returns:


set

protected void set(java.lang.String protocol, java.lang.String host, int port, java.lang.String file, java.lang.String ref)

Sets the specified fields of the URL. This is not a public method so that only URLStreamHandlers can modify URL fields. This might be called by the parseURL() method in that class. URLs are otherwise constant.

Parameters:


set

protected void set(java.lang.String protocol, java.lang.String host, int port, java.lang.String authority, java.lang.String userInfo, java.lang.String path, java.lang.String query, java.lang.String ref)

Sets the specified fields of the URL. This is not a public method so that only URLStreamHandlers can modify URL fields. URLs are otherwise constant.

Since:Parameters:


setURLStreamHandlerFactory

public static synchronized void setURLStreamHandlerFactory(java.net.URLStreamHandlerFactory fac)

Sets the URLStreamHandlerFactory for this class. This factory is responsible for returning the appropriate protocol handler for a given URL.

Parameters:

Throws:


toExternalForm

public String toExternalForm()

Returns a String representing this URL. The String returned is created by calling the protocol handler's toExternalForm() method.

Returns:


toString

public String toString()

Returns a String representing this URL. Identical to toExternalForm(). The value returned is created by the protocol handler's toExternalForm method. Overrides Object.toString()

Returns: