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