Save This Page
Home » apache-harmony-6.0-src-r917296-snapshot » java » net » [javadoc | source]
java.net
public final class: URL [javadoc | source]
java.lang.Object
   java.net.URL

All Implemented Interfaces:
    Serializable

A URL instance specifies the location of a resource on the internet as specified by RFC 1738. Such a resource can be a simple file or a service which generates the output dynamically. A URL is divided in its parts protocol, host name, port, path, file, user-info, query, reference and authority. However, not each of this parts has to be defined.
Field Summary
transient  URLStreamHandler strmHandler    The URL Stream (protocol) Handler 
Constructor:
 public URL(String spec) throws MalformedURLException 
    Creates a new URL instance by parsing the string {@code spec}.
    Parameters:
    spec - the URL string representation which has to be parsed.
    Throws:
    MalformedURLException - if the given string {@code spec} could not be parsed as a URL.
 public URL(URL context,
    String spec) throws MalformedURLException 
    Creates a new URL to the specified resource {@code spec}. This URL is relative to the given {@code 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 {@code spec}. Otherwise the protocol is defined by the context URL.
    Parameters:
    context - the URL which is used as the context.
    spec - the URL string representation which has to be parsed.
    Throws:
    MalformedURLException - if the given string {@code 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 
    Creates a new URL to the specified resource {@code spec}. This URL is relative to the given {@code context}. The {@code handler} will be used to parse the URL string representation. If this argument is {@code null} the default {@code 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 {@code spec}. Otherwise the protocol is defined by the context URL.
    Parameters:
    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.
    Throws:
    MalformedURLException - if the given string {@code 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 
    Creates a new URL instance using the given arguments. The URL uses the default port for the specified protocol.
    Parameters:
    protocol - the protocol of the new URL.
    host - the host name or IP address of the new URL.
    file - the name of the resource.
    Throws:
    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 
    Creates a new URL instance using the given arguments. The URL uses the specified port instead of the default port for the given protocol.
    Parameters:
    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. {@code -1} represents the default port of the protocol.
    file - the name of the resource.
    Throws:
    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 
    Creates a new URL instance using the given arguments. The URL uses the specified port instead of the default port for the given protocol.
    Parameters:
    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. {@code -1} represents the default port of the protocol.
    file - the name of the resource.
    handler - the stream handler to be used by this URL.
    Throws:
    MalformedURLException - if the combination of all arguments do not represent a valid URL or the protocol is invalid.
    SecurityException - if {@code handler} is non-{@code null}, and a security manager is installed that disallows user-defined protocol handlers.
Method from java.net.URL Summary:
equals,   fixURL,   getAuthority,   getContent,   getContent,   getDefaultPort,   getFile,   getHost,   getPath,   getPort,   getProtocol,   getQuery,   getRef,   getUserInfo,   hashCode,   openConnection,   openConnection,   openStream,   sameFile,   set,   set,   setURLStreamHandlerFactory,   setupStreamHandler,   toExternalForm,   toString,   toURI
Methods from java.lang.Object:
clone,   equals,   finalize,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from java.net.URL Detail:
 public boolean equals(Object o) 
    Compares this URL instance with the given argument {@code 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.
  void fixURL(boolean fixHost) 
 public String getAuthority() 
    Gets the value of the authority part of this URL.
 public final Object getContent() throws IOException 
    Gets the content of the resource which is referred by this URL. By default one of the following object types will be returned:

  • Image for pictures
  • AudioClip for audio sequences
  • InputStream for all other data
 public final Object getContent(Class[] types) throws IOException 
    Gets the content of the resource which is referred by this URL. The argument {@code types} is an array of allowed or expected object types. {@code 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.
 public int getDefaultPort() 
    Gets the default port number of the protocol used by this URL. If no default port is defined by the protocol or the {@code URLStreamHandler}, {@code -1} will be returned.
 public String getFile() 
    Gets the value of the file part of this URL.
 public String getHost() 
    Gets the value of the host part of this URL.
 public String getPath() 
    Gets the value of the path part of this URL.
 public int getPort() 
    Gets the port number of this URL or {@code -1} if the port is not set.
 public String getProtocol() 
    Gets the protocol of this URL.
 public String getQuery() 
    Gets the value of the query part of this URL.
 public String getRef() 
    Gets the value of the reference part of this URL.
 public String getUserInfo() 
    Gets the value of the user-info part of this URL.
 public int hashCode() 
    Gets the hashcode value of this URL instance.
 public URLConnection openConnection() throws IOException 
    Opens a connection to the remote resource specified by this URL. This connection allows bidirectional data transfer.
 public URLConnection openConnection(Proxy proxy) throws IOException 
    Opens a connection to the remote resource specified by this URL. The connection will be established through the given proxy and allows bidirectional data transfer.
 public final InputStream openStream() throws IOException 
    Opens an InputStream to read the resource referred by this URL.
 public boolean sameFile(URL otherURL) 
    Returns whether this URL refers to the same resource as the given argument {@code otherURL}. All URL components except the reference field are compared.
 protected  void set(String protocol,
    String host,
    int port,
    String file,
    String ref) 
    Sets the properties of this URL using the provided arguments. Only a {@code URLStreamHandler} can use this method to set fields of the existing URL instance. A URL is generally constant.
 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. Only a {@code URLStreamHandler} can use this method to set fields of the existing URL instance. A URL is generally constant.
 public static synchronized  void setURLStreamHandlerFactory(URLStreamHandlerFactory streamFactory) 
    Sets the {@code URLStreamHandlerFactory} which creates protocol specific stream handlers. This method can be invoked only once during an application's lifetime. If the {@code 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.

  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).

 public String toExternalForm() 
    Returns a string containing a concise, human-readable representation of this URL.
 public String toString() 
    Returns a string containing a concise, human-readable representation of this URL. The returned string is the same as the result of the method {@code toExternalForm()}.
 public URI toURI() throws URISyntaxException 
    Converts this URL instance into an equivalent URI object.