The URLConnection
class is intimately tied to Java’s protocol and content handler
mechanism. The protocol handler is responsible for making
connections, exchanging headers, requesting particular documents, and
so forth. It handles all the overhead of the protocol for requesting
files. The content handler deals only with the actual data. It takes
the raw input after all headers and so forth are stripped and
converts it to the right kind of object for Java to deal with; for
instance, an InputStream
or an
ImageProducer
.
The getContent( )
methods of
URLConnection
use a content handler to turn the
raw data of a connection into a Java object.
This method is virtually identical to the getContent( )
method of the URL
class. In fact, that
method just calls this method. getContent( )
downloads the object selected by the URL
of this
URLConnection
. For getContent( )
to work, the environment needs to recognize and
understand the content type. The only content types that are
supported in the JDK are text/plain, image/gif, and image/jpeg. Other
VMs and applications may support additional types. For instance,
HotJava 3.0 includes a PDF content handler. Furthermore, you can
install additional content handlers that understand other content
types.
getContent( )
works only for protocols like HTTP
that have a clear understanding of MIME content types. If the content
type is unknown, or the protocol doesn’t understand content
types, getContent( )
throws an
UnknownServiceException
.
Java 1.3 lets a content handler provide different object
representations of data. This overloaded variant of the
getContent( )
method lets you choose what class
you’d like the content returned as. The method will attempt to
return the content in the form of one of the classes in the
classes
array. The order of preference is the
order of the array. For instance, if you’d prefer an HTML file
to be returned as a String
, but your second choice
is a Reader
and your third choice is an
InputStream
, you would write:
URL u = new URL("http://www.thehungersite.com/"); URLConnection uc = u.openConnection( ) Class[] types = {String.class, Reader.class, InputStream.class}; Object o = uc.getContent(types);
You would then have to test for the type of the returned object using
instanceof
. For example:
if (o instanceof String) { System.out.println(o); } else if (o instanceof Reader) { int c; Reader r = (Reader) o; while ((c = r.read( )) != -1) System.out.print((char) c); } else if (o instanceof InputStream) { int c; InputStream in = (InputStream) o; while ((c = in.read( )) != -1) System.out.write(c); } else if (o == null) { System.out.println("None of the requested types were available."); } else { System.out.println("Error: unexpected type " + o.getClass( )); }
That last else
clause isn’t supposed to be
reached. If none of the requested types are available, this method is
supposed to return null
rather than returning an
unexpected type.
The URLConnection
class contains a static
Hashtable
of ContentHandler
objects. Whenever the getContent( )
method of
URLConnection
is invoked, Java looks in this
Hashtable
to find the right content handler for
the current URL, as indicated by the URL’s Content-type. If it
doesn’t find a ContentHandler
object for the
MIME type, then it tries to create one using a
ContentHandlerFactory
, which you’ll learn
more about in Chapter 17. That is, a content
handler factory tells the program where it can find a content handler
for a text/html
file, an
image/gif
file, or some other kind of file. You
can set the ContentHandlerFactory
by passing an
instance of the java.net.ContentHandlerFactory
interface to the setContentHandlerFactory( )
method:
public static synchronized void setContentHandlerFactory( ContentHandlerFactory factory) throws SecurityException, Error
You may set the
ContentHandlerFactory
only once per application;
this method throws a generic Error
if it is called
a second time. As with most other setFactory( )
methods, untrusted applets will generally not be allowed to set the
content handler factory whether one has already been set or not.
Attempting to do so will throw a
SecurityException
.
3.144.103.10