Build 1.0_r1(from source)

java.net
Class URL

java.lang.Object
  extended by java.net.URL
All Implemented Interfaces:
Serializable

public final class URL
extends Object
implements Serializable

An instance of class URL specifies the location of a resource on the world wide web as specified by RFC 1738.

See Also:
Serialized Form

Field Summary
(package private)  URLStreamHandler strmHandler
          The URL Stream (protocol) Handler
 
Constructor Summary
URL(String spec)
          Constructs a new URL instance by parsing the specification.
URL(String protocol, String host, int port, String file)
          Constructs a new URL instance using the arguments provided.
URL(String protocol, String host, int port, String file, URLStreamHandler handler)
          Constructs a new URL instance using the arguments provided.
URL(String protocol, String host, String file)
          Constructs a new URL instance using the arguments provided.
URL(URL context, String spec)
          Constructs a new URL by parsing the specification given by spec and using the context provided by context.
URL(URL context, String spec, URLStreamHandler handler)
          Constructs a new URL by parsing the specification given by spec and using the context provided by context.
 
Method Summary
 boolean equals(Object o)
          Compares the argument to the receiver, and returns true if they represent the same URL.
(package private)  void fixURL(boolean fixHost)
           
 String getAuthority()
          Returns the authority component of this URL.
 Object getContent()
          Returns an Object representing the resource referenced by this URL.
 Object getContent(Class[] types)
          Returns an Object representing the resource referenced by this URL.
 int getDefaultPort()
          Returns the default port for this URL as defined by the URLStreamHandler.
 String getFile()
          Returns the file component of this URL.
 String getHost()
          Returns the host component of this URL.
 String getPath()
          Returns the path component of this URL.
 int getPort()
          Returns the port component of this URL.
 String getProtocol()
          Returns the protocol component of this URL.
 String getQuery()
          Returns the query component of this URL.
 String getRef()
          Returns the reference component of this URL.
(package private)  URLStreamHandler getStreamHandler()
           
 String getUserInfo()
          Returns the user info component of this URL.
 int hashCode()
          Returns a hash code for this URL object.
 URLConnection openConnection()
          Creates a connection to this URL using the appropriate ProtocolHandler.
 URLConnection openConnection(Proxy proxy)
          The method is the same as openConnection() except that it uses the proxy to establish a connection to this URL using appropriate ProtocolHandler.
 InputStream openStream()
          Returns a stream for reading from this URL.
 boolean sameFile(URL otherURL)
          Returns true if the receiver and the argument refer to the same file.
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.
(package private)  void setupStreamHandler()
          Sets the receiver's stream handler to one which is appropriate for its protocol.
static void setURLStreamHandlerFactory(URLStreamHandlerFactory streamFactory)
          Sets the URL Stream (protocol) handler factory.
 String toExternalForm()
          Create and return the String representation of this URL.
 String toString()
          Returns a string containing a concise, human-readable description of the receiver.
 URI toURI()
          Creates a URI related with this URL
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

strmHandler

transient URLStreamHandler strmHandler
The URL Stream (protocol) Handler

Constructor Detail

URL

public URL(String spec)
    throws MalformedURLException
Constructs a new URL instance by parsing the specification.

Parameters:
spec - java.lang.String a URL specification.
Throws:
MalformedURLException - if the spec could not be parsed as an URL.

URL

public URL(URL context,
           String spec)
    throws MalformedURLException
Constructs a new URL by parsing the specification given by spec and using the context provided by context.

The protocol of the specification is obtained by parsing the spec string.

If the spec does not specify a protocol:

If the spec does specify a protocol:

Parameters:
context - java.net.URL URL to use as context.
spec - java.lang.String a URL specification.
Throws:
MalformedURLException - if the spec could not be parsed as an URL.

URL

public URL(URL context,
           String spec,
           URLStreamHandler handler)
    throws MalformedURLException
Constructs a new URL by parsing the specification given by spec and using the context provided by context.

If the handler argument is non-null, a security check is made to verify that user-defined protocol handlers can be specified.

The protocol of the specification is obtained by parsing the spec string.

If the spec does not specify a protocol:

If the spec does specify a protocol:

Parameters:
context - java.net.URL URL to use as context.
spec - java.lang.String a URL specification.
handler - java.net.URLStreamHandler a URLStreamHandler.
Throws:
MalformedURLException - if the spec could not be parsed as an URL

URL

public URL(String protocol,
           String host,
           String file)
    throws MalformedURLException
Constructs a new URL instance using the arguments provided.

Parameters:
protocol - String the protocol for the URL.
host - String the name of the host.
file - the name of the resource.
Throws:
MalformedURLException - if the parameters do not represent a valid URL.

URL

public URL(String protocol,
           String host,
           int port,
           String file)
    throws MalformedURLException
Constructs a new URL instance using the arguments provided.

Parameters:
protocol - String the protocol for the URL.
host - String the name of the host.
port - int the port number.
file - String the name of the resource.
Throws:
MalformedURLException - if the parameters do not represent a valid URL.

URL

public URL(String protocol,
           String host,
           int port,
           String file,
           URLStreamHandler handler)
    throws MalformedURLException
Constructs a new URL instance using the arguments provided.

If the handler argument is non-null, a security check is made to verify that user-defined protocol handlers can be specified.

Parameters:
protocol - the protocol for the URL.
host - the name of the host.
port - the port number.
file - the name of the resource.
handler - the stream handler that this URL uses.
Throws:
MalformedURLException - if the parameters do not represent an URL.
Method Detail

setURLStreamHandlerFactory

public static void setURLStreamHandlerFactory(URLStreamHandlerFactory streamFactory)
Sets the URL Stream (protocol) handler factory. This method can be invoked only once during an application's lifetime.

A security check is performed to verify that the current Policy allows the stream handler factory to be set.

Parameters:
streamFactory - URLStreamHandlerFactory The factory to use for finding stream handlers.

fixURL

void fixURL(boolean fixHost)

set

protected void set(String protocol,
                   String host,
                   int port,
                   String file,
                   String ref)
Sets the properties of this URL using the provided arguments. This method is used both within this class and by the URLStreamHandler code.

Parameters:
protocol - the new protocol.
host - the new host name.
port - the new port number.
file - the new file component.
ref - the new reference.
See Also:
URL, URLStreamHandler

equals

public boolean equals(Object o)
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.

Overrides:
equals in class Object
Parameters:
o - the object to compare with this URL.
Returns:
true if the object is the same as this URL, false otherwise.
See Also:
hashCode

sameFile

public boolean sameFile(URL otherURL)
Returns true if the receiver and the argument refer to the same file. All components except the reference are compared.

Parameters:
otherURL - URL to compare against.
Returns:
true if the same resource, false otherwise

hashCode

public int hashCode()
Returns a hash code for this URL object.

Overrides:
hashCode in class Object
Returns:
the hashcode for hashtable indexing
See Also:
Object.equals(java.lang.Object)

setupStreamHandler

void setupStreamHandler()
Sets the receiver's stream handler to one which is appropriate for its protocol. Throws a MalformedURLException if no reasonable handler is available.

Note that this will overwrite any existing stream handler with the new one. Senders must check if the strmHandler is null before calling the method if they do not want this behavior (a speed optimization).


getContent

public final Object getContent()
                        throws IOException
Returns an Object representing the resource referenced by this URL.

Returns:
The object of the resource pointed by this URL.
Throws:
IOException - If an error occurred obtaining the content.

getContent

public final Object getContent(Class[] types)
                        throws IOException
Returns an Object representing the resource referenced by this URL.

Parameters:
types - The list of acceptable content types
Returns:
The object of the resource pointed by this URL, or null if the content does not match a specified content type.
Throws:
IOException - If an error occurred obtaining the content.

openStream

public final InputStream openStream()
                             throws IOException
Returns a stream for reading from this URL.

Returns:
a stream on the contents of the resource.
Throws:
IOException - if a stream could not be created.

openConnection

public URLConnection openConnection()
                             throws IOException
Creates a connection to this URL using the appropriate ProtocolHandler.

Returns:
The connection to this URL.
Throws:
IOException - if the connection to the URL is not possible.

toURI

public URI toURI()
          throws URISyntaxException
Creates a URI related with this URL

Returns:
a URI related to this URL
Throws:
URISyntaxException - if this URL cannot format into URI

openConnection

public URLConnection openConnection(Proxy proxy)
                             throws IOException
The method is the same as openConnection() except that it uses the proxy to establish a connection to this URL using appropriate ProtocolHandler.

Parameters:
proxy - the proxy which is used to make the connection
Returns:
The connection to this URL.
Throws:
IOException - thrown if an IO error occurs during connection establishment
SecurityException - thrown if a security manager is installed and it denies the permission to connect to the proxy.
IllegalArgumentException - thrown if the proxy is null or of an invalid type.
UnsupportedOperationException - thrown if the protocol handler doesn't support this method.

toString

public String toString()
Returns a string containing a concise, human-readable description of the receiver.

Overrides:
toString in class Object
Returns:
a printable representation for the receiver.

toExternalForm

public String toExternalForm()
Create and return the String representation of this URL.

Returns:
the external representation of this URL.
See Also:
toString(), URL, URLStreamHandler.toExternalForm(URL)

getFile

public String getFile()
Returns the file component of this URL.

Returns:
the receiver's file.

getHost

public String getHost()
Returns the host component of this URL.

Returns:
the receiver's host.

getPort

public int getPort()
Returns the port component of this URL.

Returns:
the receiver's port.

getProtocol

public String getProtocol()
Returns the protocol component of this URL.

Returns:
the receiver's protocol.

getRef

public String getRef()
Returns the reference component of this URL.

Returns:
the receiver's reference component.

getQuery

public String getQuery()
Returns the query component of this URL.

Returns:
the receiver's query.

getPath

public String getPath()
Returns the path component of this URL.

Returns:
the receiver's path.

getUserInfo

public String getUserInfo()
Returns the user info component of this URL.

Returns:
the receiver's user info.

getAuthority

public String getAuthority()
Returns the authority component of this URL.

Returns:
the receiver's authority.

set

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. This method is used both within this class and by the URLStreamHandler code.

Parameters:
protocol - the new protocol.
host - the new host name.
port - the new port number.
authority - the new authority.
userInfo - the new user info.
path - the new path component.
query - the new query.
ref - the new reference.
See Also:
URL, URLStreamHandler

getStreamHandler

URLStreamHandler getStreamHandler()

getDefaultPort

public int getDefaultPort()
Returns the default port for this URL as defined by the URLStreamHandler.

Returns:
the default port for this URL
See Also:
URLStreamHandler.getDefaultPort()

Build 1.0_r1(from source)

Please submit a feedback, bug or feature