Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Class URL in Java

All About Class URL in Java

Class URL represents a uniform resource locator-a pointer to a resource on the world wide web such as webpage or FTP directory. A resource can be sometimes as simple as a file or a directory or it can be referenced to a more complicated Object, such as a query to a database or to a search engine.

The class provides methods for retrieving the various parts of a URL and also access to the resource itself.

This class allows the data referred to by the URL to be downloaded. A URL can be a single String or with separated protocol, host and file specifications.

Relative URLs may also be specified with a String and the URL object that it is relative to.

The URL can be broken down into parts like:
protocol://host:port/path?query=#ref
Examples of the protocol can be HTTP, HTTPS, FTP, and File. The path can also be referred to as the filename and the host can be referred to as an authority.

In case the URL does not specify the port, the default port for the corresponding protocol is used. Like HTTP default port is 80.

getFile(),getHost(),getPort(),getProtocol() and getRef() returns the variaous portions of the URL specified by the URL object. sameFile() determines if an URL object refers to the same file as this one.

The data object referred to by an URL may be downloaded from the internet in three ways:

  • Through a URLConnection created with openConnection().
  • Through an InputStream created by openStream().
  • Through getContent() that returns the URL content directly if a ContentHandler is found.

The URL class also provides access to the resource itself, through the getContent(), openConnection(), and openStream() methods. However, these are all convenience functions: other classes do the actual work of accessing the resource.

A protocol handler is an object that knows how to deal with a specific protocol. For example, an http protocol handler opens a connection to an http host. In java.net, subclasses of URLStreamHandler deal with different protocols.

A URLStreamHandlerFactory selects a subclass of URLStreamHandler based on a MIME type. Once the URLStreamHandler has established a connection with a host using a specific protocol, a subclass of ContentHandler retrieves resource data from the host and creates an object from it.

The class structure of the URL is given as:

public final class java.net.URL extends java.lang.Object implements java.io.Serializable{
//constructors
public URL();//creates a URL object from the String representation.This constructor is equivalent to a call to the two argument constructor with null first arguments.Throws MalformedURLException if the string specifies an Unknown protocol.
public URL(String url) throws MalformedURLException;;//creates a URL from the given String.
public URL(String protocol,String host,int port,String file) throws MalformedURLException;//Creates an URL from the specified protocol,host,port number and file. Throws MalformedURLException if the string specifies an Unknown protocol.
public URL(String protocol,String host,String file) throws MalformedURLException;;//Creates an absolute URL from the specified protocol,host and file. Throws MalformedURLException if the string specifies an Unknown protocol.
public URL(URL context,String spec) throws MalformedURLException;;//Creates a URL by parsing the String specification within a specific context.If the context argument is not null and teh spec argument is a particular URL specification,then any of the Strings missing components are inherited from the context argument.throws  MalformedURLException  if no protocol is specified or an unknown protocol is found.
//Methods
public boolean equals(Object obj);
public final Object getContext();//returns the context of the URL.
public final Object getContent() throws IOException;
public String getFile();//Returns the filename of the URL.
public String getPath();//returns the path of the URL.
public String getQuery();//returns the query part of the URL.
public String getAuthority();//returns the authority of the URL.
public String getHost();//returns the host of the URL.
public int getPort();//returns the port of the URL.
public int getDefaultPort();//returns the default port of the protocol of the URL.
public String getProtocol();//returns the protocol of the URL.
public String getRef();//returns the reference part of the URL.
public int hashCode();
public URLConnection openConnection() throws IOException;
//Opens a connection to the URL,allowing a client to communicate with the resource.
public final InputStream openStream()throws IOException;
public boolean sameFile(URL other);
public static synchronized void setURLStreamHandlerFactory(URLStreamHandlerFactory fac);
public String toExternalForm();
public String toString();
protected void set(String protocol,String host,int port,String file,String ref);
} 

The details of the class structure are given as follows:

public URL()

public URL() constructor creates a URL object from the String representation.This constructor is equivalent to a call to the two-argument constructor with null first arguments.Throws MalformedURLException if the string specifies an Unknown protocol.

public URL(String spec);

public URL(String spec) constructor creates a URL by parsing the given string. The string should specify an absolute URL. Calling this constructor is equivalent to calling URL(null, spec).

Parameter
spec – A String that represents a URL.

public URL(URL context, String spec);

public URL(URL context, String spec) constructor creates a URL relative to the base URL specified by the context. If the context is not null, and spec specifies a partial URL, the missing parts of the spec are inherited from context.

The given string is first parsed to see if it specifies a protocol. If the string contains a colon (:) before the first occurrence of a slash (/), the characters before the colon comprise the protocol.

If the spec does not specify a protocol, and context is not null, the protocol is inherited from context, as are the hostname, port number, and filename. If the context is null in this situation, the constructor throws a MalformedURLException.

If the spec does specify a protocol, and context is null or specifies a different protocol, the context argument is ignored and spec should specify an absolute URL. If context specifies the same protocol as the spec, the hostname,port number, and filename from context are inherited.

Once the constructor has created a fully specified URL object, it searches for an appropriate protocol handler of type URLStreamHandler, as described for URL(String, String, int, String). Then the parseURL() method of the URLStreamHandleris called to parse the remainder of the URL so that the fields in spec can override any values inherited from context.

Parameter
context – A base URL that provides the context for parsing spec.
spec – A String that represents a URL.

public URL(String protocol, String host, String file);

public URL(String protocol, String host, String file) constructor creates a URL with the given protocol, hostname, and filename. The port number is set to the default port for the given protocol. Calling this constructor is equivalent to calling URL(protocol, host, -1, file).

Parameter
protocol – A protocol.
host – A hostname.
file – A filename.

public URL(String protocol, String host, int port, String file);

public URL(String protocol, String host, int port, String file) creates a URL with the given protocol, hostname, port number, and filename. If this is the first URL object being created with the specified protocol, a protocol handler of type URLStreamHandler is created for the protocol. Here are the steps that are taken to create a protocol handler:

  • If an application has set up a URLStreamHandlerFactory by calling setURLStreamHandlerFactory(), the constructor calls the createURLStreamHandler() method of that object to create the protocol handler. The protocol is passed as a String argument to that method.
  • If no URLStreamHandlerFactory has been established, or the createURLStreamHandler() method returns null, the constructor retrieves the value of the system property java.protocol.handler.pkgs.If this value is not null, it is interpreted as a list of packages separated by a vertical bar (|) characters. The constructor then tries to load the class named package.protocol.Handler, where the package is the name of the first package in the list and protocol is the name of the protocol. If the class exists, and is a subclass of
    URLStreamHandler, it is used as the URLStreamHandler for the protocol. If the class does not exist, or if it exists but is not a subclass of URLStreamHandler, the constructor tries the next package in the list.
  • If the previous step fails to find an appropriate protocol handler, the constructor tries to load the class named sun.net.www.protocol.protocol.Handler, where the protocol is the name of the protocol. If the class exists and is a subclass of URLStreamHandler, it is used as the URLStreamHandler for the protocol. If the class does not exist, or if it exists but is not a subclass of URLStreamHandler, a MalformedURLException is thrown.

    Parameter
    protocol – A protocol.
    host – A hostname.
    port – A port number or -1 to use the default port for the protocol.
    file – A filename.

    public static synchronized void setURLStreamHandlerFactory(URLStreamHandlerFactory fac);

    public static synchronized void setURLStreamHandlerFactory(URLStreamHandlerFactory fac) method tells the URL class to use the given URLStreamHandlerFactory object for handling all URL objects.

    The purpose of this mechanism is to allow a program that hosts applets, such as a web browser, control over the creation of URLStreamHandler objects.

    Parameter
    fac – An object that implements URLStreamHandlerFactory.

    public boolean equals(Object obj);

    public boolean equals(Object obj) method returns true if obj is an instance of URL with the same protocol, hostname, port number, and filename as this URL. The reference is only compared if it is not null in this URL.

    This method returns true if the objects are equivalent; false if they are not.

    Parameter
    obj – The object to be compared with this object.

    public final Object getContent();

    public final Object getContent() method returns the content of the URL, encapsulated in an object that is appropriate for the type of the content.

    The method is shorthand for calling openConnection().getContent(), which uses a ContentHandler object to retrieve the content.

    This method returns the Object created from the resource represented by this URL.

    public String getFile();

    public String getFile() method returns the name of the file of this URL. Note that the file can be misleading; although the resource represented by this URL may be a file, it can also be generated on the fly by the server.

    This method returns the filename of the URL.

    public String getHost();

    public String getHost() method returns the hostname from this URL.

    This method returns the hostname of the URL.

    public int getPort();

    public int getPort() method returns the port number of this URL. If a port number is not specified for this URL, meaning it uses the default port for the protocol, -1 is returned.

    This method returns the port number of the URL.

    public String getProtocol();

    public String getProtocol() method returns the protocol of this URL. Some examples of protocols are http, ftp, and mailto.

    This method returns the protocol of the URL.

    public String getRef();

    public String getRef() method returns the reference, or anchor, of this URL.

    This method returns the reference to the URL.

    public int hashCode();

    public int hashCode() method returns a hashcode for this object.

    This method returns the hashcode of the URL.

    public URLConnection openConnection();

    public URLConnection openConnection() method returns a URLConnection than manages a connection to the resource represented by this URL. If there is not already an open connection, the method opens a connection by calling the openConnection() method of the
    URLStreamHandler for this URL. A URLStreamHandler for the protocol of the URL is created by the constructor of the URL.

    This method returns a URLConnection object for the URL.

    public final InputStream openStream();

    public final InputStream openStream() method returns an InputStream object that reads the content of the given URL. The method is shorthand for calling  openConnection().getInputStream().

    This method returns an InputStream that reads from this URL.

    public boolean sameFile(URL other);

    public boolean sameFile(URL other) method returns true if this object and the given URL object specify the same protocol, specify hosts that have the same IP address, specify the same port number, and specify the same filename. The filename comparison is case-sensitive. References specified by the URLs are not considered by this method. This method is a helper method for equals().

    This method returns a boolean value that indicates if this URL is equivalent to others with the exception of references.

    Parameter
    other – The URL to compare.

    public String toExternalForm();

    public String toExternalForm() method returns a string representation of this URL. The string representation is determined by the protocol of the URL. The method calls the toExternalForm() method of the URLStreamHandler for this URL. A URLStreamHandler for the protocol of the URL is created by the constructor of the URL.

    This method returns A string representation of the URL.

    public String toString();

    public String toString() method returns a string representation of this URL by calling toExternalForm().

    This method returns a string representation of the URL.

    protected void set(String protocol, String host, int port,String file, String ref);

    protected void set(String protocol, String host, int port, String file, String ref) method sets the protocol, hostname, port number, filename, and reference of this URL. The method is called by a URLStreamHandler to set the parts of the URL. A URLStreamHandler for the protocol of the URL is created by the constructor of the URL. It is this URLStreamHandler that parses the URL string. This method is used after parsing to set the values of the URL.

    Parameter
    protocol – A protocol.
    host – A hostname.
    port – A port number.
    file – A filename.
    ref – A reference.

    Apart from these methods, MNO class also has inherited methods from class- Object. They are as follows:

    • clone()
    • finalize()
    • notifyAll()
    • wait()
    • wait(long, int)
    • getClass()
    • notify()
    • wait(long)

The post Class URL in Java appeared first on Tech Travel Hub.



This post first appeared on Tech Travel Hub, please read the originial post: here

Share the post

Class URL in Java

×

Subscribe to Tech Travel Hub

Get updates delivered right to your inbox!

Thank you for your subscription

×