digitization, transformation, binary

Class ContentHandler in Java

Class ContentHandler in Java

ContentHandler is an abstract class and is the superclass of all classes that read an Object from a URL connection and then create an Object appropriate for the type of content it has read. Each subclass of ContentHandler handles a specific type of content (i.e., MIME type).

An application does not generally call the getContentHandler() method in this class directly. Instead,an application calls getContent() method in class URL or in URLConnection.The application’s content handler factory (an instance of a class that implements the interface ContentHandlerFactory) sets up by a call to setContentHandler and calls with a String giving the MIME type of the object being received on the socket.

The factory returns an instance of a subclass of ContentHandler and its getContent() method is called to create the object.

A ContentHandler works in conjunction with a URLStreamHandler, but their roles do not overlap. The URLStreamHandler deals with the specifics of a protocol, such as negotiating with a server to retrieve a resource, while the ContentHandler expects a data stream from which it can construct an object.

The structure of the class ContentHandler is given as:


public abstract class java.net.ContentHandler extends java.lang.Object{
//constructors:
public ContentHandler();//The default constructor for ContentHandler class,creates an object of ContentHandler.
//Methods:
public abstract Object getContent(URLConnection conn);//given an URL connect stream positioned at the beginning of the representation of an Object,this method reads the stream and creates an Object from it. Returns the Object read by ContentHandler.It can throw IOException if an IO error occurs while reading the Object.
}
 

The details of the class structure are given as follows:

public ContentHandler();

public ContentHandler() is the default constructor for ContentHandler class,creates an object of ContentHandler.

public abstract Object getContent(URLConnection conn);

public abstract Object getContent(URLConnection conn) method reads data from the given URLConnection and returns the object that is represented by the data.

This method returns the Object created from the data source.

Parameter
conn – A URLConnection that is the data source.

Apart from these ContentHandler 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 *