url.java
来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 719 行 · 第 1/2 页
JAVA
719 行
i = spec.indexOf('#', start);
if (i >= 0) {
ref = spec.substring(i + 1, limit);
limit = i;
}
handler.parseURL(this, spec, start, limit);
} catch(MalformedURLException e) {
throw e;
} catch(Exception e) {
throw new MalformedURLException(original + ": " + e);
}
}
/**
* Sets the fields of the URL. This is not a public method so that
* only URLStreamHandlers can modify URL fields. URLs are
* otherwise constant.
*
* REMIND: this method will be moved to URLStreamHandler
*
* @param protocol the protocol to use
* @param host the host name to connecto to
* @param port the protocol port to connect to
* @param file the specified file name on that host
* @param ref the reference
*/
protected void set(String protocol, String host, int port, String file, String ref) {
this.protocol = protocol;
this.host = host;
this.port = port;
this.file = file;
this.ref = ref;
}
/**
* Returns the port number of this <code>URL</code>.
* Returns -1 if the port is not set.
*
* @return the port number
* @since JDK1.0
*/
public int getPort() {
return port;
}
/**
* Returns the protocol name this <code>URL</code>.
*
* @return the protocol of this <code>URL</code>.
* @since JDK1.0
*/
public String getProtocol() {
return protocol;
}
/**
* Returns the host name of this <code>URL</code>, if applicable.
* For "<code>file</code>" protocol, this is an empty string.
*
* @return the host name of this <code>URL</code>.
* @since JDK1.0
*/
public String getHost() {
return host;
}
/**
* Returns the file name of this <code>URL</code>.
*
* @return the file name of this <code>URL</code>.
* @since JDK1.0
*/
public String getFile() {
return file;
}
/**
* Returns the anchor (also known as the "reference") of this
* <code>URL</code>.
*
* @return the anchor (also known as the "reference") of this
* <code>URL</code>.
* @since JDK1.0
*/
public String getRef() {
return ref;
}
/**
* Compares two URLs.
* The result is <code>true</code> if and only if the argument is
* not <code>null</code> and is a <code>URL</code> object that
* represents the same <code>URL</code> as this object. Two URL
* objects are equal if they have the same protocol and reference the
* same host, the same port number on the host, and the same file on
* the host. The anchors of the URL objects are not compared.
* <p>
* This method is equivalent to:
* <ul><code>
* (obj instanceof URL) && sameFile((URL)obj)
* </code></ul>
*
* @param obj the URL to compare against.
* @return <code>true</code> if the objects are the same;
* <code>false</code> otherwise.
* @since JDK1.0
*/
public boolean equals(Object obj) {
return (ref == null) ? (obj instanceof URL) && sameFile((URL)obj):
(obj instanceof URL) && sameFile((URL)obj) && ref.equals(((URL)obj).ref);
}
/**
* Creates an integer suitable for hash table indexing.
*
* @return a hash code for this <code>URL</code>.
* @since JDK1.0
*/
public int hashCode() {
if (hashCode == -1) {
hashCode = host.toLowerCase().hashCode() ^ file.hashCode() ^
protocol.hashCode();
}
return hashCode;
}
/**
* Compares the host components of two URLs.
* @param h1 the URL of the first host to compare
* @param h2 the URL of the second host to compare
* @return true if and only if they are equal, false otherwise.
* @exception UnknownHostException If an unknown host is found.
*/
boolean hostsEqual(String h1, String h2) {
if (h1.equals(h2)) {
return true;
}
// Have to resolve addresses before comparing, otherwise
// names like tachyon and tachyon.eng would compare different
try {
InetAddress a1 = InetAddress.getByName(h1);
InetAddress a2 = InetAddress.getByName(h2);
return a1.equals(a2);
} catch(UnknownHostException e) {
} catch(SecurityException e) {
}
return false;
}
/**
* Compares two URLs, excluding the "ref" fields.
* Returns <code>true</code> if this <code>URL</code> and the
* <code>other</code> argument both refer to the same resource.
* The two <code>URL</code>s might not both contain the same anchor.
*
* @param other the <code>URL</code> to compare against.
* @return <code>true</code> if they reference the same remote object;
* <code>false</code> otherwise.
* @since JDK1.0
*/
public boolean sameFile(URL other) {
// AVH: should we not user getPort to compare ports?
return protocol.equals(other.protocol) &&
hostsEqual(host, other.host) &&
(port == other.port) &&
file.equals(other.file);
}
/**
* Constructs a string representation of this <code>URL</code>. The
* string is created by calling the <code>toExternalForm</code>
* method of the stream protocol handler for this object.
*
* @return a string representation of this object.
* @see java.net.URL#URL(java.lang.String, java.lang.String, int, java.lang.String)
* @see java.net.URLStreamHandler#toExternalForm(java.net.URL)
* @since JDK1.0
*/
public String toString() {
return toExternalForm();
}
/**
* Constructs a string representation of this <code>URL</code>. The
* string is created by calling the <code>toExternalForm</code>
* method of the stream protocol handler for this object.
*
* @return a string representation of this object.
* @see java.net.URL#URL(java.lang.String, java.lang.String, int, java.lang.String)
* @see java.net.URLStreamHandler#toExternalForm(java.net.URL)
* @since JDK1.0
*/
public String toExternalForm() {
return handler.toExternalForm(this);
}
/**
* Returns a <code>URLConnection</code> object that represents a
* connection to the remote object referred to by the <code>URL</code>.
* <p>
* If there is not already an open connection, the connection is
* opened by calling the <code>openConnection</code> method of the
* protocol handler for this URL.
*
* @return a <code>URLConnection</code> to the URL.
* @exception IOException if an I/O exception occurs.
* @see java.net.URL#URL(java.lang.String, java.lang.String, int, java.lang.String)
* @see java.net.URLConnection
* @see java.net.URLStreamHandler#openConnection(java.net.URL)
* @since JDK1.0
*/
public URLConnection openConnection()
throws java.io.IOException
{
return handler.openConnection(this);
}
/**
* Opens a connection to this <code>URL</code> and returns an
* <code>InputStream</code> for reading from that connection. This
* method is a shorthand for:
* <ul><code>
* openConnection().getInputStream()
* </code></ul>
*
* @return an input stream for reading from the URL connection.
* @exception IOException if an I/O exception occurs.
* @since JDK1.0
*/
public final InputStream openStream() // REMIND: drop final
throws java.io.IOException
{
return openConnection().getInputStream();
}
/**
* Returns the contents of this URL. This method is a shorthand for:
* <ul><code>
* openConnection().getContent()
* </code></ul>
*
* @return the contents of this URL.
* @exception IOException if an I/O exception occurs.
* @see java.net.URLConnection#getContent()
* @since JDK1.0
*/
public final Object getContent() // REMIND: drop final
throws java.io.IOException
{
return openConnection().getContent();
}
/**
* The URLStreamHandler factory.
*/
static URLStreamHandlerFactory factory;
/**
* Sets an application's <code>URLStreamHandlerFactory</code>.
* This method can be called at most once by an application.
* <p>
* The <code>URLStreamHandlerFactory</code> instance is used to
* construct a stream protocol handler from a protocol name.
*
* @param fac the desired factory.
* @exception Error if the application has already set a factory.
* @see java.net.URL#URL(java.lang.String, java.lang.String, int, java.lang.String)
* @see java.net.URLStreamHandlerFactory
* @since JDK1.0
*/
public static synchronized void setURLStreamHandlerFactory(URLStreamHandlerFactory fac) {
if (factory != null) {
throw new Error("factory already defined");
}
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkSetFactory();
}
handlers.clear();
factory = fac;
}
/**
* A table of protocol handlers.
*/
static Hashtable handlers = new Hashtable();
/**
* Returns the Stream Handler.
* @param protocol the protocol to use
*/
static synchronized URLStreamHandler getURLStreamHandler(String protocol) {
URLStreamHandler handler = (URLStreamHandler)handlers.get(protocol);
if (handler == null) {
// Use the factory (if any)
if (factory != null) {
handler = factory.createURLStreamHandler(protocol);
}
// Try java protocol handler
if (handler == null) {
String packagePrefixList =
System.getProperty(protocolPathProp, "");
if (packagePrefixList != "") {
packagePrefixList += "|";
}
// REMIND: decide whether to allow the "null" class prefix
// or not.
packagePrefixList += "sun.net.www.protocol";
StringTokenizer packagePrefixIter =
new StringTokenizer(packagePrefixList, "|");
while (handler == null && packagePrefixIter.hasMoreTokens()) {
String packagePrefix = packagePrefixIter.nextToken().trim();
try {
String clname = packagePrefix + "." + protocol
+ ".Handler";
handler = (URLStreamHandler)Class.forName(clname).newInstance();
} catch (Exception e) {
}
}
}
if (handler != null) {
handlers.put(protocol, handler);
}
}
return handler;
}
/**
* WriteObject is called to save the state of the URL to an ObjectOutputStream
* The handler is not saved since it is specific to this system.
*/
private synchronized void writeObject(java.io.ObjectOutputStream s)
throws IOException
{
s.defaultWriteObject(); // write the fields
}
/**
* readObject is called to restore the state of the URL from the stream.
* It reads the compoents of the URL and finds the local stream handler.
*/
private synchronized void readObject(java.io.ObjectInputStream s)
throws IOException, ClassNotFoundException
{
s.defaultReadObject(); // read the fields
if ((handler = getURLStreamHandler(protocol)) == null) {
throw new IOException("unknown protocol: " + protocol);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?