Abstract Class URLStreamHandler in Java

digitization, transformation, binary

Abstract Class URLStreamHandler in Java

The abstract class URLStreamHandler is the common superclass for all stream protocol handlers. A stream protocol handler knows how to make a connection for a particular protocol type such as FTP, HTTP, gopher, etc.

The URLStreamHandler class is an abstract class that defines methods that encapsulate protocol-specific behavior. A stream handler protocol knows how to establish a connection for a particular protocol and how to parse the protocol-specific portion of a URL. An application does not normally create a URLStreamHandler directly; the appropriate subclass of URLStreamHandler is created by a URLStreamHandlerFactory.

Ads code goes here

The main purpose of a subclass of URLStreamHandler is to create a URLConnection object for a given URL. The URLStreamHandler object creates an object of the appropriate subclass of URLConnection for the protocol type specified by the URL. In order for a URL object to handle a protocol type such as http, ftp, or nntp, it needs an object of the appropriate subclass of URLStreamHandler to handle the protocol-specific details.

This abstract class defines the openConnection() method that creates a URLConnection for a given URL. A separate subclass of this class may be defined for various URL protocol types. A URLStreamHandler is created by a URLStreamHandlerFactory. Normal applications never need to use or subclass this class.

The class structure of URLStreamHandler is given as:

public abstract class java.net.URLStreamHandler extends java.lang.Object{
public URLStreamHandler();//constructor-default constructor
//methods
protected abstract URLConnection OpenConnection(URL url) throws IOException;//opens a connection to the object referenced by the URL argument.it returns an URLConnection object for the URL.it throws IOException if an I/O error occurs while opening the connection.
protected void parseURL(URL url,String spec,int start,int limit);//parses the string representation of a URL into an URL object.Limit-the character position to stop parsing at.This is the end of th string or the position of the # character if present.
protected void setURL(URL url,String protocol,String host,int port,String file,String ref);//sets the fileds of the URL argument to the indicated value.
protected String toExternalForm(URL url);//converts a URL of a specific protocol to a String.
} 

The details of the class structure are given as follows:

protected abstract URLConnection openConnection(URL u)

protected abstract URLConnection openConnection(URL u) method handles the protocol-specific details of establishing a connection to a remote resource specified by the URL. The connection should be handled just up to the point where the resource data can be downloaded. A ContentHandler then takes care of downloading the data and creating an appropriate object. A subclass of URLStreamHandler must implement this method.

READ  ImageObserver Class in Java Simplified

This method returns the URLConnection object for the given URL.

Parameter
u – The URL being connected to.

protected void parseURL(URL u, String spec, int start, int limit);

protected void parseURL(URL u, String spec, int start, int limit) method parses the string representation of a URL into a URL object. Some parts of the URL object may already be specified if spec specifies a relative URL. However, values for those parts in spec can override the inherited context.

The method only parses the protocol-specific portion of the URL. In other words, start should specify the character immediately after the first colon (:), which marks the termination of the protocol type, and limit should either be the last character in the string or the first pound sign (#), which marks the beginning of a protocol-independent anchor. Rather than return a result, the method calls the set() method of the specified URL object to set its fields to the appropriate values.

The implementation of the parseURL() method in URLStreamHandler parses the string representation as if it were an http specification. A subclass that implements a protocol stream handler for a different protocol must override this method to properly parse the URL.

Parameter
u – A reference to a URL object that receives the results of parsing.
spec – The string representation of a URL to be parsed.
start – The offset at which to begin parsing the protocol-specific portion of the URL.
limit – The offset of the last character that is to be parsed.

protected void setURL(URL u, String protocol, String host, int port, String file, String ref);

protected void setURL(URL u, String protocol, String host, int port, String file, String ref) method sets the protocol, hostname, port number, filename, and reference of the given URL to the specified values by calling the set() method of the URL. Only subclasses of URLStreamHandler are allowed to call the set() method of a URL object.

READ  Class Date in Java

Parameter
u – A reference to a URL object to be modified.
protocol – A protocol.
host – A hostname.
port – A port number.
file – A filename.
ref – A reference.

protected String toExternalForm(URL u);

protected String toExternalForm(URL u) method unparses a URL object and returns a string representation of the URL.

The implementation of the toExternalForm() method in URLStreamHandler returns a string representation that is appropriate for an http specification. A subclass that implements a protocol stream handler for a different protocol must override this method to create a correct string representation.

This method returns a string representation of the given URL.

Parameter
u – The URL object to convert to a string representation.

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

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *