1 /*
   2  * Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.net;
  27 
  28 import java.io.IOException;
  29 import java.io.InputStream;
  30 import java.net.spi.URLStreamHandlerProvider;
  31 import java.security.AccessController;
  32 import java.security.PrivilegedAction;
  33 import java.util.Hashtable;
  34 import java.util.Iterator;
  35 import java.util.NoSuchElementException;
  36 import java.util.ServiceConfigurationError;
  37 import java.util.ServiceLoader;
  38 
  39 import sun.security.util.SecurityConstants;
  40 
  41 /**
  42  * Class {@code URL} represents a Uniform Resource
  43  * Locator, a pointer to a "resource" on the World
  44  * Wide Web. A resource can be something as simple as a file or a
  45  * directory, or it can be a reference to a more complicated object,
  46  * such as a query to a database or to a search engine. More
  47  * information on the types of URLs and their formats can be found at:
  48  * <a href=
  49  * "http://web.archive.org/web/20051219043731/http://archive.ncsa.uiuc.edu/SDG/Software/Mosaic/Demo/url-primer.html">
  50  * <i>Types of URL</i></a>
  51  * <p>
  52  * In general, a URL can be broken into several parts. Consider the
  53  * following example:
  54  * <blockquote><pre>
  55  *     http://www.example.com/docs/resource1.html
  56  * </pre></blockquote>
  57  * <p>
  58  * The URL above indicates that the protocol to use is
  59  * {@code http} (HyperText Transfer Protocol) and that the
  60  * information resides on a host machine named
  61  * {@code www.example.com}. The information on that host
  62  * machine is named {@code /docs/resource1.html}. The exact
  63  * meaning of this name on the host machine is both protocol
  64  * dependent and host dependent. The information normally resides in
  65  * a file, but it could be generated on the fly. This component of
  66  * the URL is called the <i>path</i> component.
  67  * <p>
  68  * A URL can optionally specify a "port", which is the
  69  * port number to which the TCP connection is made on the remote host
  70  * machine. If the port is not specified, the default port for
  71  * the protocol is used instead. For example, the default port for
  72  * {@code http} is {@code 80}. An alternative port could be
  73  * specified as:
  74  * <blockquote><pre>
  75  *     http://www.example.com:1080/docs/resource1.html
  76  * </pre></blockquote>
  77  * <p>
  78  * The syntax of {@code URL} is defined by  <a
  79  * href="http://www.ietf.org/rfc/rfc2396.txt"><i>RFC&nbsp;2396: Uniform
  80  * Resource Identifiers (URI): Generic Syntax</i></a>, amended by <a
  81  * href="http://www.ietf.org/rfc/rfc2732.txt"><i>RFC&nbsp;2732: Format for
  82  * Literal IPv6 Addresses in URLs</i></a>. The Literal IPv6 address format
  83  * also supports scope_ids. The syntax and usage of scope_ids is described
  84  * <a href="Inet6Address.html#scoped">here</a>.
  85  * <p>
  86  * A URL may have appended to it a "fragment", also known
  87  * as a "ref" or a "reference". The fragment is indicated by the sharp
  88  * sign character "#" followed by more characters. For example,
  89  * <blockquote><pre>
  90  *     http://java.sun.com/index.html#chapter1
  91  * </pre></blockquote>
  92  * <p>
  93  * This fragment is not technically part of the URL. Rather, it
  94  * indicates that after the specified resource is retrieved, the
  95  * application is specifically interested in that part of the
  96  * document that has the tag {@code chapter1} attached to it. The
  97  * meaning of a tag is resource specific.
  98  * <p>
  99  * An application can also specify a "relative URL",
 100  * which contains only enough information to reach the resource
 101  * relative to another URL. Relative URLs are frequently used within
 102  * HTML pages. For example, if the contents of the URL:
 103  * <blockquote><pre>
 104  *     http://java.sun.com/index.html
 105  * </pre></blockquote>
 106  * contained within it the relative URL:
 107  * <blockquote><pre>
 108  *     FAQ.html
 109  * </pre></blockquote>
 110  * it would be a shorthand for:
 111  * <blockquote><pre>
 112  *     http://java.sun.com/FAQ.html
 113  * </pre></blockquote>
 114  * <p>
 115  * The relative URL need not specify all the components of a URL. If
 116  * the protocol, host name, or port number is missing, the value is
 117  * inherited from the fully specified URL. The file component must be
 118  * specified. The optional fragment is not inherited.
 119  * <p>
 120  * The URL class does not itself encode or decode any URL components
 121  * according to the escaping mechanism defined in RFC2396. It is the
 122  * responsibility of the caller to encode any fields, which need to be
 123  * escaped prior to calling URL, and also to decode any escaped fields,
 124  * that are returned from URL. Furthermore, because URL has no knowledge
 125  * of URL escaping, it does not recognise equivalence between the encoded
 126  * or decoded form of the same URL. For example, the two URLs:<br>
 127  * <pre>    http://foo.com/hello world/ and http://foo.com/hello%20world</pre>
 128  * would be considered not equal to each other.
 129  * <p>
 130  * Note, the {@link java.net.URI} class does perform escaping of its
 131  * component fields in certain circumstances. The recommended way
 132  * to manage the encoding and decoding of URLs is to use {@link java.net.URI},
 133  * and to convert between these two classes using {@link #toURI()} and
 134  * {@link URI#toURL()}.
 135  * <p>
 136  * The {@link URLEncoder} and {@link URLDecoder} classes can also be
 137  * used, but only for HTML form encoding, which is not the same
 138  * as the encoding scheme defined in RFC2396.
 139  *
 140  * @author  James Gosling
 141  * @since 1.0
 142  */
 143 public final class URL implements java.io.Serializable {
 144 
 145     static final long serialVersionUID = -7627629688361524110L;
 146 
 147     /**
 148      * The property which specifies the package prefix list to be scanned
 149      * for protocol handlers.  The value of this property (if any) should
 150      * be a vertical bar delimited list of package names to search through
 151      * for a protocol handler to load.  The policy of this class is that
 152      * all protocol handlers will be in a class called <protocolname>.Handler,
 153      * and each package in the list is examined in turn for a matching
 154      * handler.  If none are found (or the property is not specified), the
 155      * default package prefix, sun.net.www.protocol, is used.  The search
 156      * proceeds from the first package in the list to the last and stops
 157      * when a match is found.
 158      */
 159     private static final String protocolPathProp = "java.protocol.handler.pkgs";
 160 
 161     /**
 162      * The protocol to use (ftp, http, nntp, ... etc.) .
 163      * @serial
 164      */
 165     private String protocol;
 166 
 167     /**
 168      * The host name to connect to.
 169      * @serial
 170      */
 171     private String host;
 172 
 173     /**
 174      * The protocol port to connect to.
 175      * @serial
 176      */
 177     private int port = -1;
 178 
 179     /**
 180      * The specified file name on that host. {@code file} is
 181      * defined as {@code path[?query]}
 182      * @serial
 183      */
 184     private String file;
 185 
 186     /**
 187      * The query part of this URL.
 188      */
 189     private transient String query;
 190 
 191     /**
 192      * The authority part of this URL.
 193      * @serial
 194      */
 195     private String authority;
 196 
 197     /**
 198      * The path part of this URL.
 199      */
 200     private transient String path;
 201 
 202     /**
 203      * The userinfo part of this URL.
 204      */
 205     private transient String userInfo;
 206 
 207     /**
 208      * # reference.
 209      * @serial
 210      */
 211     private String ref;
 212 
 213     /**
 214      * The host's IP address, used in equals and hashCode.
 215      * Computed on demand. An uninitialized or unknown hostAddress is null.
 216      */
 217     transient InetAddress hostAddress;
 218 
 219     /**
 220      * The URLStreamHandler for this URL.
 221      */
 222     transient URLStreamHandler handler;
 223 
 224     /* Our hash code.
 225      * @serial
 226      */
 227     private int hashCode = -1;
 228 
 229     /**
 230      * Creates a {@code URL} object from the specified
 231      * {@code protocol}, {@code host}, {@code port}
 232      * number, and {@code file}.<p>
 233      *
 234      * {@code host} can be expressed as a host name or a literal
 235      * IP address. If IPv6 literal address is used, it should be
 236      * enclosed in square brackets ({@code '['} and {@code ']'}), as
 237      * specified by <a
 238      * href="http://www.ietf.org/rfc/rfc2732.txt">RFC&nbsp;2732</a>;
 239      * However, the literal IPv6 address format defined in <a
 240      * href="http://www.ietf.org/rfc/rfc2373.txt"><i>RFC&nbsp;2373: IP
 241      * Version 6 Addressing Architecture</i></a> is also accepted.<p>
 242      *
 243      * Specifying a {@code port} number of {@code -1}
 244      * indicates that the URL should use the default port for the
 245      * protocol.<p>
 246      *
 247      * If this is the first URL object being created with the specified
 248      * protocol, a <i>stream protocol handler</i> object, an instance of
 249      * class {@code URLStreamHandler}, is created for that protocol:
 250      * <ol>
 251      * <li>If the application has previously set up an instance of
 252      *     {@code URLStreamHandlerFactory} as the stream handler factory,
 253      *     then the {@code createURLStreamHandler} method of that instance
 254      *     is called with the protocol string as an argument to create the
 255      *     stream protocol handler.
 256      * <li>If no {@code URLStreamHandlerFactory} has yet been set up,
 257      *     or if the factory's {@code createURLStreamHandler} method
 258      *     returns {@code null}, then the {@linkplain java.util.ServiceLoader
 259      *     ServiceLoader} mechanism is used to locate {@linkplain
 260      *     java.net.spi.URLStreamHandlerProvider URLStreamHandlerProvider}
 261      *     implementations using the system class
 262      *     loader. The order that providers are located is implementation
 263      *     specific, and an implementation is free to cache the located
 264      *     providers. A {@linkplain java.util.ServiceConfigurationError
 265      *     ServiceConfigurationError}, {@code Error} or {@code RuntimeException}
 266      *     thrown from the {@code createURLStreamHandler}, if encountered, will
 267      *     be propagated to the calling thread. The {@code
 268      *     createURLStreamHandler} method of each provider, if instantiated, is
 269      *     invoked, with the protocol string, until a provider returns non-null,
 270      *     or all providers have been exhausted.
 271      * <li>If the previous step fails to find a protocol handler, the
 272      *     constructor reads the value of the system property:
 273      *     <blockquote>{@code
 274      *         java.protocol.handler.pkgs
 275      *     }</blockquote>
 276      *     If the value of that system property is not {@code null},
 277      *     it is interpreted as a list of packages separated by a vertical
 278      *     slash character '{@code |}'. The constructor tries to load
 279      *     the class named:
 280      *     <blockquote>{@code
 281      *         <package>.<protocol>.Handler
 282      *     }</blockquote>
 283      *     where {@code <package>} is replaced by the name of the package
 284      *     and {@code <protocol>} is replaced by the name of the protocol.
 285      *     If this class does not exist, or if the class exists but it is not
 286      *     a subclass of {@code URLStreamHandler}, then the next package
 287      *     in the list is tried.
 288      * <li>If the previous step fails to find a protocol handler, then the
 289      *     constructor tries to load a built-in protocol handler.
 290      *     If this class does not exist, or if the class exists but it is not a
 291      *     subclass of {@code URLStreamHandler}, then a
 292      *     {@code MalformedURLException} is thrown.
 293      * </ol>
 294      *
 295      * <p>Protocol handlers for the following protocols are guaranteed
 296      * to exist on the search path :-
 297      * <blockquote><pre>
 298      *     http, https, file, and jar
 299      * </pre></blockquote>
 300      * Protocol handlers for additional protocols may also be  available.
 301      * Some protocol handlers, for example those used for loading platform
 302      * classes or classes on the class path, may not be overridden. The details
 303      * of such restrictions, and when those restrictions apply (during
 304      * initialization of the runtime for example), are implementation specific
 305      * and therefore not specified
 306      *
 307      * <p>No validation of the inputs is performed by this constructor.
 308      *
 309      * @param      protocol   the name of the protocol to use.
 310      * @param      host       the name of the host.
 311      * @param      port       the port number on the host.
 312      * @param      file       the file on the host
 313      * @exception  MalformedURLException  if an unknown protocol is specified.
 314      * @see        java.lang.System#getProperty(java.lang.String)
 315      * @see        java.net.URL#setURLStreamHandlerFactory(
 316      *                  java.net.URLStreamHandlerFactory)
 317      * @see        java.net.URLStreamHandler
 318      * @see        java.net.URLStreamHandlerFactory#createURLStreamHandler(
 319      *                  java.lang.String)
 320      */
 321     public URL(String protocol, String host, int port, String file)
 322         throws MalformedURLException
 323     {
 324         this(protocol, host, port, file, null);
 325     }
 326 
 327     /**
 328      * Creates a URL from the specified {@code protocol}
 329      * name, {@code host} name, and {@code file} name. The
 330      * default port for the specified protocol is used.
 331      * <p>
 332      * This method is equivalent to calling the four-argument
 333      * constructor with the arguments being {@code protocol},
 334      * {@code host}, {@code -1}, and {@code file}.
 335      *
 336      * No validation of the inputs is performed by this constructor.
 337      *
 338      * @param      protocol   the name of the protocol to use.
 339      * @param      host       the name of the host.
 340      * @param      file       the file on the host.
 341      * @exception  MalformedURLException  if an unknown protocol is specified.
 342      * @see        java.net.URL#URL(java.lang.String, java.lang.String,
 343      *                  int, java.lang.String)
 344      */
 345     public URL(String protocol, String host, String file)
 346             throws MalformedURLException {
 347         this(protocol, host, -1, file);
 348     }
 349 
 350     /**
 351      * Creates a {@code URL} object from the specified
 352      * {@code protocol}, {@code host}, {@code port}
 353      * number, {@code file}, and {@code handler}. Specifying
 354      * a {@code port} number of {@code -1} indicates that
 355      * the URL should use the default port for the protocol. Specifying
 356      * a {@code handler} of {@code null} indicates that the URL
 357      * should use a default stream handler for the protocol, as outlined
 358      * for:
 359      *     java.net.URL#URL(java.lang.String, java.lang.String, int,
 360      *                      java.lang.String)
 361      *
 362      * <p>If the handler is not null and there is a security manager,
 363      * the security manager's {@code checkPermission}
 364      * method is called with a
 365      * {@code NetPermission("specifyStreamHandler")} permission.
 366      * This may result in a SecurityException.
 367      *
 368      * No validation of the inputs is performed by this constructor.
 369      *
 370      * @param      protocol   the name of the protocol to use.
 371      * @param      host       the name of the host.
 372      * @param      port       the port number on the host.
 373      * @param      file       the file on the host
 374      * @param      handler    the stream handler for the URL.
 375      * @exception  MalformedURLException  if an unknown protocol is specified.
 376      * @exception  SecurityException
 377      *        if a security manager exists and its
 378      *        {@code checkPermission} method doesn't allow
 379      *        specifying a stream handler explicitly.
 380      * @see        java.lang.System#getProperty(java.lang.String)
 381      * @see        java.net.URL#setURLStreamHandlerFactory(
 382      *                  java.net.URLStreamHandlerFactory)
 383      * @see        java.net.URLStreamHandler
 384      * @see        java.net.URLStreamHandlerFactory#createURLStreamHandler(
 385      *                  java.lang.String)
 386      * @see        SecurityManager#checkPermission
 387      * @see        java.net.NetPermission
 388      */
 389     public URL(String protocol, String host, int port, String file,
 390                URLStreamHandler handler) throws MalformedURLException {
 391         if (handler != null) {
 392             SecurityManager sm = System.getSecurityManager();
 393             if (sm != null) {
 394                 // check for permission to specify a handler
 395                 checkSpecifyHandler(sm);
 396             }
 397         }
 398 
 399         protocol = protocol.toLowerCase();
 400         this.protocol = protocol;
 401         if (host != null) {
 402 
 403             /**
 404              * if host is a literal IPv6 address,
 405              * we will make it conform to RFC 2732
 406              */
 407             if (host.indexOf(':') >= 0 && !host.startsWith("[")) {
 408                 host = "["+host+"]";
 409             }
 410             this.host = host;
 411 
 412             if (port < -1) {
 413                 throw new MalformedURLException("Invalid port number :" +
 414                                                     port);
 415             }
 416             this.port = port;
 417             authority = (port == -1) ? host : host + ":" + port;
 418         }
 419 
 420         Parts parts = new Parts(file);
 421         path = parts.getPath();
 422         query = parts.getQuery();
 423 
 424         if (query != null) {
 425             this.file = path + "?" + query;
 426         } else {
 427             this.file = path;
 428         }
 429         ref = parts.getRef();
 430 
 431         // Note: we don't do validation of the URL here. Too risky to change
 432         // right now, but worth considering for future reference. -br
 433         if (handler == null &&
 434             (handler = getURLStreamHandler(protocol)) == null) {
 435             throw new MalformedURLException("unknown protocol: " + protocol);
 436         }
 437         this.handler = handler;
 438     }
 439 
 440     /**
 441      * Creates a {@code URL} object from the {@code String}
 442      * representation.
 443      * <p>
 444      * This constructor is equivalent to a call to the two-argument
 445      * constructor with a {@code null} first argument.
 446      *
 447      * @param      spec   the {@code String} to parse as a URL.
 448      * @exception  MalformedURLException  if no protocol is specified, or an
 449      *               unknown protocol is found, or {@code spec} is {@code null}.
 450      * @see        java.net.URL#URL(java.net.URL, java.lang.String)
 451      */
 452     public URL(String spec) throws MalformedURLException {
 453         this(null, spec);
 454     }
 455 
 456     /**
 457      * Creates a URL by parsing the given spec within a specified context.
 458      *
 459      * The new URL is created from the given context URL and the spec
 460      * argument as described in
 461      * RFC2396 &quot;Uniform Resource Identifiers : Generic * Syntax&quot; :
 462      * <blockquote><pre>
 463      *          &lt;scheme&gt;://&lt;authority&gt;&lt;path&gt;?&lt;query&gt;#&lt;fragment&gt;
 464      * </pre></blockquote>
 465      * The reference is parsed into the scheme, authority, path, query and
 466      * fragment parts. If the path component is empty and the scheme,
 467      * authority, and query components are undefined, then the new URL is a
 468      * reference to the current document. Otherwise, the fragment and query
 469      * parts present in the spec are used in the new URL.
 470      * <p>
 471      * If the scheme component is defined in the given spec and does not match
 472      * the scheme of the context, then the new URL is created as an absolute
 473      * URL based on the spec alone. Otherwise the scheme component is inherited
 474      * from the context URL.
 475      * <p>
 476      * If the authority component is present in the spec then the spec is
 477      * treated as absolute and the spec authority and path will replace the
 478      * context authority and path. If the authority component is absent in the
 479      * spec then the authority of the new URL will be inherited from the
 480      * context.
 481      * <p>
 482      * If the spec's path component begins with a slash character
 483      * &quot;/&quot; then the
 484      * path is treated as absolute and the spec path replaces the context path.
 485      * <p>
 486      * Otherwise, the path is treated as a relative path and is appended to the
 487      * context path, as described in RFC2396. Also, in this case,
 488      * the path is canonicalized through the removal of directory
 489      * changes made by occurrences of &quot;..&quot; and &quot;.&quot;.
 490      * <p>
 491      * For a more detailed description of URL parsing, refer to RFC2396.
 492      *
 493      * @param      context   the context in which to parse the specification.
 494      * @param      spec      the {@code String} to parse as a URL.
 495      * @exception  MalformedURLException  if no protocol is specified, or an
 496      *               unknown protocol is found, or {@code spec} is {@code null}.
 497      * @see        java.net.URL#URL(java.lang.String, java.lang.String,
 498      *                  int, java.lang.String)
 499      * @see        java.net.URLStreamHandler
 500      * @see        java.net.URLStreamHandler#parseURL(java.net.URL,
 501      *                  java.lang.String, int, int)
 502      */
 503     public URL(URL context, String spec) throws MalformedURLException {
 504         this(context, spec, null);
 505     }
 506 
 507     /**
 508      * Creates a URL by parsing the given spec with the specified handler
 509      * within a specified context. If the handler is null, the parsing
 510      * occurs as with the two argument constructor.
 511      *
 512      * @param      context   the context in which to parse the specification.
 513      * @param      spec      the {@code String} to parse as a URL.
 514      * @param      handler   the stream handler for the URL.
 515      * @exception  MalformedURLException  if no protocol is specified, or an
 516      *               unknown protocol is found, or {@code spec} is {@code null}.
 517      * @exception  SecurityException
 518      *        if a security manager exists and its
 519      *        {@code checkPermission} method doesn't allow
 520      *        specifying a stream handler.
 521      * @see        java.net.URL#URL(java.lang.String, java.lang.String,
 522      *                  int, java.lang.String)
 523      * @see        java.net.URLStreamHandler
 524      * @see        java.net.URLStreamHandler#parseURL(java.net.URL,
 525      *                  java.lang.String, int, int)
 526      */
 527     public URL(URL context, String spec, URLStreamHandler handler)
 528         throws MalformedURLException
 529     {
 530         String original = spec;
 531         int i, limit, c;
 532         int start = 0;
 533         String newProtocol = null;
 534         boolean aRef=false;
 535         boolean isRelative = false;
 536 
 537         // Check for permission to specify a handler
 538         if (handler != null) {
 539             SecurityManager sm = System.getSecurityManager();
 540             if (sm != null) {
 541                 checkSpecifyHandler(sm);
 542             }
 543         }
 544 
 545         try {
 546             limit = spec.length();
 547             while ((limit > 0) && (spec.charAt(limit - 1) <= ' ')) {
 548                 limit--;        //eliminate trailing whitespace
 549             }
 550             while ((start < limit) && (spec.charAt(start) <= ' ')) {
 551                 start++;        // eliminate leading whitespace
 552             }
 553 
 554             if (spec.regionMatches(true, start, "url:", 0, 4)) {
 555                 start += 4;
 556             }
 557             if (start < spec.length() && spec.charAt(start) == '#') {
 558                 /* we're assuming this is a ref relative to the context URL.
 559                  * This means protocols cannot start w/ '#', but we must parse
 560                  * ref URL's like: "hello:there" w/ a ':' in them.
 561                  */
 562                 aRef=true;
 563             }
 564             for (i = start ; !aRef && (i < limit) &&
 565                      ((c = spec.charAt(i)) != '/') ; i++) {
 566                 if (c == ':') {
 567 
 568                     String s = spec.substring(start, i).toLowerCase();
 569                     if (isValidProtocol(s)) {
 570                         newProtocol = s;
 571                         start = i + 1;
 572                     }
 573                     break;
 574                 }
 575             }
 576 
 577             // Only use our context if the protocols match.
 578             protocol = newProtocol;
 579             if ((context != null) && ((newProtocol == null) ||
 580                             newProtocol.equalsIgnoreCase(context.protocol))) {
 581                 // inherit the protocol handler from the context
 582                 // if not specified to the constructor
 583                 if (handler == null) {
 584                     handler = context.handler;
 585                 }
 586 
 587                 // If the context is a hierarchical URL scheme and the spec
 588                 // contains a matching scheme then maintain backwards
 589                 // compatibility and treat it as if the spec didn't contain
 590                 // the scheme; see 5.2.3 of RFC2396
 591                 if (context.path != null && context.path.startsWith("/"))
 592                     newProtocol = null;
 593 
 594                 if (newProtocol == null) {
 595                     protocol = context.protocol;
 596                     authority = context.authority;
 597                     userInfo = context.userInfo;
 598                     host = context.host;
 599                     port = context.port;
 600                     file = context.file;
 601                     path = context.path;
 602                     isRelative = true;
 603                 }
 604             }
 605 
 606             if (protocol == null) {
 607                 throw new MalformedURLException("no protocol: "+original);
 608             }
 609 
 610             // Get the protocol handler if not specified or the protocol
 611             // of the context could not be used
 612             if (handler == null &&
 613                 (handler = getURLStreamHandler(protocol)) == null) {
 614                 throw new MalformedURLException("unknown protocol: "+protocol);
 615             }
 616 
 617             this.handler = handler;
 618 
 619             i = spec.indexOf('#', start);
 620             if (i >= 0) {
 621                 ref = spec.substring(i + 1, limit);
 622                 limit = i;
 623             }
 624 
 625             /*
 626              * Handle special case inheritance of query and fragment
 627              * implied by RFC2396 section 5.2.2.
 628              */
 629             if (isRelative && start == limit) {
 630                 query = context.query;
 631                 if (ref == null) {
 632                     ref = context.ref;
 633                 }
 634             }
 635 
 636             handler.parseURL(this, spec, start, limit);
 637 
 638         } catch(MalformedURLException e) {
 639             throw e;
 640         } catch(Exception e) {
 641             MalformedURLException exception = new MalformedURLException(e.getMessage());
 642             exception.initCause(e);
 643             throw exception;
 644         }
 645     }
 646 
 647     /*
 648      * Returns true if specified string is a valid protocol name.
 649      */
 650     private boolean isValidProtocol(String protocol) {
 651         int len = protocol.length();
 652         if (len < 1)
 653             return false;
 654         char c = protocol.charAt(0);
 655         if (!Character.isLetter(c))
 656             return false;
 657         for (int i = 1; i < len; i++) {
 658             c = protocol.charAt(i);
 659             if (!Character.isLetterOrDigit(c) && c != '.' && c != '+' &&
 660                 c != '-') {
 661                 return false;
 662             }
 663         }
 664         return true;
 665     }
 666 
 667     /*
 668      * Checks for permission to specify a stream handler.
 669      */
 670     private void checkSpecifyHandler(SecurityManager sm) {
 671         sm.checkPermission(SecurityConstants.SPECIFY_HANDLER_PERMISSION);
 672     }
 673 
 674     /**
 675      * Sets the fields of the URL. This is not a public method so that
 676      * only URLStreamHandlers can modify URL fields. URLs are
 677      * otherwise constant.
 678      *
 679      * @param protocol the name of the protocol to use
 680      * @param host the name of the host
 681        @param port the port number on the host
 682      * @param file the file on the host
 683      * @param ref the internal reference in the URL
 684      */
 685     void set(String protocol, String host, int port,
 686              String file, String ref) {
 687         synchronized (this) {
 688             this.protocol = protocol;
 689             this.host = host;
 690             authority = port == -1 ? host : host + ":" + port;
 691             this.port = port;
 692             this.file = file;
 693             this.ref = ref;
 694             /* This is very important. We must recompute this after the
 695              * URL has been changed. */
 696             hashCode = -1;
 697             hostAddress = null;
 698             int q = file.lastIndexOf('?');
 699             if (q != -1) {
 700                 query = file.substring(q+1);
 701                 path = file.substring(0, q);
 702             } else
 703                 path = file;
 704         }
 705     }
 706 
 707     /**
 708      * Sets the specified 8 fields of the URL. This is not a public method so
 709      * that only URLStreamHandlers can modify URL fields. URLs are otherwise
 710      * constant.
 711      *
 712      * @param protocol the name of the protocol to use
 713      * @param host the name of the host
 714      * @param port the port number on the host
 715      * @param authority the authority part for the url
 716      * @param userInfo the username and password
 717      * @param path the file on the host
 718      * @param ref the internal reference in the URL
 719      * @param query the query part of this URL
 720      * @since 1.3
 721      */
 722     void set(String protocol, String host, int port,
 723              String authority, String userInfo, String path,
 724              String query, String ref) {
 725         synchronized (this) {
 726             this.protocol = protocol;
 727             this.host = host;
 728             this.port = port;
 729             this.file = query == null ? path : path + "?" + query;
 730             this.userInfo = userInfo;
 731             this.path = path;
 732             this.ref = ref;
 733             /* This is very important. We must recompute this after the
 734              * URL has been changed. */
 735             hashCode = -1;
 736             hostAddress = null;
 737             this.query = query;
 738             this.authority = authority;
 739         }
 740     }
 741 
 742     /**
 743      * Gets the query part of this {@code URL}.
 744      *
 745      * @return  the query part of this {@code URL},
 746      * or <CODE>null</CODE> if one does not exist
 747      * @since 1.3
 748      */
 749     public String getQuery() {
 750         return query;
 751     }
 752 
 753     /**
 754      * Gets the path part of this {@code URL}.
 755      *
 756      * @return  the path part of this {@code URL}, or an
 757      * empty string if one does not exist
 758      * @since 1.3
 759      */
 760     public String getPath() {
 761         return path;
 762     }
 763 
 764     /**
 765      * Gets the userInfo part of this {@code URL}.
 766      *
 767      * @return  the userInfo part of this {@code URL}, or
 768      * <CODE>null</CODE> if one does not exist
 769      * @since 1.3
 770      */
 771     public String getUserInfo() {
 772         return userInfo;
 773     }
 774 
 775     /**
 776      * Gets the authority part of this {@code URL}.
 777      *
 778      * @return  the authority part of this {@code URL}
 779      * @since 1.3
 780      */
 781     public String getAuthority() {
 782         return authority;
 783     }
 784 
 785     /**
 786      * Gets the port number of this {@code URL}.
 787      *
 788      * @return  the port number, or -1 if the port is not set
 789      */
 790     public int getPort() {
 791         return port;
 792     }
 793 
 794     /**
 795      * Gets the default port number of the protocol associated
 796      * with this {@code URL}. If the URL scheme or the URLStreamHandler
 797      * for the URL do not define a default port number,
 798      * then -1 is returned.
 799      *
 800      * @return  the port number
 801      * @since 1.4
 802      */
 803     public int getDefaultPort() {
 804         return handler.getDefaultPort();
 805     }
 806 
 807     /**
 808      * Gets the protocol name of this {@code URL}.
 809      *
 810      * @return  the protocol of this {@code URL}.
 811      */
 812     public String getProtocol() {
 813         return protocol;
 814     }
 815 
 816     /**
 817      * Gets the host name of this {@code URL}, if applicable.
 818      * The format of the host conforms to RFC 2732, i.e. for a
 819      * literal IPv6 address, this method will return the IPv6 address
 820      * enclosed in square brackets ({@code '['} and {@code ']'}).
 821      *
 822      * @return  the host name of this {@code URL}.
 823      */
 824     public String getHost() {
 825         return host;
 826     }
 827 
 828     /**
 829      * Gets the file name of this {@code URL}.
 830      * The returned file portion will be
 831      * the same as <CODE>getPath()</CODE>, plus the concatenation of
 832      * the value of <CODE>getQuery()</CODE>, if any. If there is
 833      * no query portion, this method and <CODE>getPath()</CODE> will
 834      * return identical results.
 835      *
 836      * @return  the file name of this {@code URL},
 837      * or an empty string if one does not exist
 838      */
 839     public String getFile() {
 840         return file;
 841     }
 842 
 843     /**
 844      * Gets the anchor (also known as the "reference") of this
 845      * {@code URL}.
 846      *
 847      * @return  the anchor (also known as the "reference") of this
 848      *          {@code URL}, or <CODE>null</CODE> if one does not exist
 849      */
 850     public String getRef() {
 851         return ref;
 852     }
 853 
 854     /**
 855      * Compares this URL for equality with another object.<p>
 856      *
 857      * If the given object is not a URL then this method immediately returns
 858      * {@code false}.<p>
 859      *
 860      * Two URL objects are equal if they have the same protocol, reference
 861      * equivalent hosts, have the same port number on the host, and the same
 862      * file and fragment of the file.<p>
 863      *
 864      * Two hosts are considered equivalent if both host names can be resolved
 865      * into the same IP addresses; else if either host name can't be
 866      * resolved, the host names must be equal without regard to case; or both
 867      * host names equal to null.<p>
 868      *
 869      * Since hosts comparison requires name resolution, this operation is a
 870      * blocking operation. <p>
 871      *
 872      * Note: The defined behavior for {@code equals} is known to
 873      * be inconsistent with virtual hosting in HTTP.
 874      *
 875      * @param   obj   the URL to compare against.
 876      * @return  {@code true} if the objects are the same;
 877      *          {@code false} otherwise.
 878      */
 879     public boolean equals(Object obj) {
 880         if (!(obj instanceof URL))
 881             return false;
 882         URL u2 = (URL)obj;
 883 
 884         return handler.equals(this, u2);
 885     }
 886 
 887     /**
 888      * Creates an integer suitable for hash table indexing.<p>
 889      *
 890      * The hash code is based upon all the URL components relevant for URL
 891      * comparison. As such, this operation is a blocking operation.
 892      *
 893      * @return  a hash code for this {@code URL}.
 894      */
 895     public synchronized int hashCode() {
 896         if (hashCode != -1)
 897             return hashCode;
 898 
 899         hashCode = handler.hashCode(this);
 900         return hashCode;
 901     }
 902 
 903     /**
 904      * Compares two URLs, excluding the fragment component.<p>
 905      *
 906      * Returns {@code true} if this {@code URL} and the
 907      * {@code other} argument are equal without taking the
 908      * fragment component into consideration.
 909      *
 910      * @param   other   the {@code URL} to compare against.
 911      * @return  {@code true} if they reference the same remote object;
 912      *          {@code false} otherwise.
 913      */
 914     public boolean sameFile(URL other) {
 915         return handler.sameFile(this, other);
 916     }
 917 
 918     /**
 919      * Constructs a string representation of this {@code URL}. The
 920      * string is created by calling the {@code toExternalForm}
 921      * method of the stream protocol handler for this object.
 922      *
 923      * @return  a string representation of this object.
 924      * @see     java.net.URL#URL(java.lang.String, java.lang.String, int,
 925      *                  java.lang.String)
 926      * @see     java.net.URLStreamHandler#toExternalForm(java.net.URL)
 927      */
 928     public String toString() {
 929         return toExternalForm();
 930     }
 931 
 932     /**
 933      * Constructs a string representation of this {@code URL}. The
 934      * string is created by calling the {@code toExternalForm}
 935      * method of the stream protocol handler for this object.
 936      *
 937      * @return  a string representation of this object.
 938      * @see     java.net.URL#URL(java.lang.String, java.lang.String,
 939      *                  int, java.lang.String)
 940      * @see     java.net.URLStreamHandler#toExternalForm(java.net.URL)
 941      */
 942     public String toExternalForm() {
 943         return handler.toExternalForm(this);
 944     }
 945 
 946     /**
 947      * Returns a {@link java.net.URI} equivalent to this URL.
 948      * This method functions in the same way as {@code new URI (this.toString())}.
 949      * <p>Note, any URL instance that complies with RFC 2396 can be converted
 950      * to a URI. However, some URLs that are not strictly in compliance
 951      * can not be converted to a URI.
 952      *
 953      * @exception URISyntaxException if this URL is not formatted strictly according to
 954      *            to RFC2396 and cannot be converted to a URI.
 955      *
 956      * @return    a URI instance equivalent to this URL.
 957      * @since 1.5
 958      */
 959     public URI toURI() throws URISyntaxException {
 960         return new URI (toString());
 961     }
 962 
 963     /**
 964      * Returns a {@link java.net.URLConnection URLConnection} instance that
 965      * represents a connection to the remote object referred to by the
 966      * {@code URL}.
 967      *
 968      * <P>A new instance of {@linkplain java.net.URLConnection URLConnection} is
 969      * created every time when invoking the
 970      * {@linkplain java.net.URLStreamHandler#openConnection(URL)
 971      * URLStreamHandler.openConnection(URL)} method of the protocol handler for
 972      * this URL.</P>
 973      *
 974      * <P>It should be noted that a URLConnection instance does not establish
 975      * the actual network connection on creation. This will happen only when
 976      * calling {@linkplain java.net.URLConnection#connect() URLConnection.connect()}.</P>
 977      *
 978      * <P>If for the URL's protocol (such as HTTP or JAR), there
 979      * exists a public, specialized URLConnection subclass belonging
 980      * to one of the following packages or one of their subpackages:
 981      * java.lang, java.io, java.util, java.net, the connection
 982      * returned will be of that subclass. For example, for HTTP an
 983      * HttpURLConnection will be returned, and for JAR a
 984      * JarURLConnection will be returned.</P>
 985      *
 986      * @return     a {@link java.net.URLConnection URLConnection} linking
 987      *             to the URL.
 988      * @exception  IOException  if an I/O exception occurs.
 989      * @see        java.net.URL#URL(java.lang.String, java.lang.String,
 990      *             int, java.lang.String)
 991      */
 992     public URLConnection openConnection() throws java.io.IOException {
 993         return handler.openConnection(this);
 994     }
 995 
 996     /**
 997      * Same as {@link #openConnection()}, except that the connection will be
 998      * made through the specified proxy; Protocol handlers that do not
 999      * support proxing will ignore the proxy parameter and make a
1000      * normal connection.
1001      *
1002      * Invoking this method preempts the system's default
1003      * {@link java.net.ProxySelector ProxySelector} settings.
1004      *
1005      * @param      proxy the Proxy through which this connection
1006      *             will be made. If direct connection is desired,
1007      *             Proxy.NO_PROXY should be specified.
1008      * @return     a {@code URLConnection} to the URL.
1009      * @exception  IOException  if an I/O exception occurs.
1010      * @exception  SecurityException if a security manager is present
1011      *             and the caller doesn't have permission to connect
1012      *             to the proxy.
1013      * @exception  IllegalArgumentException will be thrown if proxy is null,
1014      *             or proxy has the wrong type
1015      * @exception  UnsupportedOperationException if the subclass that
1016      *             implements the protocol handler doesn't support
1017      *             this method.
1018      * @see        java.net.URL#URL(java.lang.String, java.lang.String,
1019      *             int, java.lang.String)
1020      * @see        java.net.URLConnection
1021      * @see        java.net.URLStreamHandler#openConnection(java.net.URL,
1022      *             java.net.Proxy)
1023      * @since      1.5
1024      */
1025     public URLConnection openConnection(Proxy proxy)
1026         throws java.io.IOException {
1027         if (proxy == null) {
1028             throw new IllegalArgumentException("proxy can not be null");
1029         }
1030 
1031         // Create a copy of Proxy as a security measure
1032         Proxy p = proxy == Proxy.NO_PROXY ? Proxy.NO_PROXY : sun.net.ApplicationProxy.create(proxy);
1033         SecurityManager sm = System.getSecurityManager();
1034         if (p.type() != Proxy.Type.DIRECT && sm != null) {
1035             InetSocketAddress epoint = (InetSocketAddress) p.address();
1036             if (epoint.isUnresolved())
1037                 sm.checkConnect(epoint.getHostName(), epoint.getPort());
1038             else
1039                 sm.checkConnect(epoint.getAddress().getHostAddress(),
1040                                 epoint.getPort());
1041         }
1042         return handler.openConnection(this, p);
1043     }
1044 
1045     /**
1046      * Opens a connection to this {@code URL} and returns an
1047      * {@code InputStream} for reading from that connection. This
1048      * method is a shorthand for:
1049      * <blockquote><pre>
1050      *     openConnection().getInputStream()
1051      * </pre></blockquote>
1052      *
1053      * @return     an input stream for reading from the URL connection.
1054      * @exception  IOException  if an I/O exception occurs.
1055      * @see        java.net.URL#openConnection()
1056      * @see        java.net.URLConnection#getInputStream()
1057      */
1058     public final InputStream openStream() throws java.io.IOException {
1059         return openConnection().getInputStream();
1060     }
1061 
1062     /**
1063      * Gets the contents of this URL. This method is a shorthand for:
1064      * <blockquote><pre>
1065      *     openConnection().getContent()
1066      * </pre></blockquote>
1067      *
1068      * @return     the contents of this URL.
1069      * @exception  IOException  if an I/O exception occurs.
1070      * @see        java.net.URLConnection#getContent()
1071      */
1072     public final Object getContent() throws java.io.IOException {
1073         return openConnection().getContent();
1074     }
1075 
1076     /**
1077      * Gets the contents of this URL. This method is a shorthand for:
1078      * <blockquote><pre>
1079      *     openConnection().getContent(classes)
1080      * </pre></blockquote>
1081      *
1082      * @param classes an array of Java types
1083      * @return     the content object of this URL that is the first match of
1084      *               the types specified in the classes array.
1085      *               null if none of the requested types are supported.
1086      * @exception  IOException  if an I/O exception occurs.
1087      * @see        java.net.URLConnection#getContent(Class[])
1088      * @since 1.3
1089      */
1090     public final Object getContent(Class<?>[] classes)
1091     throws java.io.IOException {
1092         return openConnection().getContent(classes);
1093     }
1094 
1095     /**
1096      * The URLStreamHandler factory.
1097      */
1098     private static volatile URLStreamHandlerFactory factory;
1099 
1100     /**
1101      * Sets an application's {@code URLStreamHandlerFactory}.
1102      * This method can be called at most once in a given Java Virtual
1103      * Machine.
1104      *
1105      *<p> The {@code URLStreamHandlerFactory} instance is used to
1106      *construct a stream protocol handler from a protocol name.
1107      *
1108      * <p> If there is a security manager, this method first calls
1109      * the security manager's {@code checkSetFactory} method
1110      * to ensure the operation is allowed.
1111      * This could result in a SecurityException.
1112      *
1113      * @param      fac   the desired factory.
1114      * @exception  Error  if the application has already set a factory.
1115      * @exception  SecurityException  if a security manager exists and its
1116      *             {@code checkSetFactory} method doesn't allow
1117      *             the operation.
1118      * @see        java.net.URL#URL(java.lang.String, java.lang.String,
1119      *             int, java.lang.String)
1120      * @see        java.net.URLStreamHandlerFactory
1121      * @see        SecurityManager#checkSetFactory
1122      */
1123     public static void setURLStreamHandlerFactory(URLStreamHandlerFactory fac) {
1124         synchronized (streamHandlerLock) {
1125             if (factory != null) {
1126                 throw new Error("factory already defined");
1127             }
1128             SecurityManager security = System.getSecurityManager();
1129             if (security != null) {
1130                 security.checkSetFactory();
1131             }
1132             handlers.clear();
1133 
1134             // safe publication of URLStreamHandlerFactory with volatile write
1135             factory = fac;
1136         }
1137     }
1138 
1139     private static final URLStreamHandlerFactory defaultFactory = new DefaultFactory();
1140 
1141     private static class DefaultFactory implements URLStreamHandlerFactory {
1142         private static String PREFIX = "sun.net.www.protocol";
1143 
1144         public URLStreamHandler createURLStreamHandler(String protocol) {
1145             String name = PREFIX + "." + protocol + ".Handler";
1146             try {
1147                 Class<?> c = Class.forName(name);
1148                 return (URLStreamHandler)c.newInstance();
1149             } catch (ClassNotFoundException x) {
1150                 // ignore
1151             } catch (Exception e) {
1152                 // For compatibility, all Exceptions are ignored.
1153                 // any number of exceptions can get thrown here
1154             }
1155             return null;
1156         }
1157     }
1158 
1159     private static URLStreamHandler lookupViaProperty(String protocol) {
1160         String packagePrefixList = java.security.AccessController.doPrivileged(
1161                 new PrivilegedAction<>() {
1162                     public String run() {
1163                         return System.getProperty(protocolPathProp, "");
1164                     }
1165                 });
1166         String[] packagePrefixes = packagePrefixList.split("\\|");
1167 
1168         URLStreamHandler handler = null;
1169         for (int i=0; handler == null && i<packagePrefixes.length; i++) {
1170             String packagePrefix = packagePrefixes[i].trim();
1171             try {
1172                 String clsName = packagePrefix + "." + protocol + ".Handler";
1173                 Class<?> cls = null;
1174                 try {
1175                     cls = Class.forName(clsName);
1176                 } catch (ClassNotFoundException e) {
1177                     ClassLoader cl = ClassLoader.getSystemClassLoader();
1178                     if (cl != null) {
1179                         cls = cl.loadClass(clsName);
1180                     }
1181                 }
1182                 if (cls != null) {
1183                     handler = (URLStreamHandler)cls.newInstance();
1184                 }
1185             } catch (Exception e) {
1186                 // any number of exceptions can get thrown here
1187             }
1188         }
1189         return handler;
1190     }
1191 
1192     private static Iterator<URLStreamHandlerProvider> providers() {
1193         return new Iterator<>() {
1194 
1195             ClassLoader cl = ClassLoader.getSystemClassLoader();
1196             ServiceLoader<URLStreamHandlerProvider> sl =
1197                     ServiceLoader.load(URLStreamHandlerProvider.class, cl);
1198             Iterator<URLStreamHandlerProvider> i = sl.iterator();
1199 
1200             URLStreamHandlerProvider next = null;
1201 
1202             private boolean getNext() {
1203                 while (next == null) {
1204                     try {
1205                         if (!i.hasNext())
1206                             return false;
1207                         next = i.next();
1208                     } catch (ServiceConfigurationError sce) {
1209                         if (sce.getCause() instanceof SecurityException) {
1210                             // Ignore security exceptions
1211                             continue;
1212                         }
1213                         throw sce;
1214                     }
1215                 }
1216                 return true;
1217             }
1218 
1219             public boolean hasNext() {
1220                 return getNext();
1221             }
1222 
1223             public URLStreamHandlerProvider next() {
1224                 if (!getNext())
1225                     throw new NoSuchElementException();
1226                 URLStreamHandlerProvider n = next;
1227                 next = null;
1228                 return n;
1229             }
1230         };
1231     }
1232 
1233     // Thread-local gate to prevent recursive provider lookups
1234     private static ThreadLocal<Object> gate = new ThreadLocal<>();
1235 
1236     private static URLStreamHandler lookupViaProviders(final String protocol) {
1237         if (!sun.misc.VM.isBooted())
1238             return null;
1239 
1240         if (gate.get() != null)
1241             throw new Error("Circular loading of URL stream handler providers detected");
1242 
1243         gate.set(gate);
1244         try {
1245             return AccessController.doPrivileged(
1246                 new PrivilegedAction<>() {
1247                     public URLStreamHandler run() {
1248                         Iterator<URLStreamHandlerProvider> itr = providers();
1249                         while (itr.hasNext()) {
1250                             URLStreamHandlerProvider f = itr.next();
1251                             URLStreamHandler h = f.createURLStreamHandler(protocol);
1252                             if (h != null)
1253                                 return h;
1254                         }
1255                         return null;
1256                     }
1257                 });
1258         } finally {
1259             gate.set(null);
1260         }
1261     }
1262 
1263     private static final String[] NON_OVERRIDEABLE_PROTOCOLS = {"file", "jrt"};
1264     private static boolean isOverrideable(String protocol) {
1265         for (String p : NON_OVERRIDEABLE_PROTOCOLS)
1266             if (protocol.equalsIgnoreCase(p))
1267                 return false;
1268         return true;
1269     }
1270 
1271     /**
1272      * A table of protocol handlers.
1273      */
1274     static Hashtable<String,URLStreamHandler> handlers = new Hashtable<>();
1275     private static final Object streamHandlerLock = new Object();
1276 
1277     /**
1278      * Returns the Stream Handler.
1279      * @param protocol the protocol to use
1280      */
1281     static URLStreamHandler getURLStreamHandler(String protocol) {
1282 
1283         URLStreamHandler handler = handlers.get(protocol);
1284 
1285         if (handler != null) {
1286             return handler;
1287         }
1288 
1289         URLStreamHandlerFactory fac;
1290         boolean checkedWithFactory = false;
1291 
1292         if (isOverrideable(protocol)) {
1293             // Use the factory (if any). Volatile read makes
1294             // URLStreamHandlerFactory appear fully initialized to current thread.
1295             fac = factory;
1296             if (fac != null) {
1297                 handler = fac.createURLStreamHandler(protocol);
1298                 checkedWithFactory = true;
1299             }
1300 
1301             if (handler == null && !protocol.equalsIgnoreCase("jar")) {
1302                 handler = lookupViaProviders(protocol);
1303             }
1304 
1305             if (handler == null) {
1306                 handler = lookupViaProperty(protocol);
1307             }
1308         }
1309 
1310         synchronized (streamHandlerLock) {
1311             if (handler == null) {
1312                 // Try the built-in protocol handler
1313                 handler = defaultFactory.createURLStreamHandler(protocol);
1314             } else {
1315                 URLStreamHandler handler2 = null;
1316 
1317                 // Check again with hashtable just in case another
1318                 // thread created a handler since we last checked
1319                 handler2 = handlers.get(protocol);
1320 
1321                 if (handler2 != null) {
1322                     return handler2;
1323                 }
1324 
1325                 // Check with factory if another thread set a
1326                 // factory since our last check
1327                 if (!checkedWithFactory && (fac = factory) != null) {
1328                     handler2 =  fac.createURLStreamHandler(protocol);
1329                 }
1330 
1331                 if (handler2 != null) {
1332                     // The handler from the factory must be given more
1333                     // importance. Discard the default handler that
1334                     // this thread created.
1335                     handler = handler2;
1336                 }
1337             }
1338 
1339             // Insert this handler into the hashtable
1340             if (handler != null) {
1341                 handlers.put(protocol, handler);
1342             }
1343         }
1344 
1345         return handler;
1346     }
1347 
1348     /**
1349      * WriteObject is called to save the state of the URL to an
1350      * ObjectOutputStream. The handler is not saved since it is
1351      * specific to this system.
1352      *
1353      * @serialData the default write object value. When read back in,
1354      * the reader must ensure that calling getURLStreamHandler with
1355      * the protocol variable returns a valid URLStreamHandler and
1356      * throw an IOException if it does not.
1357      */
1358     private synchronized void writeObject(java.io.ObjectOutputStream s)
1359         throws IOException
1360     {
1361         s.defaultWriteObject(); // write the fields
1362     }
1363 
1364     /**
1365      * readObject is called to restore the state of the URL from the
1366      * stream.  It reads the components of the URL and finds the local
1367      * stream handler.
1368      */
1369     private synchronized void readObject(java.io.ObjectInputStream s)
1370          throws IOException, ClassNotFoundException
1371     {
1372         s.defaultReadObject();  // read the fields
1373         if ((handler = getURLStreamHandler(protocol)) == null) {
1374             throw new IOException("unknown protocol: " + protocol);
1375         }
1376 
1377         // Construct authority part
1378         if (authority == null &&
1379             ((host != null && host.length() > 0) || port != -1)) {
1380             if (host == null)
1381                 host = "";
1382             authority = (port == -1) ? host : host + ":" + port;
1383 
1384             // Handle hosts with userInfo in them
1385             int at = host.lastIndexOf('@');
1386             if (at != -1) {
1387                 userInfo = host.substring(0, at);
1388                 host = host.substring(at+1);
1389             }
1390         } else if (authority != null) {
1391             // Construct user info part
1392             int ind = authority.indexOf('@');
1393             if (ind != -1)
1394                 userInfo = authority.substring(0, ind);
1395         }
1396 
1397         // Construct path and query part
1398         path = null;
1399         query = null;
1400         if (file != null) {
1401             // Fix: only do this if hierarchical?
1402             int q = file.lastIndexOf('?');
1403             if (q != -1) {
1404                 query = file.substring(q+1);
1405                 path = file.substring(0, q);
1406             } else
1407                 path = file;
1408         }
1409     }
1410 }
1411 
1412 class Parts {
1413     String path, query, ref;
1414 
1415     Parts(String file) {
1416         int ind = file.indexOf('#');
1417         ref = ind < 0 ? null: file.substring(ind + 1);
1418         file = ind < 0 ? file: file.substring(0, ind);
1419         int q = file.lastIndexOf('?');
1420         if (q != -1) {
1421             query = file.substring(q+1);
1422             path = file.substring(0, q);
1423         } else {
1424             path = file;
1425         }
1426     }
1427 
1428     String getPath() {
1429         return path;
1430     }
1431 
1432     String getQuery() {
1433         return query;
1434     }
1435 
1436     String getRef() {
1437         return ref;
1438     }
1439 }