java.net
Class URL

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

public final class URL
extends Object

Class URL represents a Uniform Reference Locator -- a reference to an object on the World Wide Web. This is a constant object, once it is created, its fields cannot be changed.


Constructor Summary
URL(String spec)
          Creates a URL from the unparsed absolute URL.
URL(String protocol, String host, int port, String file)
          Creates an absolute URL from the specified protocol, host, port and file.
URL(String protocol, String host, String file)
          Creates an absolute URL from the specified protocol, host, and file.
URL(URL context, String spec)
          Creates a URL from the unparsed URL in the specified context.If spec is an absolute URL it is used as is.
 
Method Summary
 boolean equals(Object obj)
          Compares two URLs.
 Object getContent()
          Gets the contents from this opened connection.
 String getFile()
          Gets the file name.
 String getHost()
          Gets the host name.
 int getPort()
          Gets the port number.
 String getProtocol()
          Gets the protocol name.
 String getRef()
          Gets the ref.
 int hashCode()
          Creates an integer suitable for hash table indexing.
 URLConnection openConnection()
          Creates (if not already in existance) a URLConnection object that contains a connection to the remote object referred to by the URL.
 InputStream openStream()
          Opens an input stream.
 boolean sameFile(URL other)
          Compares two URLs, excluding the "ref" fields: sameFile is true if the true references the same remote object, but not necessarily the same subpiece of that object.
protected  void set(String protocol, String host, int port, String file, String ref)
          Sets the fields of the URL.
static void setURLStreamHandlerFactory(URLStreamHandlerFactory fac)
          Sets the URLStreamHandler factory.
 String toExternalForm()
          Reverses the parsing of the URL.
 String toString()
          Converts to a human-readable form.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

URL

public URL(String protocol,
           String host,
           int port,
           String file)
    throws MalformedURLException
Creates an absolute URL from the specified protocol, host, port and file.

Parameters:
protocol - the protocol to use
host - the host to connect to
port - the port at that host to connect to
file - the file on that host
Throws:
MalformedURLException - If an unknown protocol is found.

URL

public URL(String protocol,
           String host,
           String file)
    throws MalformedURLException
Creates an absolute URL from the specified protocol, host, and file. The port number used will be the default for the protocol.

Parameters:
protocol - the protocol to use
host - the host to connect to
file - the file on that host
Throws:
MalformedURLException - If an unknown protocol is found.

URL

public URL(String spec)
    throws MalformedURLException
Creates a URL from the unparsed absolute URL.

Parameters:
spec - the URL String to parse

URL

public URL(URL context,
           String spec)
    throws MalformedURLException
Creates a URL from the unparsed URL in the specified context.If spec is an absolute URL it is used as is. Otherwise it isparsed in terms of the context. Context may be null (indicating no context).

Parameters:
context - the context to parse the URL to
spec - the URL String to parse
Throws:
MalformedURLException - If the protocol is equal to null.
Method Detail

set

protected void set(String protocol,
                   String host,
                   int port,
                   String file,
                   String ref)
Sets the fields of the URL. This is not a public method so that only URLStreamHandlers can modify URL fields. URLs are otherwise constant. REMIND: this method will be moved to URLStreamHandler

Parameters:
protocol - the protocol to use
host - the host name to connecto to
port - the protocol port to connect to
file - the specified file name on that host
ref - the reference

getPort

public int getPort()
Gets the port number. Returns -1 if the port is not set.


getProtocol

public String getProtocol()
Gets the protocol name.


getHost

public String getHost()
Gets the host name.


getFile

public String getFile()
Gets the file name.


getRef

public String getRef()
Gets the ref.


equals

public boolean equals(Object obj)
Compares two URLs.

Overrides:
equals in class Object
Parameters:
obj - the URL to compare against.
Returns:
true if and only if they are equal, false otherwise.
See Also:
Hashtable

hashCode

public int hashCode()
Creates an integer suitable for hash table indexing.

Overrides:
hashCode in class Object
See Also:
Hashtable

sameFile

public boolean sameFile(URL other)
Compares two URLs, excluding the "ref" fields: sameFile is true if the true references the same remote object, but not necessarily the same subpiece of that object.

Parameters:
other - the URL to compare against.
Returns:
true if and only if they are equal, false otherwise.

toString

public String toString()
Converts to a human-readable form.

Overrides:
toString in class Object
Returns:
the textual representation.

toExternalForm

public String toExternalForm()
Reverses the parsing of the URL.

Returns:
the textual representation of the fully qualified URL (i.e. after the context and canonicalization have been applied).

openConnection

public URLConnection openConnection()
                             throws IOException
Creates (if not already in existance) a URLConnection object that contains a connection to the remote object referred to by the URL. Invokes the appropriate protocol handler. Failure is indicated by throwing an exception.

Throws:
IOException - If an I/O exception has occurred.
See Also:
URLConnection

openStream

public final InputStream openStream()
                             throws IOException
Opens an input stream.

Throws:
IOException - If an I/O exception has occurred.

getContent

public final Object getContent()
                        throws IOException
Gets the contents from this opened connection.

Throws:
IOException - If an I/O exception has occurred.

setURLStreamHandlerFactory

public static void setURLStreamHandlerFactory(URLStreamHandlerFactory fac)
Sets the URLStreamHandler factory.

Parameters:
fac - the desired factory
Throws:
Error - If the factory has already been defined.