📄 urlstreamhandler.java
字号:
{ // Strip of the previous directory - if it exists. int previous = file.lastIndexOf('/', index - 1); if (previous >= 0) file = file.substring(0, previous) + file.substring(index + 3); else break; } return file; } /** * Compares two URLs, excluding the fragment component * * @param url1 The first url * @param url2 The second url to compare with the first * * @return True if both URLs point to the same file, false otherwise. * * @specnote Now protected */ 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) return false; int p1 = url1.getPort(); if (p1 == -1) p1 = url1.ph.getDefaultPort(); int p2 = url2.getPort(); if (p2 == -1) p2 = url2.ph.getDefaultPort(); if (p1 != p2) return false; String s1; String 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 * * @return True if both given URLs are equal, false otherwise. */ 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. * * @param url1 The first URL. * @param url2 The second URL. * * @return True if both URLs contain the same host. */ protected boolean hostsEqual(URL url1, URL url2) { InetAddress addr1 = getHostAddress(url1); InetAddress addr2 = getHostAddress(url2); if (addr1 != null && addr2 != null) return addr1.equals(addr2); String host1 = url1.getHost(); String host2 = url2.getHost(); if (host1 != null && host2 != null) return host1.equalsIgnoreCase(host2); return host1 == null && host2 == null; } /** * Get the IP address of our host. An empty host field or a DNS failure will * result in a null return. * * @param url The URL to return the host address for. * * @return The address of the hostname in url. */ protected InetAddress getHostAddress(URL url) { String hostname = url.getHost(); if (hostname.equals("")) 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. * * @return The default port number. */ 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. * * @param url The URL to calc the hashcode for. * * @return The hashcode for the given URL. */ 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 * * @return A string representation of the url */ protected String toExternalForm(URL url) { String protocol; String file; String ref; String authority; protocol = url.getProtocol(); authority = url.getAuthority(); if (authority == null) authority = ""; file = url.getFile(); ref = url.getRef(); // Guess a reasonable size for the string buffer so we have to resize // at most once. int size = protocol.length() + authority.length() + file.length() + 24; StringBuffer sb = new StringBuffer(size); if (protocol.length() > 0) { sb.append(protocol); sb.append(":"); } // If we have superfluous leading slashes (that means, at least 2) // we always add the authority component ("//" + host) to // avoid ambiguity. Otherwise we would generate an URL like // proto://home/foo // where we meant: // host: <empty> - file: //home/foo // but URL spec says it is: // host: home - file: /foo if (authority.length() != 0 || file.startsWith("//") ) sb.append("//").append(authority).append(file); else sb.append(file); if (ref != null) sb.append('#').append(ref); return sb.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -