📄 url.java
字号:
/* URL.java -- Uniform Resource Locator Class Copyright (C) 1998, 1999, 2000, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package java.net;import gnu.java.net.URLParseError;import java.io.IOException;import java.io.InputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;import java.security.AccessController;import java.security.PrivilegedAction;import java.util.HashMap;import java.util.StringTokenizer;/* * Written using on-line Java Platform 1.2 API Specification, as well * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998). * Status: Believed complete and correct. *//** * This final class represents an Internet Uniform Resource Locator (URL). * For details on the syntax of URL's and what they can be used for, * refer to RFC 1738, available from <a * href="http://ds.internic.net/rfcs/rfc1738.txt"> * http://ds.internic.net/rfcs/rfc1738.txt</a> * <p> * There are a great many protocols supported by URL's such as "http", * "ftp", and "file". This object can handle any arbitrary URL for which * a URLStreamHandler object can be written. Default protocol handlers * are provided for the "http" and "ftp" protocols. Additional protocols * handler implementations may be provided in the future. In any case, * an application or applet can install its own protocol handlers that * can be "chained" with other protocol hanlders in the system to extend * the base functionality provided with this class. (Note, however, that * unsigned applets cannot access properties by default or install their * own protocol handlers). * <p> * This chaining is done via the system property java.protocol.handler.pkgs * If this property is set, it is assumed to be a "|" separated list of * package names in which to attempt locating protocol handlers. The * protocol handler is searched for by appending the string * ".<protocol>.Handler" to each packed in the list until a hander is * found. If a protocol handler is not found in this list of packages, or if * the property does not exist, then the default protocol handler of * "gnu.java.net.<protocol>.Handler" is tried. If this is * unsuccessful, a MalformedURLException is thrown. * <p> * All of the constructor methods of URL attempt to load a protocol * handler and so any needed protocol handlers must be installed when * the URL is constructed. * <p> * Here is an example of how URL searches for protocol handlers. Assume * the value of java.protocol.handler.pkgs is "com.foo|com.bar" and the * URL is "news://comp.lang.java.programmer". URL would looking the * following places for protocol handlers: * <p><pre> * com.foo.news.Handler * com.bar.news.Handler * gnu.java.net.news.Handler * </pre><p> * If the protocol handler is not found in any of those locations, a * MalformedURLException would be thrown. * <p> * Please note that a protocol handler must be a subclass of * URLStreamHandler. * <p> * Normally, this class caches protocol handlers. Once it finds a handler * for a particular protocol, it never tries to look up a new handler * again. However, if the system property * gnu.java.net.nocache_protocol_handlers is set, then this * caching behavior is disabled. This property is specific to this * implementation. Sun's JDK may or may not do protocol caching, but it * almost certainly does not examine this property. * <p> * Please also note that an application can install its own factory for * loading protocol handlers (see setURLStreamHandlerFactory). If this is * done, then the above information is superseded and the behavior of this * class in loading protocol handlers is dependent on that factory. * * @author Aaron M. Renn (arenn@urbanophile.com) * @author Warren Levy (warrenl@cygnus.com) * * @see URLStreamHandler */public final class URL implements Serializable{ private static final String DEFAULT_SEARCH_PATH = "gnu.java.net.protocol|gnu.inet"; // Cached System ClassLoader private static ClassLoader systemClassLoader; /** * The name of the protocol for this URL. * The protocol is always stored in lower case. */ private String protocol; /** * The "authority" portion of the URL. */ private String authority; /** * The hostname or IP address of this protocol. * This includes a possible user. For example <code>joe@some.host.net</code>. */ private String host; /** * The user information necessary to establish the connection. */ private String userInfo; /** * The port number of this protocol or -1 if the port number used is * the default for this protocol. */ private int port = -1; // Initialize for constructor using context. /** * The "file" portion of the URL. It is defined as <code>path[?query]</code>. */ private String file; /** * The anchor portion of the URL. */ private String ref; /** * This is the hashCode for this URL */ private int hashCode; /** * The protocol handler in use for this URL */ transient URLStreamHandler ph; /** * If an application installs its own protocol handler factory, this is * where we keep track of it. */ private static URLStreamHandlerFactory factory; private static final long serialVersionUID = -7627629688361524110L; /** * This a table where we cache protocol handlers to avoid the overhead * of looking them up each time. */ private static HashMap ph_cache = new HashMap(); /** * Whether or not to cache protocol handlers. */ private static boolean cache_handlers; static { String s = System.getProperty("gnu.java.net.nocache_protocol_handlers"); if (s == null) cache_handlers = true; else cache_handlers = false; } /** * Constructs a URL and loads a protocol handler for the values passed as * arguments. * * @param protocol The protocol for this URL ("http", "ftp", etc) * @param host The hostname or IP address to connect to * @param port The port number to use, or -1 to use the protocol's * default port * @param file The "file" portion of the URL. * * @exception MalformedURLException If a protocol handler cannot be loaded or * a parse error occurs. */ public URL(String protocol, String host, int port, String file) throws MalformedURLException { this(protocol, host, port, file, null); } /** * Constructs a URL and loads a protocol handler for the values passed in * as arugments. Uses the default port for the protocol. * * @param protocol The protocol for this URL ("http", "ftp", etc) * @param host The hostname or IP address for this URL * @param file The "file" portion of this URL. * * @exception MalformedURLException If a protocol handler cannot be loaded or * a parse error occurs. */ public URL(String protocol, String host, String file) throws MalformedURLException { this(protocol, host, -1, file, null); } /** * This method initializes a new instance of <code>URL</code> with the * specified protocol, host, port, and file. Additionally, this method * allows the caller to specify a protocol handler to use instead of * the default. If this handler is specified, the caller must have * the "specifyStreamHandler" permission (see <code>NetPermission</code>) * or a <code>SecurityException</code> will be thrown. * * @param protocol The protocol for this URL ("http", "ftp", etc) * @param host The hostname or IP address to connect to * @param port The port number to use, or -1 to use the protocol's default * port * @param file The "file" portion of the URL. * @param ph The protocol handler to use with this URL. * * @exception MalformedURLException If no protocol handler can be loaded * for the specified protocol. * @exception SecurityException If the <code>SecurityManager</code> exists * and does not allow the caller to specify its own protocol handler. * * @since 1.2 */ public URL(String protocol, String host, int port, String file, URLStreamHandler ph) throws MalformedURLException { if (protocol == null) throw new MalformedURLException("null protocol"); protocol = protocol.toLowerCase(); this.protocol = protocol; if (ph != null) { SecurityManager s = System.getSecurityManager(); if (s != null) s.checkPermission(new NetPermission("specifyStreamHandler")); this.ph = ph; } else this.ph = getURLStreamHandler(protocol); if (this.ph == null) throw new MalformedURLException("Protocol handler not found: " + protocol); this.host = host; this.port = port; this.authority = (host != null) ? host : ""; if (port >= 0 && host != null) this.authority += ":" + port; int hashAt = file.indexOf('#'); if (hashAt < 0) { this.file = file; this.ref = null; } else { this.file = file.substring(0, hashAt); this.ref = file.substring(hashAt + 1); } hashCode = hashCode(); // Used for serialization. } /** * Initializes a URL from a complete string specification such as * "http://www.urbanophile.com/arenn/". First the protocol name is parsed * out of the string. Then a handler is located for that protocol and * the parseURL() method of that protocol handler is used to parse the * remaining fields. * * @param spec The complete String representation of a URL * * @exception MalformedURLException If a protocol handler cannot be found * or the URL cannot be parsed */ public URL(String spec) throws MalformedURLException { this((URL) null, spec != null ? spec : "", (URLStreamHandler) null); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -