urlstreamhandler.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 439 行 · 第 1/2 页

JAVA
439
字号
	 */
	protected boolean sameFile(URL url1, URL url2) {
		if (url1 == url2)
			return true;
		// This comparison is very conservative.  It assumes that any
		// field can be null.
		if (url1 == null || url2 == null || url1.getPort() != url2.getPort())
			return false;
		String s1, s2;
		s1 = url1.getProtocol();
		s2 = url2.getProtocol();
		if (s1 != s2 && (s1 == null || !s1.equals(s2)))
			return false;
		s1 = url1.getHost();
		s2 = url2.getHost();
		if (s1 != s2 && (s1 == null || !s1.equals(s2)))
			return false;
		s1 = canonicalizeFilename(url1.getFile());
		s2 = canonicalizeFilename(url2.getFile());
		if (s1 != s2 && (s1 == null || !s1.equals(s2)))
			return false;
		return true;
	}

	/**
	 * This methods sets the instance variables representing the various fields
	 * of the URL to the values passed in.
	 *
	 * @param u The URL to modify
	 * @param protocol The protocol to set
	 * @param host The host name to et
	 * @param port The port number to set
	 * @param file The filename to set
	 * @param ref The reference
	 *
	 * @exception SecurityException If the protocol handler of the URL is
	 * different from this one
	 *
	 * @deprecated 1.2 Please use
	 * #setURL(URL,String,String,int,String,String,String,String);
	 */
	protected void setURL(
		URL u,
		String protocol,
		String host,
		int port,
		String file,
		String ref) {
		u.set(protocol, host, port, file, ref);
	}

	/**
	 * Sets the fields of the URL argument to the indicated values
	 *
	 * @param u The URL to modify
	 * @param protocol The protocol to set
	 * @param host The host name to set
	 * @param port The port number to set
	 * @param authority The authority to set
	 * @param userInfo The user information to set
	 * @param path The path/filename to set
	 * @param query The query part to set
	 * @param ref The reference
	 *
	 * @exception SecurityException If the protocol handler of the URL is
	 * different from this one
	 */
	protected void setURL(
		URL u,
		String protocol,
		String host,
		int port,
		String authority,
		String userInfo,
		String path,
		String query,
		String ref) {
		u.set(protocol, host, port, authority, userInfo, path, query, ref);
	}

	/**
	 * Provides the default equals calculation. May be overidden by handlers for
	 * other protocols that have different requirements for equals(). This method
	 * requires that none of its arguments is null. This is guaranteed by the
	 * fact that it is only called by java.net.URL class.
	 *
	 * @param url1 An URL object
	 * @param url2 An URL object
	 */
	protected boolean equals(URL url1, URL url2) {
		// This comparison is very conservative.  It assumes that any
		// field can be null.
		return (
			url1.getPort() == url2.getPort()
				&& ((url1.getProtocol() == null && url2.getProtocol() == null)
					|| (url1.getProtocol() != null
						&& url1.getProtocol().equals(url2.getProtocol())))
				&& ((url1.getUserInfo() == null && url2.getUserInfo() == null)
					|| (url1.getUserInfo() != null
						&& url1.getUserInfo().equals(url2.getUserInfo())))
				&& ((url1.getAuthority() == null && url2.getAuthority() == null)
					|| (url1.getAuthority() != null
						&& url1.getAuthority().equals(url2.getAuthority())))
				&& ((url1.getHost() == null && url2.getHost() == null)
					|| (url1.getHost() != null
						&& url1.getHost().equals(url2.getHost())))
				&& ((url1.getPath() == null && url2.getPath() == null)
					|| (url1.getPath() != null
						&& url1.getPath().equals(url2.getPath())))
				&& ((url1.getQuery() == null && url2.getQuery() == null)
					|| (url1.getQuery() != null
						&& url1.getQuery().equals(url2.getQuery())))
				&& ((url1.getRef() == null && url2.getRef() == null)
					|| (url1.getRef() != null
						&& url1.getRef().equals(url2.getRef()))));
	}

	/**
	 * Compares the host components of two URLs.
	 *
	 * @exception UnknownHostException If an unknown host is found
	 */
	protected boolean hostsEqual(URL url1, URL url2)
		throws UnknownHostException {
		InetAddress addr1 = InetAddress.getByName(url1.getHost());
		InetAddress addr2 = InetAddress.getByName(url2.getHost());

		return addr1.equals(addr2);
	}

	/**
	 * Get the IP address of our host. An empty host field or a DNS failure will
	 * result in a null return.
	 */
	protected InetAddress getHostAddress(URL url) {
		String hostname = url.getHost();

		if (hostname == "")
			return null;

		try {
			return InetAddress.getByName(hostname);
		} catch (UnknownHostException e) {
			return null;
		}
	}

	/**
	 * Returns the default port for a URL parsed by this handler. This method is
	 * meant to be overidden by handlers with default port numbers.
	 */
	protected int getDefaultPort() {
		return -1;
	}

	/**
	 * Provides the default hash calculation. May be overidden by handlers for
	 * other protocols that have different requirements for hashCode calculation.
	 */
	protected int hashCode(URL url) {
		return url.getProtocol().hashCode()
			+ ((url.getHost() == null) ? 0 : url.getHost().hashCode())
			+ url.getFile().hashCode()
			+ url.getPort();
	}

	/**
	 * This method converts a URL object into a String.  This method creates
	 * Strings in the mold of http URL's, so protocol handlers which use URL's
	 * that have a different syntax should override this method
	 *
	 * @param url The URL object to convert
	 */
	protected String toExternalForm(URL u) {
		String protocol, host, file, ref;
		int port;

		protocol = u.getProtocol();

		// JDK 1.2 online doc infers that host could be null because it
		// explicitly states that file cannot be null, but is silent on host.
		host = u.getHost();
		if (host == null)
			host = "";

		port = u.getPort();
		file = u.getFile();
		ref = u.getRef();

		// Guess a reasonable size for the string buffer so we have to resize
		// at most once.
		int size = protocol.length() + host.length() + file.length() + 24;
		StringBuffer sb = new StringBuffer(size);

		if (protocol != null && protocol.length() > 0) {
			sb.append(protocol);
			sb.append(":");
		}

		if (host.length() != 0)
			sb.append("//").append(host);

		// Note that this produces different results from JDK 1.2 as JDK 1.2
		// ignores a non-default port if host is null or "".  That is inconsistent
		// with the spec since the result of this method is spec'ed so it can be
		// used to construct a new URL that is equivalent to the original.
		boolean port_needed = port > 0 && port != getDefaultPort();
		if (port_needed)
			sb.append(':').append(port);

		sb.append(file);

		if (ref != null)
			sb.append('#').append(ref);

		return sb.toString();
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?