0N/A/*
2362N/A * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.net;
0N/A
0N/Aimport java.io.IOException;
0N/Aimport java.io.InputStream;
0N/Aimport java.io.OutputStream;
5426N/Aimport java.security.AccessController;
5426N/Aimport java.security.PrivilegedAction;
0N/Aimport java.util.Hashtable;
0N/Aimport java.util.StringTokenizer;
0N/Aimport sun.security.util.SecurityConstants;
0N/A
0N/A/**
0N/A * Class <code>URL</code> represents a Uniform Resource
0N/A * Locator, a pointer to a "resource" on the World
0N/A * Wide Web. A resource can be something as simple as a file or a
0N/A * directory, or it can be a reference to a more complicated object,
0N/A * such as a query to a database or to a search engine. More
0N/A * information on the types of URLs and their formats can be found at:
0N/A * <blockquote>
0N/A * <a href="http://www.socs.uts.edu.au/MosaicDocs-old/url-primer.html">
0N/A * <i>http://www.socs.uts.edu.au/MosaicDocs-old/url-primer.html</i></a>
0N/A * </blockquote>
0N/A * <p>
0N/A * In general, a URL can be broken into several parts. The previous
0N/A * example of a URL indicates that the protocol to use is
0N/A * <code>http</code> (HyperText Transfer Protocol) and that the
0N/A * information resides on a host machine named
0N/A * <code>www.socs.uts.edu.au</code>. The information on that host
0N/A * machine is named <code>/MosaicDocs-old/url-primer.html</code>. The exact
0N/A * meaning of this name on the host machine is both protocol
0N/A * dependent and host dependent. The information normally resides in
0N/A * a file, but it could be generated on the fly. This component of
0N/A * the URL is called the <i>path</i> component.
0N/A * <p>
0N/A * A URL can optionally specify a "port", which is the
0N/A * port number to which the TCP connection is made on the remote host
0N/A * machine. If the port is not specified, the default port for
0N/A * the protocol is used instead. For example, the default port for
0N/A * <code>http</code> is <code>80</code>. An alternative port could be
0N/A * specified as:
0N/A * <blockquote><pre>
0N/A * http://www.socs.uts.edu.au:80/MosaicDocs-old/url-primer.html
0N/A * </pre></blockquote>
0N/A * <p>
0N/A * The syntax of <code>URL</code> is defined by <a
374N/A * href="http://www.ietf.org/rfc/rfc2396.txt"><i>RFC&nbsp;2396: Uniform
0N/A * Resource Identifiers (URI): Generic Syntax</i></a>, amended by <a
0N/A * href="http://www.ietf.org/rfc/rfc2732.txt"><i>RFC&nbsp;2732: Format for
0N/A * Literal IPv6 Addresses in URLs</i></a>. The Literal IPv6 address format
0N/A * also supports scope_ids. The syntax and usage of scope_ids is described
0N/A * <a href="Inet6Address.html#scoped">here</a>.
0N/A * <p>
0N/A * A URL may have appended to it a "fragment", also known
0N/A * as a "ref" or a "reference". The fragment is indicated by the sharp
0N/A * sign character "#" followed by more characters. For example,
0N/A * <blockquote><pre>
0N/A * http://java.sun.com/index.html#chapter1
0N/A * </pre></blockquote>
0N/A * <p>
0N/A * This fragment is not technically part of the URL. Rather, it
0N/A * indicates that after the specified resource is retrieved, the
0N/A * application is specifically interested in that part of the
0N/A * document that has the tag <code>chapter1</code> attached to it. The
0N/A * meaning of a tag is resource specific.
0N/A * <p>
0N/A * An application can also specify a "relative URL",
0N/A * which contains only enough information to reach the resource
0N/A * relative to another URL. Relative URLs are frequently used within
0N/A * HTML pages. For example, if the contents of the URL:
0N/A * <blockquote><pre>
0N/A * http://java.sun.com/index.html
0N/A * </pre></blockquote>
0N/A * contained within it the relative URL:
0N/A * <blockquote><pre>
0N/A * FAQ.html
0N/A * </pre></blockquote>
0N/A * it would be a shorthand for:
0N/A * <blockquote><pre>
0N/A * http://java.sun.com/FAQ.html
0N/A * </pre></blockquote>
0N/A * <p>
0N/A * The relative URL need not specify all the components of a URL. If
0N/A * the protocol, host name, or port number is missing, the value is
0N/A * inherited from the fully specified URL. The file component must be
0N/A * specified. The optional fragment is not inherited.
0N/A * <p>
0N/A * The URL class does not itself encode or decode any URL components
0N/A * according to the escaping mechanism defined in RFC2396. It is the
0N/A * responsibility of the caller to encode any fields, which need to be
0N/A * escaped prior to calling URL, and also to decode any escaped fields,
0N/A * that are returned from URL. Furthermore, because URL has no knowledge
0N/A * of URL escaping, it does not recognise equivalence between the encoded
0N/A * or decoded form of the same URL. For example, the two URLs:<br>
0N/A * <pre> http://foo.com/hello world/ and http://foo.com/hello%20world</pre>
0N/A * would be considered not equal to each other.
0N/A * <p>
0N/A * Note, the {@link java.net.URI} class does perform escaping of its
0N/A * component fields in certain circumstances. The recommended way
0N/A * to manage the encoding and decoding of URLs is to use {@link java.net.URI},
0N/A * and to convert between these two classes using {@link #toURI()} and
0N/A * {@link URI#toURL()}.
0N/A * <p>
0N/A * The {@link URLEncoder} and {@link URLDecoder} classes can also be
0N/A * used, but only for HTML form encoding, which is not the same
0N/A * as the encoding scheme defined in RFC2396.
0N/A *
0N/A * @author James Gosling
0N/A * @since JDK1.0
0N/A */
0N/Apublic final class URL implements java.io.Serializable {
0N/A
0N/A static final long serialVersionUID = -7627629688361524110L;
0N/A
0N/A /**
0N/A * The property which specifies the package prefix list to be scanned
0N/A * for protocol handlers. The value of this property (if any) should
0N/A * be a vertical bar delimited list of package names to search through
0N/A * for a protocol handler to load. The policy of this class is that
0N/A * all protocol handlers will be in a class called <protocolname>.Handler,
0N/A * and each package in the list is examined in turn for a matching
0N/A * handler. If none are found (or the property is not specified), the
0N/A * default package prefix, sun.net.www.protocol, is used. The search
0N/A * proceeds from the first package in the list to the last and stops
0N/A * when a match is found.
0N/A */
0N/A private static final String protocolPathProp = "java.protocol.handler.pkgs";
0N/A
0N/A /**
0N/A * The protocol to use (ftp, http, nntp, ... etc.) .
0N/A * @serial
0N/A */
0N/A private String protocol;
0N/A
0N/A /**
0N/A * The host name to connect to.
0N/A * @serial
0N/A */
0N/A private String host;
0N/A
0N/A /**
0N/A * The protocol port to connect to.
0N/A * @serial
0N/A */
0N/A private int port = -1;
0N/A
0N/A /**
0N/A * The specified file name on that host. <code>file</code> is
0N/A * defined as <code>path[?query]</code>
0N/A * @serial
0N/A */
0N/A private String file;
0N/A
0N/A /**
0N/A * The query part of this URL.
0N/A */
0N/A private transient String query;
0N/A
0N/A /**
0N/A * The authority part of this URL.
0N/A * @serial
0N/A */
0N/A private String authority;
0N/A
0N/A /**
0N/A * The path part of this URL.
0N/A */
0N/A private transient String path;
0N/A
0N/A /**
0N/A * The userinfo part of this URL.
0N/A */
0N/A private transient String userInfo;
0N/A
0N/A /**
0N/A * # reference.
0N/A * @serial
0N/A */
0N/A private String ref;
0N/A
0N/A /**
0N/A * The host's IP address, used in equals and hashCode.
0N/A * Computed on demand. An uninitialized or unknown hostAddress is null.
0N/A */
0N/A transient InetAddress hostAddress;
0N/A
0N/A /**
0N/A * The URLStreamHandler for this URL.
0N/A */
0N/A transient URLStreamHandler handler;
0N/A
0N/A /* Our hash code.
0N/A * @serial
0N/A */
0N/A private int hashCode = -1;
0N/A
0N/A /**
0N/A * Creates a <code>URL</code> object from the specified
0N/A * <code>protocol</code>, <code>host</code>, <code>port</code>
0N/A * number, and <code>file</code>.<p>
0N/A *
0N/A * <code>host</code> can be expressed as a host name or a literal
0N/A * IP address. If IPv6 literal address is used, it should be
0N/A * enclosed in square brackets (<tt>'['</tt> and <tt>']'</tt>), as
0N/A * specified by <a
0N/A * href="http://www.ietf.org/rfc/rfc2732.txt">RFC&nbsp;2732</a>;
0N/A * However, the literal IPv6 address format defined in <a
0N/A * href="http://www.ietf.org/rfc/rfc2373.txt"><i>RFC&nbsp;2373: IP
0N/A * Version 6 Addressing Architecture</i></a> is also accepted.<p>
0N/A *
0N/A * Specifying a <code>port</code> number of <code>-1</code>
0N/A * indicates that the URL should use the default port for the
0N/A * protocol.<p>
0N/A *
0N/A * If this is the first URL object being created with the specified
0N/A * protocol, a <i>stream protocol handler</i> object, an instance of
0N/A * class <code>URLStreamHandler</code>, is created for that protocol:
0N/A * <ol>
0N/A * <li>If the application has previously set up an instance of
0N/A * <code>URLStreamHandlerFactory</code> as the stream handler factory,
0N/A * then the <code>createURLStreamHandler</code> method of that instance
0N/A * is called with the protocol string as an argument to create the
0N/A * stream protocol handler.
0N/A * <li>If no <code>URLStreamHandlerFactory</code> has yet been set up,
0N/A * or if the factory's <code>createURLStreamHandler</code> method
0N/A * returns <code>null</code>, then the constructor finds the
0N/A * value of the system property:
0N/A * <blockquote><pre>
0N/A * java.protocol.handler.pkgs
0N/A * </pre></blockquote>
0N/A * If the value of that system property is not <code>null</code>,
0N/A * it is interpreted as a list of packages separated by a vertical
0N/A * slash character '<code>|</code>'. The constructor tries to load
0N/A * the class named:
0N/A * <blockquote><pre>
0N/A * &lt;<i>package</i>&gt;.&lt;<i>protocol</i>&gt;.Handler
0N/A * </pre></blockquote>
0N/A * where &lt;<i>package</i>&gt; is replaced by the name of the package
0N/A * and &lt;<i>protocol</i>&gt; is replaced by the name of the protocol.
0N/A * If this class does not exist, or if the class exists but it is not
0N/A * a subclass of <code>URLStreamHandler</code>, then the next package
0N/A * in the list is tried.
0N/A * <li>If the previous step fails to find a protocol handler, then the
0N/A * constructor tries to load from a system default package.
0N/A * <blockquote><pre>
0N/A * &lt;<i>system default package</i>&gt;.&lt;<i>protocol</i>&gt;.Handler
0N/A * </pre></blockquote>
0N/A * If this class does not exist, or if the class exists but it is not a
0N/A * subclass of <code>URLStreamHandler</code>, then a
0N/A * <code>MalformedURLException</code> is thrown.
0N/A * </ol>
0N/A *
0N/A * <p>Protocol handlers for the following protocols are guaranteed
0N/A * to exist on the search path :-
0N/A * <blockquote><pre>
0N/A * http, https, ftp, file, and jar
0N/A * </pre></blockquote>
0N/A * Protocol handlers for additional protocols may also be
0N/A * available.
0N/A *
0N/A * <p>No validation of the inputs is performed by this constructor.
0N/A *
0N/A * @param protocol the name of the protocol to use.
0N/A * @param host the name of the host.
0N/A * @param port the port number on the host.
0N/A * @param file the file on the host
0N/A * @exception MalformedURLException if an unknown protocol is specified.
0N/A * @see java.lang.System#getProperty(java.lang.String)
0N/A * @see java.net.URL#setURLStreamHandlerFactory(
0N/A * java.net.URLStreamHandlerFactory)
0N/A * @see java.net.URLStreamHandler
0N/A * @see java.net.URLStreamHandlerFactory#createURLStreamHandler(
0N/A * java.lang.String)
0N/A */
0N/A public URL(String protocol, String host, int port, String file)
0N/A throws MalformedURLException
0N/A {
0N/A this(protocol, host, port, file, null);
0N/A }
0N/A
0N/A /**
0N/A * Creates a URL from the specified <code>protocol</code>
0N/A * name, <code>host</code> name, and <code>file</code> name. The
0N/A * default port for the specified protocol is used.
0N/A * <p>
0N/A * This method is equivalent to calling the four-argument
0N/A * constructor with the arguments being <code>protocol</code>,
0N/A * <code>host</code>, <code>-1</code>, and <code>file</code>.
0N/A *
0N/A * No validation of the inputs is performed by this constructor.
0N/A *
0N/A * @param protocol the name of the protocol to use.
0N/A * @param host the name of the host.
0N/A * @param file the file on the host.
0N/A * @exception MalformedURLException if an unknown protocol is specified.
0N/A * @see java.net.URL#URL(java.lang.String, java.lang.String,
0N/A * int, java.lang.String)
0N/A */
0N/A public URL(String protocol, String host, String file)
0N/A throws MalformedURLException {
0N/A this(protocol, host, -1, file);
0N/A }
0N/A
0N/A /**
0N/A * Creates a <code>URL</code> object from the specified
0N/A * <code>protocol</code>, <code>host</code>, <code>port</code>
0N/A * number, <code>file</code>, and <code>handler</code>. Specifying
0N/A * a <code>port</code> number of <code>-1</code> indicates that
0N/A * the URL should use the default port for the protocol. Specifying
0N/A * a <code>handler</code> of <code>null</code> indicates that the URL
0N/A * should use a default stream handler for the protocol, as outlined
0N/A * for:
0N/A * java.net.URL#URL(java.lang.String, java.lang.String, int,
0N/A * java.lang.String)
0N/A *
0N/A * <p>If the handler is not null and there is a security manager,
0N/A * the security manager's <code>checkPermission</code>
0N/A * method is called with a
0N/A * <code>NetPermission("specifyStreamHandler")</code> permission.
0N/A * This may result in a SecurityException.
0N/A *
0N/A * No validation of the inputs is performed by this constructor.
0N/A *
0N/A * @param protocol the name of the protocol to use.
0N/A * @param host the name of the host.
0N/A * @param port the port number on the host.
0N/A * @param file the file on the host
0N/A * @param handler the stream handler for the URL.
0N/A * @exception MalformedURLException if an unknown protocol is specified.
0N/A * @exception SecurityException
0N/A * if a security manager exists and its
0N/A * <code>checkPermission</code> method doesn't allow
0N/A * specifying a stream handler explicitly.
0N/A * @see java.lang.System#getProperty(java.lang.String)
0N/A * @see java.net.URL#setURLStreamHandlerFactory(
0N/A * java.net.URLStreamHandlerFactory)
0N/A * @see java.net.URLStreamHandler
0N/A * @see java.net.URLStreamHandlerFactory#createURLStreamHandler(
0N/A * java.lang.String)
0N/A * @see SecurityManager#checkPermission
0N/A * @see java.net.NetPermission
0N/A */
0N/A public URL(String protocol, String host, int port, String file,
0N/A URLStreamHandler handler) throws MalformedURLException {
0N/A if (handler != null) {
0N/A SecurityManager sm = System.getSecurityManager();
0N/A if (sm != null) {
0N/A // check for permission to specify a handler
0N/A checkSpecifyHandler(sm);
0N/A }
0N/A }
0N/A
0N/A protocol = protocol.toLowerCase();
0N/A this.protocol = protocol;
0N/A if (host != null) {
0N/A
0N/A /**
0N/A * if host is a literal IPv6 address,
0N/A * we will make it conform to RFC 2732
0N/A */
0N/A if (host.indexOf(':') >= 0 && !host.startsWith("[")) {
0N/A host = "["+host+"]";
0N/A }
0N/A this.host = host;
0N/A
0N/A if (port < -1) {
0N/A throw new MalformedURLException("Invalid port number :" +
0N/A port);
0N/A }
0N/A this.port = port;
0N/A authority = (port == -1) ? host : host + ":" + port;
0N/A }
0N/A
0N/A Parts parts = new Parts(file);
0N/A path = parts.getPath();
0N/A query = parts.getQuery();
0N/A
0N/A if (query != null) {
0N/A this.file = path + "?" + query;
0N/A } else {
0N/A this.file = path;
0N/A }
0N/A ref = parts.getRef();
0N/A
0N/A // Note: we don't do validation of the URL here. Too risky to change
0N/A // right now, but worth considering for future reference. -br
0N/A if (handler == null &&
0N/A (handler = getURLStreamHandler(protocol)) == null) {
0N/A throw new MalformedURLException("unknown protocol: " + protocol);
0N/A }
0N/A this.handler = handler;
0N/A }
0N/A
0N/A /**
0N/A * Creates a <code>URL</code> object from the <code>String</code>
0N/A * representation.
0N/A * <p>
0N/A * This constructor is equivalent to a call to the two-argument
0N/A * constructor with a <code>null</code> first argument.
0N/A *
0N/A * @param spec the <code>String</code> to parse as a URL.
0N/A * @exception MalformedURLException if no protocol is specified, or an
0N/A * unknown protocol is found, or <tt>spec</tt> is <tt>null</tt>.
0N/A * @see java.net.URL#URL(java.net.URL, java.lang.String)
0N/A */
0N/A public URL(String spec) throws MalformedURLException {
0N/A this(null, spec);
0N/A }
0N/A
0N/A /**
0N/A * Creates a URL by parsing the given spec within a specified context.
0N/A *
0N/A * The new URL is created from the given context URL and the spec
0N/A * argument as described in
0N/A * RFC2396 &quot;Uniform Resource Identifiers : Generic * Syntax&quot; :
0N/A * <blockquote><pre>
0N/A * &lt;scheme&gt;://&lt;authority&gt;&lt;path&gt;?&lt;query&gt;#&lt;fragment&gt;
0N/A * </pre></blockquote>
0N/A * The reference is parsed into the scheme, authority, path, query and
0N/A * fragment parts. If the path component is empty and the scheme,
0N/A * authority, and query components are undefined, then the new URL is a
0N/A * reference to the current document. Otherwise, the fragment and query
0N/A * parts present in the spec are used in the new URL.
0N/A * <p>
0N/A * If the scheme component is defined in the given spec and does not match
0N/A * the scheme of the context, then the new URL is created as an absolute
0N/A * URL based on the spec alone. Otherwise the scheme component is inherited
0N/A * from the context URL.
0N/A * <p>
0N/A * If the authority component is present in the spec then the spec is
0N/A * treated as absolute and the spec authority and path will replace the
0N/A * context authority and path. If the authority component is absent in the
0N/A * spec then the authority of the new URL will be inherited from the
0N/A * context.
0N/A * <p>
0N/A * If the spec's path component begins with a slash character
0N/A * &quot;/&quot; then the
0N/A * path is treated as absolute and the spec path replaces the context path.
0N/A * <p>
0N/A * Otherwise, the path is treated as a relative path and is appended to the
0N/A * context path, as described in RFC2396. Also, in this case,
0N/A * the path is canonicalized through the removal of directory
0N/A * changes made by occurences of &quot;..&quot; and &quot;.&quot;.
0N/A * <p>
0N/A * For a more detailed description of URL parsing, refer to RFC2396.
0N/A *
0N/A * @param context the context in which to parse the specification.
0N/A * @param spec the <code>String</code> to parse as a URL.
0N/A * @exception MalformedURLException if no protocol is specified, or an
0N/A * unknown protocol is found, or <tt>spec</tt> is <tt>null</tt>.
0N/A * @see java.net.URL#URL(java.lang.String, java.lang.String,
0N/A * int, java.lang.String)
0N/A * @see java.net.URLStreamHandler
0N/A * @see java.net.URLStreamHandler#parseURL(java.net.URL,
0N/A * java.lang.String, int, int)
0N/A */
0N/A public URL(URL context, String spec) throws MalformedURLException {
0N/A this(context, spec, null);
0N/A }
0N/A
0N/A /**
0N/A * Creates a URL by parsing the given spec with the specified handler
0N/A * within a specified context. If the handler is null, the parsing
0N/A * occurs as with the two argument constructor.
0N/A *
0N/A * @param context the context in which to parse the specification.
0N/A * @param spec the <code>String</code> to parse as a URL.
0N/A * @param handler the stream handler for the URL.
0N/A * @exception MalformedURLException if no protocol is specified, or an
0N/A * unknown protocol is found, or <tt>spec</tt> is <tt>null</tt>.
0N/A * @exception SecurityException
0N/A * if a security manager exists and its
0N/A * <code>checkPermission</code> method doesn't allow
0N/A * specifying a stream handler.
0N/A * @see java.net.URL#URL(java.lang.String, java.lang.String,
0N/A * int, java.lang.String)
0N/A * @see java.net.URLStreamHandler
0N/A * @see java.net.URLStreamHandler#parseURL(java.net.URL,
0N/A * java.lang.String, int, int)
0N/A */
0N/A public URL(URL context, String spec, URLStreamHandler handler)
0N/A throws MalformedURLException
0N/A {
0N/A String original = spec;
0N/A int i, limit, c;
0N/A int start = 0;
0N/A String newProtocol = null;
0N/A boolean aRef=false;
0N/A boolean isRelative = false;
0N/A
0N/A // Check for permission to specify a handler
0N/A if (handler != null) {
0N/A SecurityManager sm = System.getSecurityManager();
0N/A if (sm != null) {
0N/A checkSpecifyHandler(sm);
0N/A }
0N/A }
0N/A
0N/A try {
0N/A limit = spec.length();
0N/A while ((limit > 0) && (spec.charAt(limit - 1) <= ' ')) {
0N/A limit--; //eliminate trailing whitespace
0N/A }
0N/A while ((start < limit) && (spec.charAt(start) <= ' ')) {
0N/A start++; // eliminate leading whitespace
0N/A }
0N/A
0N/A if (spec.regionMatches(true, start, "url:", 0, 4)) {
0N/A start += 4;
0N/A }
0N/A if (start < spec.length() && spec.charAt(start) == '#') {
0N/A /* we're assuming this is a ref relative to the context URL.
0N/A * This means protocols cannot start w/ '#', but we must parse
0N/A * ref URL's like: "hello:there" w/ a ':' in them.
0N/A */
0N/A aRef=true;
0N/A }
0N/A for (i = start ; !aRef && (i < limit) &&
0N/A ((c = spec.charAt(i)) != '/') ; i++) {
0N/A if (c == ':') {
0N/A
0N/A String s = spec.substring(start, i).toLowerCase();
0N/A if (isValidProtocol(s)) {
0N/A newProtocol = s;
0N/A start = i + 1;
0N/A }
0N/A break;
0N/A }
0N/A }
0N/A
0N/A // Only use our context if the protocols match.
0N/A protocol = newProtocol;
0N/A if ((context != null) && ((newProtocol == null) ||
0N/A newProtocol.equalsIgnoreCase(context.protocol))) {
0N/A // inherit the protocol handler from the context
0N/A // if not specified to the constructor
0N/A if (handler == null) {
0N/A handler = context.handler;
0N/A }
0N/A
0N/A // If the context is a hierarchical URL scheme and the spec
0N/A // contains a matching scheme then maintain backwards
0N/A // compatibility and treat it as if the spec didn't contain
0N/A // the scheme; see 5.2.3 of RFC2396
0N/A if (context.path != null && context.path.startsWith("/"))
0N/A newProtocol = null;
0N/A
0N/A if (newProtocol == null) {
0N/A protocol = context.protocol;
0N/A authority = context.authority;
0N/A userInfo = context.userInfo;
0N/A host = context.host;
0N/A port = context.port;
0N/A file = context.file;
0N/A path = context.path;
0N/A isRelative = true;
0N/A }
0N/A }
0N/A
0N/A if (protocol == null) {
0N/A throw new MalformedURLException("no protocol: "+original);
0N/A }
0N/A
0N/A // Get the protocol handler if not specified or the protocol
0N/A // of the context could not be used
0N/A if (handler == null &&
0N/A (handler = getURLStreamHandler(protocol)) == null) {
0N/A throw new MalformedURLException("unknown protocol: "+protocol);
0N/A }
0N/A
0N/A this.handler = handler;
0N/A
0N/A i = spec.indexOf('#', start);
0N/A if (i >= 0) {
0N/A ref = spec.substring(i + 1, limit);
0N/A limit = i;
0N/A }
0N/A
0N/A /*
0N/A * Handle special case inheritance of query and fragment
0N/A * implied by RFC2396 section 5.2.2.
0N/A */
0N/A if (isRelative && start == limit) {
0N/A query = context.query;
0N/A if (ref == null) {
0N/A ref = context.ref;
0N/A }
0N/A }
0N/A
0N/A handler.parseURL(this, spec, start, limit);
0N/A
0N/A } catch(MalformedURLException e) {
0N/A throw e;
0N/A } catch(Exception e) {
0N/A MalformedURLException exception = new MalformedURLException(e.getMessage());
0N/A exception.initCause(e);
0N/A throw exception;
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Returns true if specified string is a valid protocol name.
0N/A */
0N/A private boolean isValidProtocol(String protocol) {
0N/A int len = protocol.length();
0N/A if (len < 1)
0N/A return false;
0N/A char c = protocol.charAt(0);
0N/A if (!Character.isLetter(c))
0N/A return false;
0N/A for (int i = 1; i < len; i++) {
0N/A c = protocol.charAt(i);
0N/A if (!Character.isLetterOrDigit(c) && c != '.' && c != '+' &&
0N/A c != '-') {
0N/A return false;
0N/A }
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A /*
0N/A * Checks for permission to specify a stream handler.
0N/A */
0N/A private void checkSpecifyHandler(SecurityManager sm) {
0N/A sm.checkPermission(SecurityConstants.SPECIFY_HANDLER_PERMISSION);
0N/A }
0N/A
0N/A /**
0N/A * Sets the fields of the URL. This is not a public method so that
0N/A * only URLStreamHandlers can modify URL fields. URLs are
0N/A * otherwise constant.
0N/A *
0N/A * @param protocol the name of the protocol to use
0N/A * @param host the name of the host
0N/A @param port the port number on the host
0N/A * @param file the file on the host
0N/A * @param ref the internal reference in the URL
0N/A */
0N/A protected void set(String protocol, String host,
0N/A int port, String file, String ref) {
0N/A synchronized (this) {
0N/A this.protocol = protocol;
0N/A this.host = host;
0N/A authority = port == -1 ? host : host + ":" + port;
0N/A this.port = port;
0N/A this.file = file;
0N/A this.ref = ref;
0N/A /* This is very important. We must recompute this after the
0N/A * URL has been changed. */
0N/A hashCode = -1;
0N/A hostAddress = null;
0N/A int q = file.lastIndexOf('?');
0N/A if (q != -1) {
0N/A query = file.substring(q+1);
0N/A path = file.substring(0, q);
0N/A } else
0N/A path = file;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Sets the specified 8 fields of the URL. This is not a public method so
0N/A * that only URLStreamHandlers can modify URL fields. URLs are otherwise
0N/A * constant.
0N/A *
0N/A * @param protocol the name of the protocol to use
0N/A * @param host the name of the host
0N/A * @param port the port number on the host
0N/A * @param authority the authority part for the url
0N/A * @param userInfo the username and password
0N/A * @param path the file on the host
0N/A * @param ref the internal reference in the URL
0N/A * @param query the query part of this URL
0N/A * @since 1.3
0N/A */
0N/A protected void set(String protocol, String host, int port,
0N/A String authority, String userInfo, String path,
0N/A String query, String ref) {
0N/A synchronized (this) {
0N/A this.protocol = protocol;
0N/A this.host = host;
0N/A this.port = port;
0N/A this.file = query == null ? path : path + "?" + query;
0N/A this.userInfo = userInfo;
0N/A this.path = path;
0N/A this.ref = ref;
0N/A /* This is very important. We must recompute this after the
0N/A * URL has been changed. */
0N/A hashCode = -1;
0N/A hostAddress = null;
0N/A this.query = query;
0N/A this.authority = authority;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Gets the query part of this <code>URL</code>.
0N/A *
0N/A * @return the query part of this <code>URL</code>,
0N/A * or <CODE>null</CODE> if one does not exist
0N/A * @since 1.3
0N/A */
0N/A public String getQuery() {
0N/A return query;
0N/A }
0N/A
0N/A /**
0N/A * Gets the path part of this <code>URL</code>.
0N/A *
0N/A * @return the path part of this <code>URL</code>, or an
0N/A * empty string if one does not exist
0N/A * @since 1.3
0N/A */
0N/A public String getPath() {
0N/A return path;
0N/A }
0N/A
0N/A /**
0N/A * Gets the userInfo part of this <code>URL</code>.
0N/A *
0N/A * @return the userInfo part of this <code>URL</code>, or
0N/A * <CODE>null</CODE> if one does not exist
0N/A * @since 1.3
0N/A */
0N/A public String getUserInfo() {
0N/A return userInfo;
0N/A }
0N/A
0N/A /**
0N/A * Gets the authority part of this <code>URL</code>.
0N/A *
0N/A * @return the authority part of this <code>URL</code>
0N/A * @since 1.3
0N/A */
0N/A public String getAuthority() {
0N/A return authority;
0N/A }
0N/A
0N/A /**
0N/A * Gets the port number of this <code>URL</code>.
0N/A *
0N/A * @return the port number, or -1 if the port is not set
0N/A */
0N/A public int getPort() {
0N/A return port;
0N/A }
0N/A
0N/A /**
0N/A * Gets the default port number of the protocol associated
0N/A * with this <code>URL</code>. If the URL scheme or the URLStreamHandler
0N/A * for the URL do not define a default port number,
0N/A * then -1 is returned.
0N/A *
0N/A * @return the port number
0N/A * @since 1.4
0N/A */
0N/A public int getDefaultPort() {
0N/A return handler.getDefaultPort();
0N/A }
0N/A
0N/A /**
0N/A * Gets the protocol name of this <code>URL</code>.
0N/A *
0N/A * @return the protocol of this <code>URL</code>.
0N/A */
0N/A public String getProtocol() {
0N/A return protocol;
0N/A }
0N/A
0N/A /**
0N/A * Gets the host name of this <code>URL</code>, if applicable.
0N/A * The format of the host conforms to RFC 2732, i.e. for a
0N/A * literal IPv6 address, this method will return the IPv6 address
0N/A * enclosed in square brackets (<tt>'['</tt> and <tt>']'</tt>).
0N/A *
0N/A * @return the host name of this <code>URL</code>.
0N/A */
0N/A public String getHost() {
0N/A return host;
0N/A }
0N/A
0N/A /**
0N/A * Gets the file name of this <code>URL</code>.
0N/A * The returned file portion will be
0N/A * the same as <CODE>getPath()</CODE>, plus the concatenation of
0N/A * the value of <CODE>getQuery()</CODE>, if any. If there is
0N/A * no query portion, this method and <CODE>getPath()</CODE> will
0N/A * return identical results.
0N/A *
0N/A * @return the file name of this <code>URL</code>,
0N/A * or an empty string if one does not exist
0N/A */
0N/A public String getFile() {
0N/A return file;
0N/A }
0N/A
0N/A /**
0N/A * Gets the anchor (also known as the "reference") of this
0N/A * <code>URL</code>.
0N/A *
0N/A * @return the anchor (also known as the "reference") of this
0N/A * <code>URL</code>, or <CODE>null</CODE> if one does not exist
0N/A */
0N/A public String getRef() {
0N/A return ref;
0N/A }
0N/A
0N/A /**
0N/A * Compares this URL for equality with another object.<p>
0N/A *
0N/A * If the given object is not a URL then this method immediately returns
0N/A * <code>false</code>.<p>
0N/A *
0N/A * Two URL objects are equal if they have the same protocol, reference
0N/A * equivalent hosts, have the same port number on the host, and the same
0N/A * file and fragment of the file.<p>
0N/A *
0N/A * Two hosts are considered equivalent if both host names can be resolved
0N/A * into the same IP addresses; else if either host name can't be
0N/A * resolved, the host names must be equal without regard to case; or both
0N/A * host names equal to null.<p>
0N/A *
0N/A * Since hosts comparison requires name resolution, this operation is a
0N/A * blocking operation. <p>
0N/A *
0N/A * Note: The defined behavior for <code>equals</code> is known to
0N/A * be inconsistent with virtual hosting in HTTP.
0N/A *
0N/A * @param obj the URL to compare against.
0N/A * @return <code>true</code> if the objects are the same;
0N/A * <code>false</code> otherwise.
0N/A */
0N/A public boolean equals(Object obj) {
0N/A if (!(obj instanceof URL))
0N/A return false;
0N/A URL u2 = (URL)obj;
0N/A
0N/A return handler.equals(this, u2);
0N/A }
0N/A
0N/A /**
0N/A * Creates an integer suitable for hash table indexing.<p>
0N/A *
0N/A * The hash code is based upon all the URL components relevant for URL
0N/A * comparison. As such, this operation is a blocking operation.<p>
0N/A *
0N/A * @return a hash code for this <code>URL</code>.
0N/A */
0N/A public synchronized int hashCode() {
0N/A if (hashCode != -1)
0N/A return hashCode;
0N/A
0N/A hashCode = handler.hashCode(this);
0N/A return hashCode;
0N/A }
0N/A
0N/A /**
0N/A * Compares two URLs, excluding the fragment component.<p>
0N/A *
0N/A * Returns <code>true</code> if this <code>URL</code> and the
0N/A * <code>other</code> argument are equal without taking the
0N/A * fragment component into consideration.
0N/A *
0N/A * @param other the <code>URL</code> to compare against.
0N/A * @return <code>true</code> if they reference the same remote object;
0N/A * <code>false</code> otherwise.
0N/A */
0N/A public boolean sameFile(URL other) {
0N/A return handler.sameFile(this, other);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a string representation of this <code>URL</code>. The
0N/A * string is created by calling the <code>toExternalForm</code>
0N/A * method of the stream protocol handler for this object.
0N/A *
0N/A * @return a string representation of this object.
0N/A * @see java.net.URL#URL(java.lang.String, java.lang.String, int,
0N/A * java.lang.String)
0N/A * @see java.net.URLStreamHandler#toExternalForm(java.net.URL)
0N/A */
0N/A public String toString() {
0N/A return toExternalForm();
0N/A }
0N/A
0N/A /**
0N/A * Constructs a string representation of this <code>URL</code>. The
0N/A * string is created by calling the <code>toExternalForm</code>
0N/A * method of the stream protocol handler for this object.
0N/A *
0N/A * @return a string representation of this object.
0N/A * @see java.net.URL#URL(java.lang.String, java.lang.String,
0N/A * int, java.lang.String)
0N/A * @see java.net.URLStreamHandler#toExternalForm(java.net.URL)
0N/A */
0N/A public String toExternalForm() {
0N/A return handler.toExternalForm(this);
0N/A }
0N/A
0N/A /**
0N/A * Returns a {@link java.net.URI} equivalent to this URL.
0N/A * This method functions in the same way as <code>new URI (this.toString())</code>.
0N/A * <p>Note, any URL instance that complies with RFC 2396 can be converted
0N/A * to a URI. However, some URLs that are not strictly in compliance
0N/A * can not be converted to a URI.
0N/A *
0N/A * @exception URISyntaxException if this URL is not formatted strictly according to
0N/A * to RFC2396 and cannot be converted to a URI.
0N/A *
0N/A * @return a URI instance equivalent to this URL.
0N/A * @since 1.5
0N/A */
0N/A public URI toURI() throws URISyntaxException {
0N/A return new URI (toString());
0N/A }
0N/A
0N/A /**
0N/A * Returns a {@link java.net.URLConnection URLConnection} instance that
0N/A * represents a connection to the remote object referred to by the
0N/A * {@code URL}.
0N/A *
0N/A * <P>A new instance of {@linkplain java.net.URLConnection URLConnection} is
0N/A * created every time when invoking the
0N/A * {@linkplain java.net.URLStreamHandler#openConnection(URL)
0N/A * URLStreamHandler.openConnection(URL)} method of the protocol handler for
0N/A * this URL.</P>
0N/A *
0N/A * <P>It should be noted that a URLConnection instance does not establish
0N/A * the actual network connection on creation. This will happen only when
0N/A * calling {@linkplain java.net.URLConnection#connect() URLConnection.connect()}.</P>
0N/A *
0N/A * <P>If for the URL's protocol (such as HTTP or JAR), there
0N/A * exists a public, specialized URLConnection subclass belonging
0N/A * to one of the following packages or one of their subpackages:
0N/A * java.lang, java.io, java.util, java.net, the connection
0N/A * returned will be of that subclass. For example, for HTTP an
0N/A * HttpURLConnection will be returned, and for JAR a
0N/A * JarURLConnection will be returned.</P>
0N/A *
0N/A * @return a {@link java.net.URLConnection URLConnection} linking
0N/A * to the URL.
0N/A * @exception IOException if an I/O exception occurs.
0N/A * @see java.net.URL#URL(java.lang.String, java.lang.String,
0N/A * int, java.lang.String)
0N/A */
0N/A public URLConnection openConnection() throws java.io.IOException {
0N/A return handler.openConnection(this);
0N/A }
0N/A
0N/A /**
0N/A * Same as {@link #openConnection()}, except that the connection will be
0N/A * made through the specified proxy; Protocol handlers that do not
0N/A * support proxing will ignore the proxy parameter and make a
0N/A * normal connection.
0N/A *
0N/A * Invoking this method preempts the system's default ProxySelector
0N/A * settings.
0N/A *
0N/A * @param proxy the Proxy through which this connection
0N/A * will be made. If direct connection is desired,
0N/A * Proxy.NO_PROXY should be specified.
0N/A * @return a <code>URLConnection</code> to the URL.
0N/A * @exception IOException if an I/O exception occurs.
0N/A * @exception SecurityException if a security manager is present
0N/A * and the caller doesn't have permission to connect
0N/A * to the proxy.
0N/A * @exception IllegalArgumentException will be thrown if proxy is null,
0N/A * or proxy has the wrong type
0N/A * @exception UnsupportedOperationException if the subclass that
0N/A * implements the protocol handler doesn't support
0N/A * this method.
0N/A * @see java.net.URL#URL(java.lang.String, java.lang.String,
0N/A * int, java.lang.String)
0N/A * @see java.net.URLConnection
0N/A * @see java.net.URLStreamHandler#openConnection(java.net.URL,
0N/A * java.net.Proxy)
0N/A * @since 1.5
0N/A */
0N/A public URLConnection openConnection(Proxy proxy)
0N/A throws java.io.IOException {
0N/A if (proxy == null) {
0N/A throw new IllegalArgumentException("proxy can not be null");
0N/A }
0N/A
1495N/A // Create a copy of Proxy as a security measure
1503N/A Proxy p = proxy == Proxy.NO_PROXY ? Proxy.NO_PROXY : sun.net.ApplicationProxy.create(proxy);
0N/A SecurityManager sm = System.getSecurityManager();
1495N/A if (p.type() != Proxy.Type.DIRECT && sm != null) {
1495N/A InetSocketAddress epoint = (InetSocketAddress) p.address();
0N/A if (epoint.isUnresolved())
0N/A sm.checkConnect(epoint.getHostName(), epoint.getPort());
0N/A else
0N/A sm.checkConnect(epoint.getAddress().getHostAddress(),
0N/A epoint.getPort());
0N/A }
1495N/A return handler.openConnection(this, p);
0N/A }
0N/A
0N/A /**
0N/A * Opens a connection to this <code>URL</code> and returns an
0N/A * <code>InputStream</code> for reading from that connection. This
0N/A * method is a shorthand for:
0N/A * <blockquote><pre>
0N/A * openConnection().getInputStream()
0N/A * </pre></blockquote>
0N/A *
0N/A * @return an input stream for reading from the URL connection.
0N/A * @exception IOException if an I/O exception occurs.
0N/A * @see java.net.URL#openConnection()
0N/A * @see java.net.URLConnection#getInputStream()
0N/A */
0N/A public final InputStream openStream() throws java.io.IOException {
0N/A return openConnection().getInputStream();
0N/A }
0N/A
0N/A /**
0N/A * Gets the contents of this URL. This method is a shorthand for:
0N/A * <blockquote><pre>
0N/A * openConnection().getContent()
0N/A * </pre></blockquote>
0N/A *
0N/A * @return the contents of this URL.
0N/A * @exception IOException if an I/O exception occurs.
0N/A * @see java.net.URLConnection#getContent()
0N/A */
0N/A public final Object getContent() throws java.io.IOException {
0N/A return openConnection().getContent();
0N/A }
0N/A
0N/A /**
0N/A * Gets the contents of this URL. This method is a shorthand for:
0N/A * <blockquote><pre>
0N/A * openConnection().getContent(Class[])
0N/A * </pre></blockquote>
0N/A *
0N/A * @param classes an array of Java types
0N/A * @return the content object of this URL that is the first match of
0N/A * the types specified in the classes array.
0N/A * null if none of the requested types are supported.
0N/A * @exception IOException if an I/O exception occurs.
0N/A * @see java.net.URLConnection#getContent(Class[])
0N/A * @since 1.3
0N/A */
0N/A public final Object getContent(Class[] classes)
0N/A throws java.io.IOException {
0N/A return openConnection().getContent(classes);
0N/A }
0N/A
0N/A /**
0N/A * The URLStreamHandler factory.
0N/A */
0N/A static URLStreamHandlerFactory factory;
0N/A
0N/A /**
0N/A * Sets an application's <code>URLStreamHandlerFactory</code>.
0N/A * This method can be called at most once in a given Java Virtual
0N/A * Machine.
0N/A *
0N/A *<p> The <code>URLStreamHandlerFactory</code> instance is used to
0N/A *construct a stream protocol handler from a protocol name.
0N/A *
0N/A * <p> If there is a security manager, this method first calls
0N/A * the security manager's <code>checkSetFactory</code> method
0N/A * to ensure the operation is allowed.
0N/A * This could result in a SecurityException.
0N/A *
0N/A * @param fac the desired factory.
0N/A * @exception Error if the application has already set a factory.
0N/A * @exception SecurityException if a security manager exists and its
0N/A * <code>checkSetFactory</code> method doesn't allow
0N/A * the operation.
0N/A * @see java.net.URL#URL(java.lang.String, java.lang.String,
0N/A * int, java.lang.String)
0N/A * @see java.net.URLStreamHandlerFactory
0N/A * @see SecurityManager#checkSetFactory
0N/A */
0N/A public static void setURLStreamHandlerFactory(URLStreamHandlerFactory fac) {
0N/A synchronized (streamHandlerLock) {
0N/A if (factory != null) {
0N/A throw new Error("factory already defined");
0N/A }
0N/A SecurityManager security = System.getSecurityManager();
0N/A if (security != null) {
0N/A security.checkSetFactory();
0N/A }
0N/A handlers.clear();
0N/A factory = fac;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * A table of protocol handlers.
0N/A */
0N/A static Hashtable handlers = new Hashtable();
0N/A private static Object streamHandlerLock = new Object();
0N/A
5426N/A // special case the gopher protocol, disabled by default
5426N/A private static final String GOPHER = "gopher";
5426N/A private static final String ENABLE_GOPHER_PROP = "jdk.net.registerGopherProtocol";
5426N/A private static final boolean enableGopher = AccessController.doPrivileged(
5426N/A new PrivilegedAction<Boolean>() {
5426N/A @Override
5426N/A public Boolean run() {
5426N/A String prop = System.getProperty(ENABLE_GOPHER_PROP);
5426N/A return prop == null ? false :
5426N/A (prop.equalsIgnoreCase("false") ? false : true);
5426N/A }
5426N/A });
5426N/A
5426N/A // package name of the JDK implementation protocol handlers
5426N/A private static final String JDK_PACKAGE_PREFIX = "sun.net.www.protocol";
5426N/A
0N/A /**
0N/A * Returns the Stream Handler.
0N/A * @param protocol the protocol to use
0N/A */
0N/A static URLStreamHandler getURLStreamHandler(String protocol) {
0N/A
0N/A URLStreamHandler handler = (URLStreamHandler)handlers.get(protocol);
0N/A if (handler == null) {
0N/A
0N/A boolean checkedWithFactory = false;
0N/A
0N/A // Use the factory (if any)
0N/A if (factory != null) {
0N/A handler = factory.createURLStreamHandler(protocol);
0N/A checkedWithFactory = true;
0N/A }
0N/A
0N/A // Try java protocol handler
0N/A if (handler == null) {
0N/A String packagePrefixList = null;
0N/A
0N/A packagePrefixList
0N/A = java.security.AccessController.doPrivileged(
0N/A new sun.security.action.GetPropertyAction(
0N/A protocolPathProp,""));
0N/A if (packagePrefixList != "") {
0N/A packagePrefixList += "|";
0N/A }
0N/A
0N/A // REMIND: decide whether to allow the "null" class prefix
0N/A // or not.
5426N/A packagePrefixList += JDK_PACKAGE_PREFIX;
0N/A
0N/A StringTokenizer packagePrefixIter =
0N/A new StringTokenizer(packagePrefixList, "|");
0N/A
0N/A while (handler == null &&
0N/A packagePrefixIter.hasMoreTokens()) {
0N/A
0N/A String packagePrefix =
0N/A packagePrefixIter.nextToken().trim();
5426N/A
5426N/A // do not try to instantiate the JDK gopher handler
5426N/A // unless the system property had been explicitly set
5426N/A if (protocol.equalsIgnoreCase(GOPHER) &&
5426N/A packagePrefix.equals(JDK_PACKAGE_PREFIX) &&
5426N/A !enableGopher) {
5426N/A continue;
5426N/A }
0N/A try {
0N/A String clsName = packagePrefix + "." + protocol +
0N/A ".Handler";
0N/A Class cls = null;
0N/A try {
0N/A cls = Class.forName(clsName);
0N/A } catch (ClassNotFoundException e) {
0N/A ClassLoader cl = ClassLoader.getSystemClassLoader();
0N/A if (cl != null) {
0N/A cls = cl.loadClass(clsName);
0N/A }
0N/A }
0N/A if (cls != null) {
0N/A handler =
0N/A (URLStreamHandler)cls.newInstance();
0N/A }
0N/A } catch (Exception e) {
0N/A // any number of exceptions can get thrown here
0N/A }
0N/A }
0N/A }
0N/A
0N/A synchronized (streamHandlerLock) {
0N/A
0N/A URLStreamHandler handler2 = null;
0N/A
0N/A // Check again with hashtable just in case another
0N/A // thread created a handler since we last checked
0N/A handler2 = (URLStreamHandler)handlers.get(protocol);
0N/A
0N/A if (handler2 != null) {
0N/A return handler2;
0N/A }
0N/A
0N/A // Check with factory if another thread set a
0N/A // factory since our last check
0N/A if (!checkedWithFactory && factory != null) {
0N/A handler2 = factory.createURLStreamHandler(protocol);
0N/A }
0N/A
0N/A if (handler2 != null) {
0N/A // The handler from the factory must be given more
0N/A // importance. Discard the default handler that
0N/A // this thread created.
0N/A handler = handler2;
0N/A }
0N/A
0N/A // Insert this handler into the hashtable
0N/A if (handler != null) {
0N/A handlers.put(protocol, handler);
0N/A }
0N/A
0N/A }
0N/A }
0N/A
0N/A return handler;
0N/A
0N/A }
0N/A
0N/A /**
0N/A * WriteObject is called to save the state of the URL to an
0N/A * ObjectOutputStream. The handler is not saved since it is
0N/A * specific to this system.
0N/A *
0N/A * @serialData the default write object value. When read back in,
0N/A * the reader must ensure that calling getURLStreamHandler with
0N/A * the protocol variable returns a valid URLStreamHandler and
0N/A * throw an IOException if it does not.
0N/A */
0N/A private synchronized void writeObject(java.io.ObjectOutputStream s)
0N/A throws IOException
0N/A {
0N/A s.defaultWriteObject(); // write the fields
0N/A }
0N/A
0N/A /**
0N/A * readObject is called to restore the state of the URL from the
0N/A * stream. It reads the components of the URL and finds the local
0N/A * stream handler.
0N/A */
0N/A private synchronized void readObject(java.io.ObjectInputStream s)
0N/A throws IOException, ClassNotFoundException
0N/A {
0N/A s.defaultReadObject(); // read the fields
0N/A if ((handler = getURLStreamHandler(protocol)) == null) {
0N/A throw new IOException("unknown protocol: " + protocol);
0N/A }
0N/A
0N/A // Construct authority part
0N/A if (authority == null &&
0N/A ((host != null && host.length() > 0) || port != -1)) {
0N/A if (host == null)
0N/A host = "";
0N/A authority = (port == -1) ? host : host + ":" + port;
0N/A
0N/A // Handle hosts with userInfo in them
0N/A int at = host.lastIndexOf('@');
0N/A if (at != -1) {
0N/A userInfo = host.substring(0, at);
0N/A host = host.substring(at+1);
0N/A }
0N/A } else if (authority != null) {
0N/A // Construct user info part
0N/A int ind = authority.indexOf('@');
0N/A if (ind != -1)
0N/A userInfo = authority.substring(0, ind);
0N/A }
0N/A
0N/A // Construct path and query part
0N/A path = null;
0N/A query = null;
0N/A if (file != null) {
0N/A // Fix: only do this if hierarchical?
0N/A int q = file.lastIndexOf('?');
0N/A if (q != -1) {
0N/A query = file.substring(q+1);
0N/A path = file.substring(0, q);
0N/A } else
0N/A path = file;
0N/A }
0N/A }
0N/A}
0N/A
0N/Aclass Parts {
0N/A String path, query, ref;
0N/A
0N/A Parts(String file) {
0N/A int ind = file.indexOf('#');
0N/A ref = ind < 0 ? null: file.substring(ind + 1);
0N/A file = ind < 0 ? file: file.substring(0, ind);
0N/A int q = file.lastIndexOf('?');
0N/A if (q != -1) {
0N/A query = file.substring(q+1);
0N/A path = file.substring(0, q);
0N/A } else {
0N/A path = file;
0N/A }
0N/A }
0N/A
0N/A String getPath() {
0N/A return path;
0N/A }
0N/A
0N/A String getQuery() {
0N/A return query;
0N/A }
0N/A
0N/A String getRef() {
0N/A return ref;
0N/A }
0N/A}