The URLStreamHandler Class
The abstract
URLStreamHandler
class is a superclass for classes
that handle specific protocols—for example, HTTP. You rarely
call the methods of the URLStreamHandler
class
directly; they are called by other methods in the
URL
and URLConnection
classes.
By overriding the URLStreamHandler
methods in your
own subclass, you teach the URL
class how to
handle new protocols. Therefore, we’ll focus on overriding the
methods of URLStreamHandler
rather than on calling
the methods.
The Constructor
You do not create URLStreamHandler
objects
directly. Instead, when a URL is constructed with a protocol that
hasn’t been seen before, Java asks the application’s
URLStreamHandlerFactory
to create the appropriate
URLStreamHandler
subclass for the protocol. If
that fails, Java guesses at the fully package-qualified name of the
URLStreamHandler
class and uses
Class.forName( )
to attempt to construct such an
object. This means concrete subclasses should have a noargs
constructor. The single constructor for
URLStreamHandler
doesn’t take any arguments:
public URLStreamHandler( )
Because URLStreamHandler
is an abstract class,
this constructor is never called directly; it is only called from the
constructors of subclasses.
Methods for Parsing URLs
The first
responsibility of a URLStreamHandler
is to split a
string representation of a URL
into its component
parts and use those parts to set the various fields of the
URL
object. The parseURL( )
method splits the URL into parts, possibly ...
Get Java Network Programming, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.