Java Reference
In-Depth Information
Parsing Methods
These seven getter methods are the main purpose for this class. They return individual
pieces of the URL:
public int getPort ()
public String getProtocol ()
public String getFile ()
public String getRef ()
public String getHost ()
public String getUsername ()
public String getPassword ()
These methods can all be easily understood by analogy with the similarly named meth‐
ods in java.net.URL . Except for getPort() , these methods all return null if the piece
is missing. getPort() returns -1 if the port is not explicitly included in the URL.
There's also a getURL() method that converts a URLName to a java.net.URL . Since doing
so requires that Java have a protocol handler for the URL's scheme, this method can
throw a MalformedURLException :
public URL getURL () throws MalformedURLException
Finally, there are the usual three utility methods with the usual semantics:
public boolean equals ( Object o )
public int hashCode ()
public String toString ()
The toString() method simply returns the string form of the URL. The equals()
method is underspecified but in practice any two URLName objects that are character-
by-character identical will compare equal. However, JavaMail does not consider case to
be significant in domain names. http://www.example.com and http://WWW.EXAM
PLE.COM are equal. Surprisingly, it does consider case to be significant in URL schemes.
That is, http://www.example.com is not equal to HTTP://www.example.com . Finally,
JavaMail recognizes / as the default path; for example, http://www.example.com is
equal to http://www.example.com/ . The hashCode() method is implemented accord‐
ingly.
We can use the URLName class to provide an interface for an email client that is completely
protocol-independent. All information about protocol, host, and other details is pro‐
vided by a URL read from the command line. Example 6-1 demonstrates.
Example 6-1. A protocol-independent mail client
import javax.mail.* ;
import java.io.IOException ;
import java.util.* ;
public class MailClient {
Search WWH ::




Custom Search