jdk/src/share/classes/java/net/URL.java
changeset 19069 1d9cb0d080e3
parent 16055 311a101eb72c
child 21278 ef8a3a2a72f2
equal deleted inserted replaced
19068:f2358d18923a 19069:1d9cb0d080e3
     1 /*
     1 /*
     2  * Copyright (c) 1995, 2012, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    30 import java.util.Hashtable;
    30 import java.util.Hashtable;
    31 import java.util.StringTokenizer;
    31 import java.util.StringTokenizer;
    32 import sun.security.util.SecurityConstants;
    32 import sun.security.util.SecurityConstants;
    33 
    33 
    34 /**
    34 /**
    35  * Class <code>URL</code> represents a Uniform Resource
    35  * Class {@code URL} represents a Uniform Resource
    36  * Locator, a pointer to a "resource" on the World
    36  * Locator, a pointer to a "resource" on the World
    37  * Wide Web. A resource can be something as simple as a file or a
    37  * Wide Web. A resource can be something as simple as a file or a
    38  * directory, or it can be a reference to a more complicated object,
    38  * directory, or it can be a reference to a more complicated object,
    39  * such as a query to a database or to a search engine. More
    39  * such as a query to a database or to a search engine. More
    40  * information on the types of URLs and their formats can be found at:
    40  * information on the types of URLs and their formats can be found at:
    47  * <blockquote><pre>
    47  * <blockquote><pre>
    48  *     http://www.example.com/docs/resource1.html
    48  *     http://www.example.com/docs/resource1.html
    49  * </pre></blockquote>
    49  * </pre></blockquote>
    50  * <p>
    50  * <p>
    51  * The URL above indicates that the protocol to use is
    51  * The URL above indicates that the protocol to use is
    52  * <code>http</code> (HyperText Transfer Protocol) and that the
    52  * {@code http} (HyperText Transfer Protocol) and that the
    53  * information resides on a host machine named
    53  * information resides on a host machine named
    54  * <code>www.example.com</code>. The information on that host
    54  * {@code www.example.com}. The information on that host
    55  * machine is named <code>/docs/resource1.html</code>. The exact
    55  * machine is named {@code /docs/resource1.html}. The exact
    56  * meaning of this name on the host machine is both protocol
    56  * meaning of this name on the host machine is both protocol
    57  * dependent and host dependent. The information normally resides in
    57  * dependent and host dependent. The information normally resides in
    58  * a file, but it could be generated on the fly. This component of
    58  * a file, but it could be generated on the fly. This component of
    59  * the URL is called the <i>path</i> component.
    59  * the URL is called the <i>path</i> component.
    60  * <p>
    60  * <p>
    61  * A URL can optionally specify a "port", which is the
    61  * A URL can optionally specify a "port", which is the
    62  * port number to which the TCP connection is made on the remote host
    62  * port number to which the TCP connection is made on the remote host
    63  * machine. If the port is not specified, the default port for
    63  * machine. If the port is not specified, the default port for
    64  * the protocol is used instead. For example, the default port for
    64  * the protocol is used instead. For example, the default port for
    65  * <code>http</code> is <code>80</code>. An alternative port could be
    65  * {@code http} is {@code 80}. An alternative port could be
    66  * specified as:
    66  * specified as:
    67  * <blockquote><pre>
    67  * <blockquote><pre>
    68  *     http://www.example.com:1080/docs/resource1.html
    68  *     http://www.example.com:1080/docs/resource1.html
    69  * </pre></blockquote>
    69  * </pre></blockquote>
    70  * <p>
    70  * <p>
    71  * The syntax of <code>URL</code> is defined by  <a
    71  * The syntax of {@code URL} is defined by  <a
    72  * href="http://www.ietf.org/rfc/rfc2396.txt"><i>RFC&nbsp;2396: Uniform
    72  * href="http://www.ietf.org/rfc/rfc2396.txt"><i>RFC&nbsp;2396: Uniform
    73  * Resource Identifiers (URI): Generic Syntax</i></a>, amended by <a
    73  * Resource Identifiers (URI): Generic Syntax</i></a>, amended by <a
    74  * href="http://www.ietf.org/rfc/rfc2732.txt"><i>RFC&nbsp;2732: Format for
    74  * href="http://www.ietf.org/rfc/rfc2732.txt"><i>RFC&nbsp;2732: Format for
    75  * Literal IPv6 Addresses in URLs</i></a>. The Literal IPv6 address format
    75  * Literal IPv6 Addresses in URLs</i></a>. The Literal IPv6 address format
    76  * also supports scope_ids. The syntax and usage of scope_ids is described
    76  * also supports scope_ids. The syntax and usage of scope_ids is described
    84  * </pre></blockquote>
    84  * </pre></blockquote>
    85  * <p>
    85  * <p>
    86  * This fragment is not technically part of the URL. Rather, it
    86  * This fragment is not technically part of the URL. Rather, it
    87  * indicates that after the specified resource is retrieved, the
    87  * indicates that after the specified resource is retrieved, the
    88  * application is specifically interested in that part of the
    88  * application is specifically interested in that part of the
    89  * document that has the tag <code>chapter1</code> attached to it. The
    89  * document that has the tag {@code chapter1} attached to it. The
    90  * meaning of a tag is resource specific.
    90  * meaning of a tag is resource specific.
    91  * <p>
    91  * <p>
    92  * An application can also specify a "relative URL",
    92  * An application can also specify a "relative URL",
    93  * which contains only enough information to reach the resource
    93  * which contains only enough information to reach the resource
    94  * relative to another URL. Relative URLs are frequently used within
    94  * relative to another URL. Relative URLs are frequently used within
   168      * @serial
   168      * @serial
   169      */
   169      */
   170     private int port = -1;
   170     private int port = -1;
   171 
   171 
   172     /**
   172     /**
   173      * The specified file name on that host. <code>file</code> is
   173      * The specified file name on that host. {@code file} is
   174      * defined as <code>path[?query]</code>
   174      * defined as {@code path[?query]}
   175      * @serial
   175      * @serial
   176      */
   176      */
   177     private String file;
   177     private String file;
   178 
   178 
   179     /**
   179     /**
   218      * @serial
   218      * @serial
   219      */
   219      */
   220     private int hashCode = -1;
   220     private int hashCode = -1;
   221 
   221 
   222     /**
   222     /**
   223      * Creates a <code>URL</code> object from the specified
   223      * Creates a {@code URL} object from the specified
   224      * <code>protocol</code>, <code>host</code>, <code>port</code>
   224      * {@code protocol}, {@code host}, {@code port}
   225      * number, and <code>file</code>.<p>
   225      * number, and {@code file}.<p>
   226      *
   226      *
   227      * <code>host</code> can be expressed as a host name or a literal
   227      * {@code host} can be expressed as a host name or a literal
   228      * IP address. If IPv6 literal address is used, it should be
   228      * IP address. If IPv6 literal address is used, it should be
   229      * enclosed in square brackets (<tt>'['</tt> and <tt>']'</tt>), as
   229      * enclosed in square brackets ({@code '['} and {@code ']'}), as
   230      * specified by <a
   230      * specified by <a
   231      * href="http://www.ietf.org/rfc/rfc2732.txt">RFC&nbsp;2732</a>;
   231      * href="http://www.ietf.org/rfc/rfc2732.txt">RFC&nbsp;2732</a>;
   232      * However, the literal IPv6 address format defined in <a
   232      * However, the literal IPv6 address format defined in <a
   233      * href="http://www.ietf.org/rfc/rfc2373.txt"><i>RFC&nbsp;2373: IP
   233      * href="http://www.ietf.org/rfc/rfc2373.txt"><i>RFC&nbsp;2373: IP
   234      * Version 6 Addressing Architecture</i></a> is also accepted.<p>
   234      * Version 6 Addressing Architecture</i></a> is also accepted.<p>
   235      *
   235      *
   236      * Specifying a <code>port</code> number of <code>-1</code>
   236      * Specifying a {@code port} number of {@code -1}
   237      * indicates that the URL should use the default port for the
   237      * indicates that the URL should use the default port for the
   238      * protocol.<p>
   238      * protocol.<p>
   239      *
   239      *
   240      * If this is the first URL object being created with the specified
   240      * If this is the first URL object being created with the specified
   241      * protocol, a <i>stream protocol handler</i> object, an instance of
   241      * protocol, a <i>stream protocol handler</i> object, an instance of
   242      * class <code>URLStreamHandler</code>, is created for that protocol:
   242      * class {@code URLStreamHandler}, is created for that protocol:
   243      * <ol>
   243      * <ol>
   244      * <li>If the application has previously set up an instance of
   244      * <li>If the application has previously set up an instance of
   245      *     <code>URLStreamHandlerFactory</code> as the stream handler factory,
   245      *     {@code URLStreamHandlerFactory} as the stream handler factory,
   246      *     then the <code>createURLStreamHandler</code> method of that instance
   246      *     then the {@code createURLStreamHandler} method of that instance
   247      *     is called with the protocol string as an argument to create the
   247      *     is called with the protocol string as an argument to create the
   248      *     stream protocol handler.
   248      *     stream protocol handler.
   249      * <li>If no <code>URLStreamHandlerFactory</code> has yet been set up,
   249      * <li>If no {@code URLStreamHandlerFactory} has yet been set up,
   250      *     or if the factory's <code>createURLStreamHandler</code> method
   250      *     or if the factory's {@code createURLStreamHandler} method
   251      *     returns <code>null</code>, then the constructor finds the
   251      *     returns {@code null}, then the constructor finds the
   252      *     value of the system property:
   252      *     value of the system property:
   253      *     <blockquote><pre>
   253      *     <blockquote><pre>
   254      *         java.protocol.handler.pkgs
   254      *         java.protocol.handler.pkgs
   255      *     </pre></blockquote>
   255      *     </pre></blockquote>
   256      *     If the value of that system property is not <code>null</code>,
   256      *     If the value of that system property is not {@code null},
   257      *     it is interpreted as a list of packages separated by a vertical
   257      *     it is interpreted as a list of packages separated by a vertical
   258      *     slash character '<code>|</code>'. The constructor tries to load
   258      *     slash character '{@code |}'. The constructor tries to load
   259      *     the class named:
   259      *     the class named:
   260      *     <blockquote><pre>
   260      *     <blockquote><pre>
   261      *         &lt;<i>package</i>&gt;.&lt;<i>protocol</i>&gt;.Handler
   261      *         &lt;<i>package</i>&gt;.&lt;<i>protocol</i>&gt;.Handler
   262      *     </pre></blockquote>
   262      *     </pre></blockquote>
   263      *     where &lt;<i>package</i>&gt; is replaced by the name of the package
   263      *     where &lt;<i>package</i>&gt; is replaced by the name of the package
   264      *     and &lt;<i>protocol</i>&gt; is replaced by the name of the protocol.
   264      *     and &lt;<i>protocol</i>&gt; is replaced by the name of the protocol.
   265      *     If this class does not exist, or if the class exists but it is not
   265      *     If this class does not exist, or if the class exists but it is not
   266      *     a subclass of <code>URLStreamHandler</code>, then the next package
   266      *     a subclass of {@code URLStreamHandler}, then the next package
   267      *     in the list is tried.
   267      *     in the list is tried.
   268      * <li>If the previous step fails to find a protocol handler, then the
   268      * <li>If the previous step fails to find a protocol handler, then the
   269      *     constructor tries to load from a system default package.
   269      *     constructor tries to load from a system default package.
   270      *     <blockquote><pre>
   270      *     <blockquote><pre>
   271      *         &lt;<i>system default package</i>&gt;.&lt;<i>protocol</i>&gt;.Handler
   271      *         &lt;<i>system default package</i>&gt;.&lt;<i>protocol</i>&gt;.Handler
   272      *     </pre></blockquote>
   272      *     </pre></blockquote>
   273      *     If this class does not exist, or if the class exists but it is not a
   273      *     If this class does not exist, or if the class exists but it is not a
   274      *     subclass of <code>URLStreamHandler</code>, then a
   274      *     subclass of {@code URLStreamHandler}, then a
   275      *     <code>MalformedURLException</code> is thrown.
   275      *     {@code MalformedURLException} is thrown.
   276      * </ol>
   276      * </ol>
   277      *
   277      *
   278      * <p>Protocol handlers for the following protocols are guaranteed
   278      * <p>Protocol handlers for the following protocols are guaranteed
   279      * to exist on the search path :-
   279      * to exist on the search path :-
   280      * <blockquote><pre>
   280      * <blockquote><pre>
   302     {
   302     {
   303         this(protocol, host, port, file, null);
   303         this(protocol, host, port, file, null);
   304     }
   304     }
   305 
   305 
   306     /**
   306     /**
   307      * Creates a URL from the specified <code>protocol</code>
   307      * Creates a URL from the specified {@code protocol}
   308      * name, <code>host</code> name, and <code>file</code> name. The
   308      * name, {@code host} name, and {@code file} name. The
   309      * default port for the specified protocol is used.
   309      * default port for the specified protocol is used.
   310      * <p>
   310      * <p>
   311      * This method is equivalent to calling the four-argument
   311      * This method is equivalent to calling the four-argument
   312      * constructor with the arguments being <code>protocol</code>,
   312      * constructor with the arguments being {@code protocol},
   313      * <code>host</code>, <code>-1</code>, and <code>file</code>.
   313      * {@code host}, {@code -1}, and {@code file}.
   314      *
   314      *
   315      * No validation of the inputs is performed by this constructor.
   315      * No validation of the inputs is performed by this constructor.
   316      *
   316      *
   317      * @param      protocol   the name of the protocol to use.
   317      * @param      protocol   the name of the protocol to use.
   318      * @param      host       the name of the host.
   318      * @param      host       the name of the host.
   325             throws MalformedURLException {
   325             throws MalformedURLException {
   326         this(protocol, host, -1, file);
   326         this(protocol, host, -1, file);
   327     }
   327     }
   328 
   328 
   329     /**
   329     /**
   330      * Creates a <code>URL</code> object from the specified
   330      * Creates a {@code URL} object from the specified
   331      * <code>protocol</code>, <code>host</code>, <code>port</code>
   331      * {@code protocol}, {@code host}, {@code port}
   332      * number, <code>file</code>, and <code>handler</code>. Specifying
   332      * number, {@code file}, and {@code handler}. Specifying
   333      * a <code>port</code> number of <code>-1</code> indicates that
   333      * a {@code port} number of {@code -1} indicates that
   334      * the URL should use the default port for the protocol. Specifying
   334      * the URL should use the default port for the protocol. Specifying
   335      * a <code>handler</code> of <code>null</code> indicates that the URL
   335      * a {@code handler} of {@code null} indicates that the URL
   336      * should use a default stream handler for the protocol, as outlined
   336      * should use a default stream handler for the protocol, as outlined
   337      * for:
   337      * for:
   338      *     java.net.URL#URL(java.lang.String, java.lang.String, int,
   338      *     java.net.URL#URL(java.lang.String, java.lang.String, int,
   339      *                      java.lang.String)
   339      *                      java.lang.String)
   340      *
   340      *
   341      * <p>If the handler is not null and there is a security manager,
   341      * <p>If the handler is not null and there is a security manager,
   342      * the security manager's <code>checkPermission</code>
   342      * the security manager's {@code checkPermission}
   343      * method is called with a
   343      * method is called with a
   344      * <code>NetPermission("specifyStreamHandler")</code> permission.
   344      * {@code NetPermission("specifyStreamHandler")} permission.
   345      * This may result in a SecurityException.
   345      * This may result in a SecurityException.
   346      *
   346      *
   347      * No validation of the inputs is performed by this constructor.
   347      * No validation of the inputs is performed by this constructor.
   348      *
   348      *
   349      * @param      protocol   the name of the protocol to use.
   349      * @param      protocol   the name of the protocol to use.
   352      * @param      file       the file on the host
   352      * @param      file       the file on the host
   353      * @param      handler    the stream handler for the URL.
   353      * @param      handler    the stream handler for the URL.
   354      * @exception  MalformedURLException  if an unknown protocol is specified.
   354      * @exception  MalformedURLException  if an unknown protocol is specified.
   355      * @exception  SecurityException
   355      * @exception  SecurityException
   356      *        if a security manager exists and its
   356      *        if a security manager exists and its
   357      *        <code>checkPermission</code> method doesn't allow
   357      *        {@code checkPermission} method doesn't allow
   358      *        specifying a stream handler explicitly.
   358      *        specifying a stream handler explicitly.
   359      * @see        java.lang.System#getProperty(java.lang.String)
   359      * @see        java.lang.System#getProperty(java.lang.String)
   360      * @see        java.net.URL#setURLStreamHandlerFactory(
   360      * @see        java.net.URL#setURLStreamHandlerFactory(
   361      *                  java.net.URLStreamHandlerFactory)
   361      *                  java.net.URLStreamHandlerFactory)
   362      * @see        java.net.URLStreamHandler
   362      * @see        java.net.URLStreamHandler
   415         }
   415         }
   416         this.handler = handler;
   416         this.handler = handler;
   417     }
   417     }
   418 
   418 
   419     /**
   419     /**
   420      * Creates a <code>URL</code> object from the <code>String</code>
   420      * Creates a {@code URL} object from the {@code String}
   421      * representation.
   421      * representation.
   422      * <p>
   422      * <p>
   423      * This constructor is equivalent to a call to the two-argument
   423      * This constructor is equivalent to a call to the two-argument
   424      * constructor with a <code>null</code> first argument.
   424      * constructor with a {@code null} first argument.
   425      *
   425      *
   426      * @param      spec   the <code>String</code> to parse as a URL.
   426      * @param      spec   the {@code String} to parse as a URL.
   427      * @exception  MalformedURLException  if no protocol is specified, or an
   427      * @exception  MalformedURLException  if no protocol is specified, or an
   428      *               unknown protocol is found, or <tt>spec</tt> is <tt>null</tt>.
   428      *               unknown protocol is found, or {@code spec} is {@code null}.
   429      * @see        java.net.URL#URL(java.net.URL, java.lang.String)
   429      * @see        java.net.URL#URL(java.net.URL, java.lang.String)
   430      */
   430      */
   431     public URL(String spec) throws MalformedURLException {
   431     public URL(String spec) throws MalformedURLException {
   432         this(null, spec);
   432         this(null, spec);
   433     }
   433     }
   468      * changes made by occurences of &quot;..&quot; and &quot;.&quot;.
   468      * changes made by occurences of &quot;..&quot; and &quot;.&quot;.
   469      * <p>
   469      * <p>
   470      * For a more detailed description of URL parsing, refer to RFC2396.
   470      * For a more detailed description of URL parsing, refer to RFC2396.
   471      *
   471      *
   472      * @param      context   the context in which to parse the specification.
   472      * @param      context   the context in which to parse the specification.
   473      * @param      spec      the <code>String</code> to parse as a URL.
   473      * @param      spec      the {@code String} to parse as a URL.
   474      * @exception  MalformedURLException  if no protocol is specified, or an
   474      * @exception  MalformedURLException  if no protocol is specified, or an
   475      *               unknown protocol is found, or <tt>spec</tt> is <tt>null</tt>.
   475      *               unknown protocol is found, or {@code spec} is {@code null}.
   476      * @see        java.net.URL#URL(java.lang.String, java.lang.String,
   476      * @see        java.net.URL#URL(java.lang.String, java.lang.String,
   477      *                  int, java.lang.String)
   477      *                  int, java.lang.String)
   478      * @see        java.net.URLStreamHandler
   478      * @see        java.net.URLStreamHandler
   479      * @see        java.net.URLStreamHandler#parseURL(java.net.URL,
   479      * @see        java.net.URLStreamHandler#parseURL(java.net.URL,
   480      *                  java.lang.String, int, int)
   480      *                  java.lang.String, int, int)
   487      * Creates a URL by parsing the given spec with the specified handler
   487      * Creates a URL by parsing the given spec with the specified handler
   488      * within a specified context. If the handler is null, the parsing
   488      * within a specified context. If the handler is null, the parsing
   489      * occurs as with the two argument constructor.
   489      * occurs as with the two argument constructor.
   490      *
   490      *
   491      * @param      context   the context in which to parse the specification.
   491      * @param      context   the context in which to parse the specification.
   492      * @param      spec      the <code>String</code> to parse as a URL.
   492      * @param      spec      the {@code String} to parse as a URL.
   493      * @param      handler   the stream handler for the URL.
   493      * @param      handler   the stream handler for the URL.
   494      * @exception  MalformedURLException  if no protocol is specified, or an
   494      * @exception  MalformedURLException  if no protocol is specified, or an
   495      *               unknown protocol is found, or <tt>spec</tt> is <tt>null</tt>.
   495      *               unknown protocol is found, or {@code spec} is {@code null}.
   496      * @exception  SecurityException
   496      * @exception  SecurityException
   497      *        if a security manager exists and its
   497      *        if a security manager exists and its
   498      *        <code>checkPermission</code> method doesn't allow
   498      *        {@code checkPermission} method doesn't allow
   499      *        specifying a stream handler.
   499      *        specifying a stream handler.
   500      * @see        java.net.URL#URL(java.lang.String, java.lang.String,
   500      * @see        java.net.URL#URL(java.lang.String, java.lang.String,
   501      *                  int, java.lang.String)
   501      *                  int, java.lang.String)
   502      * @see        java.net.URLStreamHandler
   502      * @see        java.net.URLStreamHandler
   503      * @see        java.net.URLStreamHandler#parseURL(java.net.URL,
   503      * @see        java.net.URLStreamHandler#parseURL(java.net.URL,
   717             this.authority = authority;
   717             this.authority = authority;
   718         }
   718         }
   719     }
   719     }
   720 
   720 
   721     /**
   721     /**
   722      * Gets the query part of this <code>URL</code>.
   722      * Gets the query part of this {@code URL}.
   723      *
   723      *
   724      * @return  the query part of this <code>URL</code>,
   724      * @return  the query part of this {@code URL},
   725      * or <CODE>null</CODE> if one does not exist
   725      * or <CODE>null</CODE> if one does not exist
   726      * @since 1.3
   726      * @since 1.3
   727      */
   727      */
   728     public String getQuery() {
   728     public String getQuery() {
   729         return query;
   729         return query;
   730     }
   730     }
   731 
   731 
   732     /**
   732     /**
   733      * Gets the path part of this <code>URL</code>.
   733      * Gets the path part of this {@code URL}.
   734      *
   734      *
   735      * @return  the path part of this <code>URL</code>, or an
   735      * @return  the path part of this {@code URL}, or an
   736      * empty string if one does not exist
   736      * empty string if one does not exist
   737      * @since 1.3
   737      * @since 1.3
   738      */
   738      */
   739     public String getPath() {
   739     public String getPath() {
   740         return path;
   740         return path;
   741     }
   741     }
   742 
   742 
   743     /**
   743     /**
   744      * Gets the userInfo part of this <code>URL</code>.
   744      * Gets the userInfo part of this {@code URL}.
   745      *
   745      *
   746      * @return  the userInfo part of this <code>URL</code>, or
   746      * @return  the userInfo part of this {@code URL}, or
   747      * <CODE>null</CODE> if one does not exist
   747      * <CODE>null</CODE> if one does not exist
   748      * @since 1.3
   748      * @since 1.3
   749      */
   749      */
   750     public String getUserInfo() {
   750     public String getUserInfo() {
   751         return userInfo;
   751         return userInfo;
   752     }
   752     }
   753 
   753 
   754     /**
   754     /**
   755      * Gets the authority part of this <code>URL</code>.
   755      * Gets the authority part of this {@code URL}.
   756      *
   756      *
   757      * @return  the authority part of this <code>URL</code>
   757      * @return  the authority part of this {@code URL}
   758      * @since 1.3
   758      * @since 1.3
   759      */
   759      */
   760     public String getAuthority() {
   760     public String getAuthority() {
   761         return authority;
   761         return authority;
   762     }
   762     }
   763 
   763 
   764     /**
   764     /**
   765      * Gets the port number of this <code>URL</code>.
   765      * Gets the port number of this {@code URL}.
   766      *
   766      *
   767      * @return  the port number, or -1 if the port is not set
   767      * @return  the port number, or -1 if the port is not set
   768      */
   768      */
   769     public int getPort() {
   769     public int getPort() {
   770         return port;
   770         return port;
   771     }
   771     }
   772 
   772 
   773     /**
   773     /**
   774      * Gets the default port number of the protocol associated
   774      * Gets the default port number of the protocol associated
   775      * with this <code>URL</code>. If the URL scheme or the URLStreamHandler
   775      * with this {@code URL}. If the URL scheme or the URLStreamHandler
   776      * for the URL do not define a default port number,
   776      * for the URL do not define a default port number,
   777      * then -1 is returned.
   777      * then -1 is returned.
   778      *
   778      *
   779      * @return  the port number
   779      * @return  the port number
   780      * @since 1.4
   780      * @since 1.4
   782     public int getDefaultPort() {
   782     public int getDefaultPort() {
   783         return handler.getDefaultPort();
   783         return handler.getDefaultPort();
   784     }
   784     }
   785 
   785 
   786     /**
   786     /**
   787      * Gets the protocol name of this <code>URL</code>.
   787      * Gets the protocol name of this {@code URL}.
   788      *
   788      *
   789      * @return  the protocol of this <code>URL</code>.
   789      * @return  the protocol of this {@code URL}.
   790      */
   790      */
   791     public String getProtocol() {
   791     public String getProtocol() {
   792         return protocol;
   792         return protocol;
   793     }
   793     }
   794 
   794 
   795     /**
   795     /**
   796      * Gets the host name of this <code>URL</code>, if applicable.
   796      * Gets the host name of this {@code URL}, if applicable.
   797      * The format of the host conforms to RFC 2732, i.e. for a
   797      * The format of the host conforms to RFC 2732, i.e. for a
   798      * literal IPv6 address, this method will return the IPv6 address
   798      * literal IPv6 address, this method will return the IPv6 address
   799      * enclosed in square brackets (<tt>'['</tt> and <tt>']'</tt>).
   799      * enclosed in square brackets ({@code '['} and {@code ']'}).
   800      *
   800      *
   801      * @return  the host name of this <code>URL</code>.
   801      * @return  the host name of this {@code URL}.
   802      */
   802      */
   803     public String getHost() {
   803     public String getHost() {
   804         return host;
   804         return host;
   805     }
   805     }
   806 
   806 
   807     /**
   807     /**
   808      * Gets the file name of this <code>URL</code>.
   808      * Gets the file name of this {@code URL}.
   809      * The returned file portion will be
   809      * The returned file portion will be
   810      * the same as <CODE>getPath()</CODE>, plus the concatenation of
   810      * the same as <CODE>getPath()</CODE>, plus the concatenation of
   811      * the value of <CODE>getQuery()</CODE>, if any. If there is
   811      * the value of <CODE>getQuery()</CODE>, if any. If there is
   812      * no query portion, this method and <CODE>getPath()</CODE> will
   812      * no query portion, this method and <CODE>getPath()</CODE> will
   813      * return identical results.
   813      * return identical results.
   814      *
   814      *
   815      * @return  the file name of this <code>URL</code>,
   815      * @return  the file name of this {@code URL},
   816      * or an empty string if one does not exist
   816      * or an empty string if one does not exist
   817      */
   817      */
   818     public String getFile() {
   818     public String getFile() {
   819         return file;
   819         return file;
   820     }
   820     }
   821 
   821 
   822     /**
   822     /**
   823      * Gets the anchor (also known as the "reference") of this
   823      * Gets the anchor (also known as the "reference") of this
   824      * <code>URL</code>.
   824      * {@code URL}.
   825      *
   825      *
   826      * @return  the anchor (also known as the "reference") of this
   826      * @return  the anchor (also known as the "reference") of this
   827      *          <code>URL</code>, or <CODE>null</CODE> if one does not exist
   827      *          {@code URL}, or <CODE>null</CODE> if one does not exist
   828      */
   828      */
   829     public String getRef() {
   829     public String getRef() {
   830         return ref;
   830         return ref;
   831     }
   831     }
   832 
   832 
   833     /**
   833     /**
   834      * Compares this URL for equality with another object.<p>
   834      * Compares this URL for equality with another object.<p>
   835      *
   835      *
   836      * If the given object is not a URL then this method immediately returns
   836      * If the given object is not a URL then this method immediately returns
   837      * <code>false</code>.<p>
   837      * {@code false}.<p>
   838      *
   838      *
   839      * Two URL objects are equal if they have the same protocol, reference
   839      * Two URL objects are equal if they have the same protocol, reference
   840      * equivalent hosts, have the same port number on the host, and the same
   840      * equivalent hosts, have the same port number on the host, and the same
   841      * file and fragment of the file.<p>
   841      * file and fragment of the file.<p>
   842      *
   842      *
   846      * host names equal to null.<p>
   846      * host names equal to null.<p>
   847      *
   847      *
   848      * Since hosts comparison requires name resolution, this operation is a
   848      * Since hosts comparison requires name resolution, this operation is a
   849      * blocking operation. <p>
   849      * blocking operation. <p>
   850      *
   850      *
   851      * Note: The defined behavior for <code>equals</code> is known to
   851      * Note: The defined behavior for {@code equals} is known to
   852      * be inconsistent with virtual hosting in HTTP.
   852      * be inconsistent with virtual hosting in HTTP.
   853      *
   853      *
   854      * @param   obj   the URL to compare against.
   854      * @param   obj   the URL to compare against.
   855      * @return  <code>true</code> if the objects are the same;
   855      * @return  {@code true} if the objects are the same;
   856      *          <code>false</code> otherwise.
   856      *          {@code false} otherwise.
   857      */
   857      */
   858     public boolean equals(Object obj) {
   858     public boolean equals(Object obj) {
   859         if (!(obj instanceof URL))
   859         if (!(obj instanceof URL))
   860             return false;
   860             return false;
   861         URL u2 = (URL)obj;
   861         URL u2 = (URL)obj;
   867      * Creates an integer suitable for hash table indexing.<p>
   867      * Creates an integer suitable for hash table indexing.<p>
   868      *
   868      *
   869      * The hash code is based upon all the URL components relevant for URL
   869      * The hash code is based upon all the URL components relevant for URL
   870      * comparison. As such, this operation is a blocking operation.<p>
   870      * comparison. As such, this operation is a blocking operation.<p>
   871      *
   871      *
   872      * @return  a hash code for this <code>URL</code>.
   872      * @return  a hash code for this {@code URL}.
   873      */
   873      */
   874     public synchronized int hashCode() {
   874     public synchronized int hashCode() {
   875         if (hashCode != -1)
   875         if (hashCode != -1)
   876             return hashCode;
   876             return hashCode;
   877 
   877 
   880     }
   880     }
   881 
   881 
   882     /**
   882     /**
   883      * Compares two URLs, excluding the fragment component.<p>
   883      * Compares two URLs, excluding the fragment component.<p>
   884      *
   884      *
   885      * Returns <code>true</code> if this <code>URL</code> and the
   885      * Returns {@code true} if this {@code URL} and the
   886      * <code>other</code> argument are equal without taking the
   886      * {@code other} argument are equal without taking the
   887      * fragment component into consideration.
   887      * fragment component into consideration.
   888      *
   888      *
   889      * @param   other   the <code>URL</code> to compare against.
   889      * @param   other   the {@code URL} to compare against.
   890      * @return  <code>true</code> if they reference the same remote object;
   890      * @return  {@code true} if they reference the same remote object;
   891      *          <code>false</code> otherwise.
   891      *          {@code false} otherwise.
   892      */
   892      */
   893     public boolean sameFile(URL other) {
   893     public boolean sameFile(URL other) {
   894         return handler.sameFile(this, other);
   894         return handler.sameFile(this, other);
   895     }
   895     }
   896 
   896 
   897     /**
   897     /**
   898      * Constructs a string representation of this <code>URL</code>. The
   898      * Constructs a string representation of this {@code URL}. The
   899      * string is created by calling the <code>toExternalForm</code>
   899      * string is created by calling the {@code toExternalForm}
   900      * method of the stream protocol handler for this object.
   900      * method of the stream protocol handler for this object.
   901      *
   901      *
   902      * @return  a string representation of this object.
   902      * @return  a string representation of this object.
   903      * @see     java.net.URL#URL(java.lang.String, java.lang.String, int,
   903      * @see     java.net.URL#URL(java.lang.String, java.lang.String, int,
   904      *                  java.lang.String)
   904      *                  java.lang.String)
   907     public String toString() {
   907     public String toString() {
   908         return toExternalForm();
   908         return toExternalForm();
   909     }
   909     }
   910 
   910 
   911     /**
   911     /**
   912      * Constructs a string representation of this <code>URL</code>. The
   912      * Constructs a string representation of this {@code URL}. The
   913      * string is created by calling the <code>toExternalForm</code>
   913      * string is created by calling the {@code toExternalForm}
   914      * method of the stream protocol handler for this object.
   914      * method of the stream protocol handler for this object.
   915      *
   915      *
   916      * @return  a string representation of this object.
   916      * @return  a string representation of this object.
   917      * @see     java.net.URL#URL(java.lang.String, java.lang.String,
   917      * @see     java.net.URL#URL(java.lang.String, java.lang.String,
   918      *                  int, java.lang.String)
   918      *                  int, java.lang.String)
   922         return handler.toExternalForm(this);
   922         return handler.toExternalForm(this);
   923     }
   923     }
   924 
   924 
   925     /**
   925     /**
   926      * Returns a {@link java.net.URI} equivalent to this URL.
   926      * Returns a {@link java.net.URI} equivalent to this URL.
   927      * This method functions in the same way as <code>new URI (this.toString())</code>.
   927      * This method functions in the same way as {@code new URI (this.toString())}.
   928      * <p>Note, any URL instance that complies with RFC 2396 can be converted
   928      * <p>Note, any URL instance that complies with RFC 2396 can be converted
   929      * to a URI. However, some URLs that are not strictly in compliance
   929      * to a URI. However, some URLs that are not strictly in compliance
   930      * can not be converted to a URI.
   930      * can not be converted to a URI.
   931      *
   931      *
   932      * @exception URISyntaxException if this URL is not formatted strictly according to
   932      * @exception URISyntaxException if this URL is not formatted strictly according to
   982      * settings.
   982      * settings.
   983      *
   983      *
   984      * @param      proxy the Proxy through which this connection
   984      * @param      proxy the Proxy through which this connection
   985      *             will be made. If direct connection is desired,
   985      *             will be made. If direct connection is desired,
   986      *             Proxy.NO_PROXY should be specified.
   986      *             Proxy.NO_PROXY should be specified.
   987      * @return     a <code>URLConnection</code> to the URL.
   987      * @return     a {@code URLConnection} to the URL.
   988      * @exception  IOException  if an I/O exception occurs.
   988      * @exception  IOException  if an I/O exception occurs.
   989      * @exception  SecurityException if a security manager is present
   989      * @exception  SecurityException if a security manager is present
   990      *             and the caller doesn't have permission to connect
   990      *             and the caller doesn't have permission to connect
   991      *             to the proxy.
   991      *             to the proxy.
   992      * @exception  IllegalArgumentException will be thrown if proxy is null,
   992      * @exception  IllegalArgumentException will be thrown if proxy is null,
  1020         }
  1020         }
  1021         return handler.openConnection(this, p);
  1021         return handler.openConnection(this, p);
  1022     }
  1022     }
  1023 
  1023 
  1024     /**
  1024     /**
  1025      * Opens a connection to this <code>URL</code> and returns an
  1025      * Opens a connection to this {@code URL} and returns an
  1026      * <code>InputStream</code> for reading from that connection. This
  1026      * {@code InputStream} for reading from that connection. This
  1027      * method is a shorthand for:
  1027      * method is a shorthand for:
  1028      * <blockquote><pre>
  1028      * <blockquote><pre>
  1029      *     openConnection().getInputStream()
  1029      *     openConnection().getInputStream()
  1030      * </pre></blockquote>
  1030      * </pre></blockquote>
  1031      *
  1031      *
  1075      * The URLStreamHandler factory.
  1075      * The URLStreamHandler factory.
  1076      */
  1076      */
  1077     static URLStreamHandlerFactory factory;
  1077     static URLStreamHandlerFactory factory;
  1078 
  1078 
  1079     /**
  1079     /**
  1080      * Sets an application's <code>URLStreamHandlerFactory</code>.
  1080      * Sets an application's {@code URLStreamHandlerFactory}.
  1081      * This method can be called at most once in a given Java Virtual
  1081      * This method can be called at most once in a given Java Virtual
  1082      * Machine.
  1082      * Machine.
  1083      *
  1083      *
  1084      *<p> The <code>URLStreamHandlerFactory</code> instance is used to
  1084      *<p> The {@code URLStreamHandlerFactory} instance is used to
  1085      *construct a stream protocol handler from a protocol name.
  1085      *construct a stream protocol handler from a protocol name.
  1086      *
  1086      *
  1087      * <p> If there is a security manager, this method first calls
  1087      * <p> If there is a security manager, this method first calls
  1088      * the security manager's <code>checkSetFactory</code> method
  1088      * the security manager's {@code checkSetFactory} method
  1089      * to ensure the operation is allowed.
  1089      * to ensure the operation is allowed.
  1090      * This could result in a SecurityException.
  1090      * This could result in a SecurityException.
  1091      *
  1091      *
  1092      * @param      fac   the desired factory.
  1092      * @param      fac   the desired factory.
  1093      * @exception  Error  if the application has already set a factory.
  1093      * @exception  Error  if the application has already set a factory.
  1094      * @exception  SecurityException  if a security manager exists and its
  1094      * @exception  SecurityException  if a security manager exists and its
  1095      *             <code>checkSetFactory</code> method doesn't allow
  1095      *             {@code checkSetFactory} method doesn't allow
  1096      *             the operation.
  1096      *             the operation.
  1097      * @see        java.net.URL#URL(java.lang.String, java.lang.String,
  1097      * @see        java.net.URL#URL(java.lang.String, java.lang.String,
  1098      *             int, java.lang.String)
  1098      *             int, java.lang.String)
  1099      * @see        java.net.URLStreamHandlerFactory
  1099      * @see        java.net.URLStreamHandlerFactory
  1100      * @see        SecurityManager#checkSetFactory
  1100      * @see        SecurityManager#checkSetFactory