📄 url.java
字号:
public int hashCode() { if (hashCode != 0) return hashCode; // Use cached value if available. else return ph.hashCode(this); } /** * Returns a URLConnection object that represents a connection to the remote * object referred to by the URL. The URLConnection is created by calling the * openConnection() method of the protocol handler * * @return A URLConnection for this URL * * @exception IOException If an error occurs */ public URLConnection openConnection() throws IOException { return ph.openConnection(this); } /** * Opens a connection to this URL and returns an InputStream for reading * from that connection * * @return An <code>InputStream</code> for this URL. * * @exception IOException If an error occurs */ public InputStream openStream() throws IOException { return openConnection().getInputStream(); } /** * Tests whether or not another URL refers to the same "file" as this one. * This will be true if and only if the passed object is not null, is a * URL, and matches all fields but the ref (ie, protocol, host, port, * and file); * * @param url The URL object to test with * * @return true if URL matches this URL's file, false otherwise */ public boolean sameFile(URL url) { return ph.sameFile(this, url); } /** * Sets the specified fields of the URL. This is not a public method so * that only URLStreamHandlers can modify URL fields. This might be called * by the <code>parseURL()</code> method in that class. URLs are otherwise * constant. If the given protocol does not exist, it will keep the previously * set protocol. * * @param protocol The protocol name for this URL * @param host The hostname or IP address for this URL * @param port The port number of this URL * @param file The "file" portion of this URL. * @param ref The anchor portion of this URL. */ protected void set(String protocol, String host, int port, String file, String ref) { URLStreamHandler protocolHandler = null; protocol = protocol.toLowerCase(); if (! this.protocol.equals(protocol)) protocolHandler = getURLStreamHandler(protocol); // It is an hidden feature of the JDK. If the protocol does not exist, // we keep the previously initialized protocol. if (protocolHandler != null) { this.ph = protocolHandler; this.protocol = protocol; } this.authority = ""; this.port = port; this.host = host; this.file = file; this.ref = ref; if (host != null) this.authority += host; if (port >= 0) this.authority += ":" + port; hashCode = hashCode(); // Used for serialization. } /** * Sets the specified fields of the URL. This is not a public method so * that only URLStreamHandlers can modify URL fields. URLs are otherwise * constant. If the given protocol does not exist, it will keep the previously * set protocol. * * @param protocol The protocol name for this URL. * @param host The hostname or IP address for this URL. * @param port The port number of this URL. * @param authority The authority of this URL. * @param userInfo The user and password (if needed) of this URL. * @param path The "path" portion of this URL. * @param query The query of this URL. * @param ref The anchor portion of this URL. * * @since 1.3 */ protected void set(String protocol, String host, int port, String authority, String userInfo, String path, String query, String ref) { URLStreamHandler protocolHandler = null; protocol = protocol.toLowerCase(); if (! this.protocol.equals(protocol)) protocolHandler = getURLStreamHandler(protocol); // It is an hidden feature of the JDK. If the protocol does not exist, // we keep the previously initialized protocol. if (protocolHandler != null) { this.ph = protocolHandler; this.protocol = protocol; } this.host = host; this.userInfo = userInfo; this.port = port; this.authority = authority; if (query == null) this.file = path; else this.file = path + "?" + query; this.ref = ref; hashCode = hashCode(); // Used for serialization. } /** * Sets the URLStreamHandlerFactory for this class. This factory is * responsible for returning the appropriate protocol handler for * a given URL. * * @param fac The URLStreamHandlerFactory class to use * * @exception Error If the factory is alread set. * @exception SecurityException If a security manager exists and its * checkSetFactory method doesn't allow the operation */ public static synchronized void setURLStreamHandlerFactory(URLStreamHandlerFactory fac) { if (factory != null) throw new Error("URLStreamHandlerFactory already set"); // Throw an exception if an extant security mgr precludes // setting the factory. SecurityManager s = System.getSecurityManager(); if (s != null) s.checkSetFactory(); factory = fac; } /** * Returns a String representing this URL. The String returned is * created by calling the protocol handler's toExternalForm() method. * * @return A string for this URL */ public String toExternalForm() { // Identical to toString(). return ph.toExternalForm(this); } /** * Returns a String representing this URL. Identical to toExternalForm(). * The value returned is created by the protocol handler's * toExternalForm method. Overrides Object.toString() * * @return A string for this URL */ public String toString() { // Identical to toExternalForm(). return ph.toExternalForm(this); } /** * This internal method is used in two different constructors to load * a protocol handler for this URL. * * @param protocol The protocol to load a handler for * * @return A URLStreamHandler for this protocol, or null when not found. */ private static synchronized URLStreamHandler getURLStreamHandler(String protocol) { URLStreamHandler ph = null; // First, see if a protocol handler is in our cache. if (cache_handlers) { if ((ph = (URLStreamHandler) ph_cache.get(protocol)) != null) return ph; } // If a non-default factory has been set, use it to find the protocol. if (factory != null) { ph = factory.createURLStreamHandler(protocol); } else if (protocol.equals("core")) { ph = new gnu.java.net.protocol.core.Handler(); } else if (protocol.equals("file")) { // This is an interesting case. It's tempting to think that we // could call Class.forName ("gnu.java.net.protocol.file.Handler") to // get the appropriate class. Unfortunately, if we do that the // program will never terminate, because getURLStreamHandler is // eventually called by Class.forName. // // Treating "file" as a special case is the minimum that will // fix this problem. If other protocols are required in a // statically linked application they will need to be handled in // the same way as "file". ph = new gnu.java.net.protocol.file.Handler(); } // Non-default factory may have returned null or a factory wasn't set. // Use the default search algorithm to find a handler for this protocol. if (ph == null) { // Get the list of packages to check and append our default handler // to it, along with the JDK specified default as a last resort. // Except in very unusual environments the JDK specified one shouldn't // ever be needed (or available). String ph_search_path = System.getProperty("java.protocol.handler.pkgs"); // Tack our default package on at the ends. if (ph_search_path != null) ph_search_path += "|" + DEFAULT_SEARCH_PATH; else ph_search_path = DEFAULT_SEARCH_PATH; // Finally loop through our search path looking for a match. StringTokenizer pkgPrefix = new StringTokenizer(ph_search_path, "|"); // Cache the systemClassLoader if (systemClassLoader == null) { systemClassLoader = (ClassLoader) AccessController.doPrivileged (new PrivilegedAction() { public Object run() { return ClassLoader.getSystemClassLoader(); } }); } do { try { // Try to get a class from the system/application // classloader, initialize it, make an instance // and try to cast it to a URLStreamHandler. String clsName = (pkgPrefix.nextToken() + "." + protocol + ".Handler"); Class c = Class.forName(clsName, true, systemClassLoader); ph = (URLStreamHandler) c.newInstance(); } catch (ThreadDeath death) { throw death; } catch (Throwable t) { // Ignored. } } while (ph == null && pkgPrefix.hasMoreTokens()); } // Update the hashtable with the new protocol handler. if (ph != null && cache_handlers) ph_cache.put(protocol, ph); else ph = null; return ph; } private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException { ois.defaultReadObject(); this.ph = getURLStreamHandler(protocol); if (this.ph == null) throw new IOException("Handler for protocol " + protocol + " not found"); } private void writeObject(ObjectOutputStream oos) throws IOException { oos.defaultWriteObject(); } /** * Returns the equivalent <code>URI</code> object for this <code>URL</code>. * This is the same as calling <code>new URI(this.toString())</code>. * RFC2396-compliant URLs are guaranteed a successful conversion to * a <code>URI</code> instance. However, there are some values which * form valid URLs, but which do not also form RFC2396-compliant URIs. * * @throws URISyntaxException if this URL is not RFC2396-compliant, * and thus can not be successfully converted to a URI. */ public URI toURI() throws URISyntaxException { return new URI(toString()); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -