jdk/src/java.base/share/classes/java/net/URL.java
author msheppar
Fri, 12 Aug 2016 10:37:15 +0100
changeset 43199 e7fc78485b36
parent 39057 3ad96930e68b
child 43800 1dc22b50717a
permissions -rw-r--r--
8161228: URL objects with custom protocol handlers have port changed after deserializing Reviewed-by: chegar, rriggs, coffeys
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
35280
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
     2
 * Copyright (c) 1995, 2015, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3450
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3450
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3450
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3450
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3450
diff changeset
    23
 * questions.
2
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;
29097
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
    30
import java.net.spi.URLStreamHandlerProvider;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
    31
import java.security.AccessController;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
    32
import java.security.PrivilegedAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Hashtable;
35280
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
    34
import java.io.InvalidObjectException;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
    35
import java.io.ObjectStreamException;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
    36
import java.io.ObjectStreamField;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
    37
import java.io.ObjectInputStream.GetField;
29097
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
    38
import java.util.Iterator;
35381
2545709d9a45 8147962: URL should handle lower-casing of protocol locale-independently
redestad
parents: 35280
diff changeset
    39
import java.util.Locale;
29097
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
    40
import java.util.NoSuchElementException;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
    41
import java.util.ServiceConfigurationError;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
    42
import java.util.ServiceLoader;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
    43
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import sun.security.util.SecurityConstants;
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 36511
diff changeset
    45
import sun.security.action.GetPropertyAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
    48
 * Class {@code URL} represents a Uniform Resource
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Locator, a pointer to a "resource" on the World
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * Wide Web. A resource can be something as simple as a file or a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * directory, or it can be a reference to a more complicated object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * such as a query to a database or to a search engine. More
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * information on the types of URLs and their formats can be found at:
15309
b585dca79d0b 7017962: Obsolete link is used in URL class level spec
khazra
parents: 14342
diff changeset
    54
 * <a href=
b585dca79d0b 7017962: Obsolete link is used in URL class level spec
khazra
parents: 14342
diff changeset
    55
 * "http://web.archive.org/web/20051219043731/http://archive.ncsa.uiuc.edu/SDG/Software/Mosaic/Demo/url-primer.html">
b585dca79d0b 7017962: Obsolete link is used in URL class level spec
khazra
parents: 14342
diff changeset
    56
 * <i>Types of URL</i></a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <p>
15309
b585dca79d0b 7017962: Obsolete link is used in URL class level spec
khazra
parents: 14342
diff changeset
    58
 * In general, a URL can be broken into several parts. Consider the
b585dca79d0b 7017962: Obsolete link is used in URL class level spec
khazra
parents: 14342
diff changeset
    59
 * following example:
b585dca79d0b 7017962: Obsolete link is used in URL class level spec
khazra
parents: 14342
diff changeset
    60
 * <blockquote><pre>
b585dca79d0b 7017962: Obsolete link is used in URL class level spec
khazra
parents: 14342
diff changeset
    61
 *     http://www.example.com/docs/resource1.html
b585dca79d0b 7017962: Obsolete link is used in URL class level spec
khazra
parents: 14342
diff changeset
    62
 * </pre></blockquote>
b585dca79d0b 7017962: Obsolete link is used in URL class level spec
khazra
parents: 14342
diff changeset
    63
 * <p>
b585dca79d0b 7017962: Obsolete link is used in URL class level spec
khazra
parents: 14342
diff changeset
    64
 * The URL above indicates that the protocol to use is
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
    65
 * {@code http} (HyperText Transfer Protocol) and that the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * information resides on a host machine named
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
    67
 * {@code www.example.com}. The information on that host
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
    68
 * machine is named {@code /docs/resource1.html}. The exact
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * meaning of this name on the host machine is both protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * dependent and host dependent. The information normally resides in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * a file, but it could be generated on the fly. This component of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * the URL is called the <i>path</i> component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * A URL can optionally specify a "port", which is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * port number to which the TCP connection is made on the remote host
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * machine. If the port is not specified, the default port for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * the protocol is used instead. For example, the default port for
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
    78
 * {@code http} is {@code 80}. An alternative port could be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * specified as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * <blockquote><pre>
15309
b585dca79d0b 7017962: Obsolete link is used in URL class level spec
khazra
parents: 14342
diff changeset
    81
 *     http://www.example.com:1080/docs/resource1.html
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
    84
 * The syntax of {@code URL} is defined by  <a
708
a780486c413c 6630348: Invalid html tags (extra double quote)
chegar
parents: 2
diff changeset
    85
 * href="http://www.ietf.org/rfc/rfc2396.txt"><i>RFC&nbsp;2396: Uniform
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * Resource Identifiers (URI): Generic Syntax</i></a>, amended by <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * href="http://www.ietf.org/rfc/rfc2732.txt"><i>RFC&nbsp;2732: Format for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * Literal IPv6 Addresses in URLs</i></a>. The Literal IPv6 address format
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * also supports scope_ids. The syntax and usage of scope_ids is described
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * <a href="Inet6Address.html#scoped">here</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * A URL may have appended to it a "fragment", also known
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * as a "ref" or a "reference". The fragment is indicated by the sharp
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * sign character "#" followed by more characters. For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *     http://java.sun.com/index.html#chapter1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * This fragment is not technically part of the URL. Rather, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * indicates that after the specified resource is retrieved, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * application is specifically interested in that part of the
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   102
 * document that has the tag {@code chapter1} attached to it. The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * meaning of a tag is resource specific.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * An application can also specify a "relative URL",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * which contains only enough information to reach the resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * relative to another URL. Relative URLs are frequently used within
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * HTML pages. For example, if the contents of the URL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *     http://java.sun.com/index.html
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * contained within it the relative URL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *     FAQ.html
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * it would be a shorthand for:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *     http://java.sun.com/FAQ.html
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * The relative URL need not specify all the components of a URL. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * the protocol, host name, or port number is missing, the value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * inherited from the fully specified URL. The file component must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * specified. The optional fragment is not inherited.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * The URL class does not itself encode or decode any URL components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * according to the escaping mechanism defined in RFC2396. It is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * responsibility of the caller to encode any fields, which need to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * escaped prior to calling URL, and also to decode any escaped fields,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * that are returned from URL. Furthermore, because URL has no knowledge
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * of URL escaping, it does not recognise equivalence between the encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * or decoded form of the same URL. For example, the two URLs:<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * <pre>    http://foo.com/hello world/ and http://foo.com/hello%20world</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * would be considered not equal to each other.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * Note, the {@link java.net.URI} class does perform escaping of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * component fields in certain circumstances. The recommended way
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * to manage the encoding and decoding of URLs is to use {@link java.net.URI},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * and to convert between these two classes using {@link #toURI()} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * {@link URI#toURL()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * The {@link URLEncoder} and {@link URLDecoder} classes can also be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * used, but only for HTML form encoding, which is not the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * as the encoding scheme defined in RFC2396.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * @author  James Gosling
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24260
diff changeset
   147
 * @since 1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
public final class URL implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
35280
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
   151
    static final String BUILTIN_HANDLERS_PREFIX = "sun.net.www.protocol";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    static final long serialVersionUID = -7627629688361524110L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * The property which specifies the package prefix list to be scanned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * for protocol handlers.  The value of this property (if any) should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * be a vertical bar delimited list of package names to search through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * for a protocol handler to load.  The policy of this class is that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * all protocol handlers will be in a class called <protocolname>.Handler,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * and each package in the list is examined in turn for a matching
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * handler.  If none are found (or the property is not specified), the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * default package prefix, sun.net.www.protocol, is used.  The search
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * proceeds from the first package in the list to the last and stops
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * when a match is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    private static final String protocolPathProp = "java.protocol.handler.pkgs";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * The protocol to use (ftp, http, nntp, ... etc.) .
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    private String protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * The host name to connect to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    private String host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * The protocol port to connect to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    private int port = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   187
     * The specified file name on that host. {@code file} is
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   188
     * defined as {@code path[?query]}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    private String file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * The query part of this URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    private transient String query;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * The authority part of this URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    private String authority;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * The path part of this URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    private transient String path;
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 userinfo part of this URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    private transient String userInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * # reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    private String ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * The host's IP address, used in equals and hashCode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * Computed on demand. An uninitialized or unknown hostAddress is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    transient InetAddress hostAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * The URLStreamHandler for this URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    transient URLStreamHandler handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /* Our hash code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    private int hashCode = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
35280
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
   236
    private transient UrlDeserializedState tempState;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
   237
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   239
     * Creates a {@code URL} object from the specified
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   240
     * {@code protocol}, {@code host}, {@code port}
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   241
     * number, and {@code file}.<p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   243
     * {@code host} can be expressed as a host name or a literal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * IP address. If IPv6 literal address is used, it should be
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   245
     * enclosed in square brackets ({@code '['} and {@code ']'}), as
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * specified by <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * href="http://www.ietf.org/rfc/rfc2732.txt">RFC&nbsp;2732</a>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * However, the literal IPv6 address format defined in <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * href="http://www.ietf.org/rfc/rfc2373.txt"><i>RFC&nbsp;2373: IP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * Version 6 Addressing Architecture</i></a> is also accepted.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   252
     * Specifying a {@code port} number of {@code -1}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * indicates that the URL should use the default port for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * protocol.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * If this is the first URL object being created with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * protocol, a <i>stream protocol handler</i> object, an instance of
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   258
     * class {@code URLStreamHandler}, is created for that protocol:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * <li>If the application has previously set up an instance of
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   261
     *     {@code URLStreamHandlerFactory} as the stream handler factory,
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   262
     *     then the {@code createURLStreamHandler} method of that instance
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *     is called with the protocol string as an argument to create the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *     stream protocol handler.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   265
     * <li>If no {@code URLStreamHandlerFactory} has yet been set up,
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   266
     *     or if the factory's {@code createURLStreamHandler} method
29097
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
   267
     *     returns {@code null}, then the {@linkplain java.util.ServiceLoader
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
   268
     *     ServiceLoader} mechanism is used to locate {@linkplain
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
   269
     *     java.net.spi.URLStreamHandlerProvider URLStreamHandlerProvider}
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
   270
     *     implementations using the system class
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
   271
     *     loader. The order that providers are located is implementation
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
   272
     *     specific, and an implementation is free to cache the located
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
   273
     *     providers. A {@linkplain java.util.ServiceConfigurationError
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
   274
     *     ServiceConfigurationError}, {@code Error} or {@code RuntimeException}
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
   275
     *     thrown from the {@code createURLStreamHandler}, if encountered, will
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
   276
     *     be propagated to the calling thread. The {@code
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
   277
     *     createURLStreamHandler} method of each provider, if instantiated, is
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
   278
     *     invoked, with the protocol string, until a provider returns non-null,
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
   279
     *     or all providers have been exhausted.
29977
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
   280
     * <li>If the previous step fails to find a protocol handler, the
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
   281
     *     constructor reads the value of the system property:
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
   282
     *     <blockquote>{@code
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
   283
     *         java.protocol.handler.pkgs
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
   284
     *     }</blockquote>
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
   285
     *     If the value of that system property is not {@code null},
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
   286
     *     it is interpreted as a list of packages separated by a vertical
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
   287
     *     slash character '{@code |}'. The constructor tries to load
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
   288
     *     the class named:
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
   289
     *     <blockquote>{@code
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
   290
     *         <package>.<protocol>.Handler
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
   291
     *     }</blockquote>
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
   292
     *     where {@code <package>} is replaced by the name of the package
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
   293
     *     and {@code <protocol>} is replaced by the name of the protocol.
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
   294
     *     If this class does not exist, or if the class exists but it is not
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
   295
     *     a subclass of {@code URLStreamHandler}, then the next package
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
   296
     *     in the list is tried.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * <li>If the previous step fails to find a protocol handler, then the
24260
55a72a7e0cf9 8039470: java.net Content Handler API incorrectly specifies implementation specific location of handler classes
chegar
parents: 23720
diff changeset
   298
     *     constructor tries to load a built-in protocol handler.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *     If this class does not exist, or if the class exists but it is not a
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   300
     *     subclass of {@code URLStreamHandler}, then a
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   301
     *     {@code MalformedURLException} is thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * <p>Protocol handlers for the following protocols are guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * to exist on the search path :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * <blockquote><pre>
14197
1b78ddd3644e 8000941: Remove ftp from the required list of protocol handlers
alanb
parents: 10596
diff changeset
   307
     *     http, https, file, and jar
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * </pre></blockquote>
29097
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
   309
     * Protocol handlers for additional protocols may also be  available.
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
   310
     * Some protocol handlers, for example those used for loading platform
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
   311
     * classes or classes on the class path, may not be overridden. The details
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
   312
     * of such restrictions, and when those restrictions apply (during
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
   313
     * initialization of the runtime for example), are implementation specific
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
   314
     * and therefore not specified
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * <p>No validation of the inputs is performed by this constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @param      protocol   the name of the protocol to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @param      host       the name of the host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @param      port       the port number on the host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @param      file       the file on the host
34776
9229b3f76c40 4906983: java.net.URL constructors throw MalformedURLException in undocumented way
sebastian
parents: 29986
diff changeset
   322
     * @exception  MalformedURLException  if an unknown protocol or the port
9229b3f76c40 4906983: java.net.URL constructors throw MalformedURLException in undocumented way
sebastian
parents: 29986
diff changeset
   323
     *                  is a negative number other than -1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @see        java.lang.System#getProperty(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @see        java.net.URL#setURLStreamHandlerFactory(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *                  java.net.URLStreamHandlerFactory)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @see        java.net.URLStreamHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @see        java.net.URLStreamHandlerFactory#createURLStreamHandler(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *                  java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    public URL(String protocol, String host, int port, String file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        throws MalformedURLException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        this(protocol, host, port, file, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   338
     * Creates a URL from the specified {@code protocol}
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   339
     * name, {@code host} name, and {@code file} name. The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * default port for the specified protocol is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * <p>
34776
9229b3f76c40 4906983: java.net.URL constructors throw MalformedURLException in undocumented way
sebastian
parents: 29986
diff changeset
   342
     * This constructor is equivalent to the four-argument
9229b3f76c40 4906983: java.net.URL constructors throw MalformedURLException in undocumented way
sebastian
parents: 29986
diff changeset
   343
     * constructor with the only difference of using the
9229b3f76c40 4906983: java.net.URL constructors throw MalformedURLException in undocumented way
sebastian
parents: 29986
diff changeset
   344
     * default port for the specified protocol.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * No validation of the inputs is performed by this constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @param      protocol   the name of the protocol to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @param      host       the name of the host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @param      file       the file on the host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @exception  MalformedURLException  if an unknown protocol is specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @see        java.net.URL#URL(java.lang.String, java.lang.String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *                  int, java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public URL(String protocol, String host, String file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            throws MalformedURLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        this(protocol, host, -1, file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   361
     * Creates a {@code URL} object from the specified
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   362
     * {@code protocol}, {@code host}, {@code port}
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   363
     * number, {@code file}, and {@code handler}. Specifying
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   364
     * a {@code port} number of {@code -1} indicates that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * the URL should use the default port for the protocol. Specifying
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   366
     * a {@code handler} of {@code null} indicates that the URL
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * should use a default stream handler for the protocol, as outlined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * for:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *     java.net.URL#URL(java.lang.String, java.lang.String, int,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *                      java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * <p>If the handler is not null and there is a security manager,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   373
     * the security manager's {@code checkPermission}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * method is called with a
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   375
     * {@code NetPermission("specifyStreamHandler")} permission.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * This may result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * No validation of the inputs is performed by this constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @param      protocol   the name of the protocol to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * @param      host       the name of the host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * @param      port       the port number on the host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @param      file       the file on the host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @param      handler    the stream handler for the URL.
34776
9229b3f76c40 4906983: java.net.URL constructors throw MalformedURLException in undocumented way
sebastian
parents: 29986
diff changeset
   385
     * @exception  MalformedURLException  if an unknown protocol or the port
9229b3f76c40 4906983: java.net.URL constructors throw MalformedURLException in undocumented way
sebastian
parents: 29986
diff changeset
   386
                        is a negative number other than -1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * @exception  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *        if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   389
     *        {@code checkPermission} method doesn't allow
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *        specifying a stream handler explicitly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @see        java.lang.System#getProperty(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * @see        java.net.URL#setURLStreamHandlerFactory(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *                  java.net.URLStreamHandlerFactory)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @see        java.net.URLStreamHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @see        java.net.URLStreamHandlerFactory#createURLStreamHandler(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     *                  java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * @see        SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @see        java.net.NetPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    public URL(String protocol, String host, int port, String file,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
               URLStreamHandler handler) throws MalformedURLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        if (handler != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                // check for permission to specify a handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                checkSpecifyHandler(sm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
35381
2545709d9a45 8147962: URL should handle lower-casing of protocol locale-independently
redestad
parents: 35280
diff changeset
   410
        protocol = protocol.toLowerCase(Locale.ROOT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        this.protocol = protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        if (host != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
             * if host is a literal IPv6 address,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
             * we will make it conform to RFC 2732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            if (host.indexOf(':') >= 0 && !host.startsWith("[")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                host = "["+host+"]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            this.host = host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            if (port < -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                throw new MalformedURLException("Invalid port number :" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                                                    port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            this.port = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            authority = (port == -1) ? host : host + ":" + port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
39057
3ad96930e68b 8159745: Remove java.net.Parts
redestad
parents: 37782
diff changeset
   431
        int index = file.indexOf('#');
3ad96930e68b 8159745: Remove java.net.Parts
redestad
parents: 37782
diff changeset
   432
        this.ref = index < 0 ? null : file.substring(index + 1);
3ad96930e68b 8159745: Remove java.net.Parts
redestad
parents: 37782
diff changeset
   433
        file = index < 0 ? file : file.substring(0, index);
3ad96930e68b 8159745: Remove java.net.Parts
redestad
parents: 37782
diff changeset
   434
        int q = file.lastIndexOf('?');
3ad96930e68b 8159745: Remove java.net.Parts
redestad
parents: 37782
diff changeset
   435
        if (q != -1) {
3ad96930e68b 8159745: Remove java.net.Parts
redestad
parents: 37782
diff changeset
   436
            this.query = file.substring(q + 1);
3ad96930e68b 8159745: Remove java.net.Parts
redestad
parents: 37782
diff changeset
   437
            this.path = file.substring(0, q);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            this.file = path + "?" + query;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        } else {
39057
3ad96930e68b 8159745: Remove java.net.Parts
redestad
parents: 37782
diff changeset
   440
            this.path = file;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            this.file = path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        // Note: we don't do validation of the URL here. Too risky to change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        // right now, but worth considering for future reference. -br
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        if (handler == null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            (handler = getURLStreamHandler(protocol)) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            throw new MalformedURLException("unknown protocol: " + protocol);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        this.handler = handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   454
     * Creates a {@code URL} object from the {@code String}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * representation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * This constructor is equivalent to a call to the two-argument
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   458
     * constructor with a {@code null} first argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   460
     * @param      spec   the {@code String} to parse as a URL.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * @exception  MalformedURLException  if no protocol is specified, or an
34776
9229b3f76c40 4906983: java.net.URL constructors throw MalformedURLException in undocumented way
sebastian
parents: 29986
diff changeset
   462
     *               unknown protocol is found, or {@code spec} is {@code null},
9229b3f76c40 4906983: java.net.URL constructors throw MalformedURLException in undocumented way
sebastian
parents: 29986
diff changeset
   463
     *               or the parsed URL fails to comply with the specific syntax
9229b3f76c40 4906983: java.net.URL constructors throw MalformedURLException in undocumented way
sebastian
parents: 29986
diff changeset
   464
     *               of the associated protocol.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * @see        java.net.URL#URL(java.net.URL, java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    public URL(String spec) throws MalformedURLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        this(null, spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * Creates a URL by parsing the given spec within a specified context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * The new URL is created from the given context URL and the spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * argument as described in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * RFC2396 &quot;Uniform Resource Identifiers : Generic * Syntax&quot; :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *          &lt;scheme&gt;://&lt;authority&gt;&lt;path&gt;?&lt;query&gt;#&lt;fragment&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * The reference is parsed into the scheme, authority, path, query and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * fragment parts. If the path component is empty and the scheme,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * authority, and query components are undefined, then the new URL is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * reference to the current document. Otherwise, the fragment and query
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * parts present in the spec are used in the new URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * If the scheme component is defined in the given spec and does not match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * the scheme of the context, then the new URL is created as an absolute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * URL based on the spec alone. Otherwise the scheme component is inherited
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * from the context URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * If the authority component is present in the spec then the spec is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * treated as absolute and the spec authority and path will replace the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * context authority and path. If the authority component is absent in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * spec then the authority of the new URL will be inherited from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * If the spec's path component begins with a slash character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * &quot;/&quot; then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * path is treated as absolute and the spec path replaces the context path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * Otherwise, the path is treated as a relative path and is appended to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * context path, as described in RFC2396. Also, in this case,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * the path is canonicalized through the removal of directory
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 19069
diff changeset
   504
     * changes made by occurrences of &quot;..&quot; and &quot;.&quot;.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * For a more detailed description of URL parsing, refer to RFC2396.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * @param      context   the context in which to parse the specification.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   509
     * @param      spec      the {@code String} to parse as a URL.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * @exception  MalformedURLException  if no protocol is specified, or an
34776
9229b3f76c40 4906983: java.net.URL constructors throw MalformedURLException in undocumented way
sebastian
parents: 29986
diff changeset
   511
     *               unknown protocol is found, or {@code spec} is {@code null},
9229b3f76c40 4906983: java.net.URL constructors throw MalformedURLException in undocumented way
sebastian
parents: 29986
diff changeset
   512
     *               or the parsed URL fails to comply with the specific syntax
9229b3f76c40 4906983: java.net.URL constructors throw MalformedURLException in undocumented way
sebastian
parents: 29986
diff changeset
   513
     *               of the associated protocol.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * @see        java.net.URL#URL(java.lang.String, java.lang.String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *                  int, java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * @see        java.net.URLStreamHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * @see        java.net.URLStreamHandler#parseURL(java.net.URL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *                  java.lang.String, int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    public URL(URL context, String spec) throws MalformedURLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        this(context, spec, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * Creates a URL by parsing the given spec with the specified handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * within a specified context. If the handler is null, the parsing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * occurs as with the two argument constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * @param      context   the context in which to parse the specification.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   530
     * @param      spec      the {@code String} to parse as a URL.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * @param      handler   the stream handler for the URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * @exception  MalformedURLException  if no protocol is specified, or an
34776
9229b3f76c40 4906983: java.net.URL constructors throw MalformedURLException in undocumented way
sebastian
parents: 29986
diff changeset
   533
     *               unknown protocol is found, or {@code spec} is {@code null},
9229b3f76c40 4906983: java.net.URL constructors throw MalformedURLException in undocumented way
sebastian
parents: 29986
diff changeset
   534
     *               or the parsed URL fails to comply with the specific syntax
9229b3f76c40 4906983: java.net.URL constructors throw MalformedURLException in undocumented way
sebastian
parents: 29986
diff changeset
   535
     *               of the associated protocol.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * @exception  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     *        if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   538
     *        {@code checkPermission} method doesn't allow
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     *        specifying a stream handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * @see        java.net.URL#URL(java.lang.String, java.lang.String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     *                  int, java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * @see        java.net.URLStreamHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * @see        java.net.URLStreamHandler#parseURL(java.net.URL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     *                  java.lang.String, int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    public URL(URL context, String spec, URLStreamHandler handler)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        throws MalformedURLException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        String original = spec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        int i, limit, c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        int start = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        String newProtocol = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        boolean aRef=false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        boolean isRelative = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        // Check for permission to specify a handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        if (handler != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                checkSpecifyHandler(sm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            limit = spec.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            while ((limit > 0) && (spec.charAt(limit - 1) <= ' ')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                limit--;        //eliminate trailing whitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            while ((start < limit) && (spec.charAt(start) <= ' ')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                start++;        // eliminate leading whitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            if (spec.regionMatches(true, start, "url:", 0, 4)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                start += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            if (start < spec.length() && spec.charAt(start) == '#') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                /* we're assuming this is a ref relative to the context URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                 * This means protocols cannot start w/ '#', but we must parse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                 * ref URL's like: "hello:there" w/ a ':' in them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                aRef=true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            for (i = start ; !aRef && (i < limit) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                     ((c = spec.charAt(i)) != '/') ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                if (c == ':') {
35381
2545709d9a45 8147962: URL should handle lower-casing of protocol locale-independently
redestad
parents: 35280
diff changeset
   586
                    String s = spec.substring(start, i).toLowerCase(Locale.ROOT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                    if (isValidProtocol(s)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                        newProtocol = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                        start = i + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            // Only use our context if the protocols match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            protocol = newProtocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            if ((context != null) && ((newProtocol == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                            newProtocol.equalsIgnoreCase(context.protocol))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                // inherit the protocol handler from the context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                // if not specified to the constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                if (handler == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                    handler = context.handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                // If the context is a hierarchical URL scheme and the spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                // contains a matching scheme then maintain backwards
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                // compatibility and treat it as if the spec didn't contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                // the scheme; see 5.2.3 of RFC2396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                if (context.path != null && context.path.startsWith("/"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                    newProtocol = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                if (newProtocol == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                    protocol = context.protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                    authority = context.authority;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                    userInfo = context.userInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                    host = context.host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                    port = context.port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                    file = context.file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                    path = context.path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                    isRelative = true;
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
            if (protocol == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                throw new MalformedURLException("no protocol: "+original);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            // Get the protocol handler if not specified or the protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            // of the context could not be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            if (handler == null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                (handler = getURLStreamHandler(protocol)) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                throw new MalformedURLException("unknown protocol: "+protocol);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            this.handler = handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            i = spec.indexOf('#', start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            if (i >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                ref = spec.substring(i + 1, limit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                limit = i;
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
             * Handle special case inheritance of query and fragment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
             * implied by RFC2396 section 5.2.2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            if (isRelative && start == limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                query = context.query;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                if (ref == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                    ref = context.ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            handler.parseURL(this, spec, start, limit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        } catch(MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            MalformedURLException exception = new MalformedURLException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            exception.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            throw exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
35393
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
   665
    /**
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
   666
     * Creates a URL from a URI, as if by invoking {@code uri.toURL()}.
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
   667
     *
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
   668
     * @see java.net.URI#toURL()
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
   669
     */
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
   670
    static URL fromURI(URI uri) throws MalformedURLException {
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
   671
        if (!uri.isAbsolute()) {
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
   672
            throw new IllegalArgumentException("URI is not absolute");
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
   673
        }
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
   674
        String protocol = uri.getScheme();
35399
babca4eef83f 8148626: URI.toURL needs to use protocol Handler to parse file URIs
redestad
parents: 35393
diff changeset
   675
babca4eef83f 8148626: URI.toURL needs to use protocol Handler to parse file URIs
redestad
parents: 35393
diff changeset
   676
        // In general we need to go via Handler.parseURL, but for the jrt
babca4eef83f 8148626: URI.toURL needs to use protocol Handler to parse file URIs
redestad
parents: 35393
diff changeset
   677
        // protocol we enforce that the Handler is not overrideable and can
babca4eef83f 8148626: URI.toURL needs to use protocol Handler to parse file URIs
redestad
parents: 35393
diff changeset
   678
        // optimize URI to URL conversion.
babca4eef83f 8148626: URI.toURL needs to use protocol Handler to parse file URIs
redestad
parents: 35393
diff changeset
   679
        //
babca4eef83f 8148626: URI.toURL needs to use protocol Handler to parse file URIs
redestad
parents: 35393
diff changeset
   680
        // Case-sensitive comparison for performance; malformed protocols will
babca4eef83f 8148626: URI.toURL needs to use protocol Handler to parse file URIs
redestad
parents: 35393
diff changeset
   681
        // be handled correctly by the slow path.
babca4eef83f 8148626: URI.toURL needs to use protocol Handler to parse file URIs
redestad
parents: 35393
diff changeset
   682
        if (protocol.equals("jrt") && !uri.isOpaque()
babca4eef83f 8148626: URI.toURL needs to use protocol Handler to parse file URIs
redestad
parents: 35393
diff changeset
   683
                && uri.getRawFragment() == null) {
35393
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
   684
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
   685
            String query = uri.getRawQuery();
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
   686
            String path = uri.getRawPath();
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
   687
            String file = (query == null) ? path : path + "?" + query;
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
   688
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
   689
            // URL represent undefined host as empty string while URI use null
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
   690
            String host = uri.getHost();
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
   691
            if (host == null) {
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
   692
                host = "";
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
   693
            }
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
   694
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
   695
            int port = uri.getPort();
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
   696
35399
babca4eef83f 8148626: URI.toURL needs to use protocol Handler to parse file URIs
redestad
parents: 35393
diff changeset
   697
            return new URL("jrt", host, port, file, null);
35393
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
   698
        } else {
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
   699
            return new URL((URL)null, uri.toString(), null);
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
   700
        }
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
   701
    }
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
   702
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * Returns true if specified string is a valid protocol name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    private boolean isValidProtocol(String protocol) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        int len = protocol.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        if (len < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        char c = protocol.charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        if (!Character.isLetter(c))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        for (int i = 1; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            c = protocol.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            if (!Character.isLetterOrDigit(c) && c != '.' && c != '+' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                c != '-') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * Checks for permission to specify a stream handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    private void checkSpecifyHandler(SecurityManager sm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        sm.checkPermission(SecurityConstants.SPECIFY_HANDLER_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * Sets the fields of the URL. This is not a public method so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * only URLStreamHandlers can modify URL fields. URLs are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * otherwise constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * @param protocol the name of the protocol to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * @param host the name of the host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
       @param port the port number on the host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * @param file the file on the host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * @param ref the internal reference in the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     */
16055
311a101eb72c 4880778: URL final class has protected methods
jzavgren
parents: 15309
diff changeset
   741
    void set(String protocol, String host, int port,
311a101eb72c 4880778: URL final class has protected methods
jzavgren
parents: 15309
diff changeset
   742
             String file, String ref) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            this.protocol = protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            this.host = host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            authority = port == -1 ? host : host + ":" + port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            this.port = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            this.file = file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            this.ref = ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            /* This is very important. We must recompute this after the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
             * URL has been changed. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            hashCode = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            hostAddress = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            int q = file.lastIndexOf('?');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            if (q != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                query = file.substring(q+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                path = file.substring(0, q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                path = file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * Sets the specified 8 fields of the URL. This is not a public method so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * that only URLStreamHandlers can modify URL fields. URLs are otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * @param protocol the name of the protocol to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * @param host the name of the host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * @param port the port number on the host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * @param authority the authority part for the url
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * @param userInfo the username and password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * @param path the file on the host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * @param ref the internal reference in the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * @param query the query part of this URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     */
16055
311a101eb72c 4880778: URL final class has protected methods
jzavgren
parents: 15309
diff changeset
   778
    void set(String protocol, String host, int port,
311a101eb72c 4880778: URL final class has protected methods
jzavgren
parents: 15309
diff changeset
   779
             String authority, String userInfo, String path,
311a101eb72c 4880778: URL final class has protected methods
jzavgren
parents: 15309
diff changeset
   780
             String query, String ref) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            this.protocol = protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            this.host = host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            this.port = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            this.file = query == null ? path : path + "?" + query;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            this.userInfo = userInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            this.path = path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            this.ref = ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            /* This is very important. We must recompute this after the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
             * URL has been changed. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            hashCode = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            hostAddress = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            this.query = query;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            this.authority = authority;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   799
     * Gets the query part of this {@code URL}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   801
     * @return  the query part of this {@code URL},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * or <CODE>null</CODE> if one does not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    public String getQuery() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        return query;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   810
     * Gets the path part of this {@code URL}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   812
     * @return  the path part of this {@code URL}, or an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * empty string if one does not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    public String getPath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        return path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   821
     * Gets the userInfo part of this {@code URL}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   823
     * @return  the userInfo part of this {@code URL}, or
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * <CODE>null</CODE> if one does not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    public String getUserInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        return userInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   832
     * Gets the authority part of this {@code URL}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   834
     * @return  the authority part of this {@code URL}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    public String getAuthority() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        return authority;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   842
     * Gets the port number of this {@code URL}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * @return  the port number, or -1 if the port is not set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    public int getPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        return port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * Gets the default port number of the protocol associated
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   852
     * with this {@code URL}. If the URL scheme or the URLStreamHandler
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * for the URL do not define a default port number,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * then -1 is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * @return  the port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    public int getDefaultPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        return handler.getDefaultPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   864
     * Gets the protocol name of this {@code URL}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   866
     * @return  the protocol of this {@code URL}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    public String getProtocol() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        return protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   873
     * Gets the host name of this {@code URL}, if applicable.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * The format of the host conforms to RFC 2732, i.e. for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * literal IPv6 address, this method will return the IPv6 address
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   876
     * enclosed in square brackets ({@code '['} and {@code ']'}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   878
     * @return  the host name of this {@code URL}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    public String getHost() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        return host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   885
     * Gets the file name of this {@code URL}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * The returned file portion will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * the same as <CODE>getPath()</CODE>, plus the concatenation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * the value of <CODE>getQuery()</CODE>, if any. If there is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * no query portion, this method and <CODE>getPath()</CODE> will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * return identical results.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   892
     * @return  the file name of this {@code URL},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * or an empty string if one does not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
    public String getFile() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        return file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * Gets the anchor (also known as the "reference") of this
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   901
     * {@code URL}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * @return  the anchor (also known as the "reference") of this
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   904
     *          {@code URL}, or <CODE>null</CODE> if one does not exist
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    public String getRef() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        return ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * Compares this URL for equality with another object.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * If the given object is not a URL then this method immediately returns
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   914
     * {@code false}.<p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * Two URL objects are equal if they have the same protocol, reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * equivalent hosts, have the same port number on the host, and the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * file and fragment of the file.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * Two hosts are considered equivalent if both host names can be resolved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * into the same IP addresses; else if either host name can't be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * resolved, the host names must be equal without regard to case; or both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * host names equal to null.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * Since hosts comparison requires name resolution, this operation is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * blocking operation. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   928
     * Note: The defined behavior for {@code equals} is known to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * be inconsistent with virtual hosting in HTTP.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * @param   obj   the URL to compare against.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   932
     * @return  {@code true} if the objects are the same;
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   933
     *          {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        if (!(obj instanceof URL))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        URL u2 = (URL)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        return handler.equals(this, u2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * Creates an integer suitable for hash table indexing.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * The hash code is based upon all the URL components relevant for URL
23720
7d5147c21927 8039172: Tidy warnings cleanup for java.net, java.math, java.time, java.rmi
yan
parents: 22119
diff changeset
   947
     * comparison. As such, this operation is a blocking operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   949
     * @return  a hash code for this {@code URL}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    public synchronized int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        if (hashCode != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            return hashCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        hashCode = handler.hashCode(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        return hashCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * Compares two URLs, excluding the fragment component.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   962
     * Returns {@code true} if this {@code URL} and the
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   963
     * {@code other} argument are equal without taking the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * fragment component into consideration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   966
     * @param   other   the {@code URL} to compare against.
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   967
     * @return  {@code true} if they reference the same remote object;
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   968
     *          {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
    public boolean sameFile(URL other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        return handler.sameFile(this, other);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   975
     * Constructs a string representation of this {@code URL}. The
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   976
     * string is created by calling the {@code toExternalForm}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * method of the stream protocol handler for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * @return  a string representation of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * @see     java.net.URL#URL(java.lang.String, java.lang.String, int,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     *                  java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * @see     java.net.URLStreamHandler#toExternalForm(java.net.URL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        return toExternalForm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   989
     * Constructs a string representation of this {@code URL}. The
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   990
     * string is created by calling the {@code toExternalForm}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * method of the stream protocol handler for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * @return  a string representation of this object.
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.URLStreamHandler#toExternalForm(java.net.URL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    public String toExternalForm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        return handler.toExternalForm(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * Returns a {@link java.net.URI} equivalent to this URL.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
  1004
     * This method functions in the same way as {@code new URI (this.toString())}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * <p>Note, any URL instance that complies with RFC 2396 can be converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * to a URI. However, some URLs that are not strictly in compliance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * can not be converted to a URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * @exception URISyntaxException if this URL is not formatted strictly according to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     *            to RFC2396 and cannot be converted to a URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * @return    a URI instance equivalent to this URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    public URI toURI() throws URISyntaxException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        return new URI (toString());
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
     * Returns a {@link java.net.URLConnection URLConnection} instance that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * represents a connection to the remote object referred to by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * {@code URL}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * <P>A new instance of {@linkplain java.net.URLConnection URLConnection} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * created every time when invoking the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * {@linkplain java.net.URLStreamHandler#openConnection(URL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * URLStreamHandler.openConnection(URL)} method of the protocol handler for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * this URL.</P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * <P>It should be noted that a URLConnection instance does not establish
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * the actual network connection on creation. This will happen only when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * calling {@linkplain java.net.URLConnection#connect() URLConnection.connect()}.</P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     * <P>If for the URL's protocol (such as HTTP or JAR), there
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     * exists a public, specialized URLConnection subclass belonging
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     * to one of the following packages or one of their subpackages:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     * java.lang, java.io, java.util, java.net, the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     * returned will be of that subclass. For example, for HTTP an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * HttpURLConnection will be returned, and for JAR a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * JarURLConnection will be returned.</P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * @return     a {@link java.net.URLConnection URLConnection} linking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     *             to the URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * @exception  IOException  if an I/O exception occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * @see        java.net.URL#URL(java.lang.String, java.lang.String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     *             int, java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
    public URLConnection openConnection() throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        return handler.openConnection(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * Same as {@link #openConnection()}, except that the connection will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * made through the specified proxy; Protocol handlers that do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * support proxing will ignore the proxy parameter and make a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * normal connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     *
22064
e3c4398c54cd 7018010: References to ProxySelector is without link
rriggs
parents: 21278
diff changeset
  1058
     * Invoking this method preempts the system's default
e3c4398c54cd 7018010: References to ProxySelector is without link
rriggs
parents: 21278
diff changeset
  1059
     * {@link java.net.ProxySelector ProxySelector} settings.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * @param      proxy the Proxy through which this connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     *             will be made. If direct connection is desired,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     *             Proxy.NO_PROXY should be specified.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
  1064
     * @return     a {@code URLConnection} to the URL.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * @exception  IOException  if an I/O exception occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * @exception  SecurityException if a security manager is present
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     *             and the caller doesn't have permission to connect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     *             to the proxy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     * @exception  IllegalArgumentException will be thrown if proxy is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     *             or proxy has the wrong type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     * @exception  UnsupportedOperationException if the subclass that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     *             implements the protocol handler doesn't support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     *             this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * @see        java.net.URL#URL(java.lang.String, java.lang.String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     *             int, java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * @see        java.net.URLConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * @see        java.net.URLStreamHandler#openConnection(java.net.URL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     *             java.net.Proxy)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * @since      1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
    public URLConnection openConnection(Proxy proxy)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        if (proxy == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            throw new IllegalArgumentException("proxy can not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
3442
02df74328591 6801497: Proxy is assumed to be immutable but is non-final
jccollet
parents: 715
diff changeset
  1087
        // Create a copy of Proxy as a security measure
3450
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 3442
diff changeset
  1088
        Proxy p = proxy == Proxy.NO_PROXY ? Proxy.NO_PROXY : sun.net.ApplicationProxy.create(proxy);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        SecurityManager sm = System.getSecurityManager();
3442
02df74328591 6801497: Proxy is assumed to be immutable but is non-final
jccollet
parents: 715
diff changeset
  1090
        if (p.type() != Proxy.Type.DIRECT && sm != null) {
02df74328591 6801497: Proxy is assumed to be immutable but is non-final
jccollet
parents: 715
diff changeset
  1091
            InetSocketAddress epoint = (InetSocketAddress) p.address();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            if (epoint.isUnresolved())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                sm.checkConnect(epoint.getHostName(), epoint.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                sm.checkConnect(epoint.getAddress().getHostAddress(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                                epoint.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        }
3442
02df74328591 6801497: Proxy is assumed to be immutable but is non-final
jccollet
parents: 715
diff changeset
  1098
        return handler.openConnection(this, p);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
  1102
     * Opens a connection to this {@code URL} and returns an
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
  1103
     * {@code InputStream} for reading from that connection. This
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * method is a shorthand for:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     *     openConnection().getInputStream()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     * @return     an input stream for reading from the URL connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     * @exception  IOException  if an I/O exception occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     * @see        java.net.URL#openConnection()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     * @see        java.net.URLConnection#getInputStream()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
    public final InputStream openStream() throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        return openConnection().getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     * Gets the contents of this URL. This method is a shorthand for:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     *     openConnection().getContent()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * @return     the contents of this URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * @exception  IOException  if an I/O exception occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * @see        java.net.URLConnection#getContent()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
    public final Object getContent() throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        return openConnection().getContent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     * Gets the contents of this URL. This method is a shorthand for:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * <blockquote><pre>
22119
da8b8a10483f 8031326: Use Class<?> rather than Class in java.net method signatures
darcy
parents: 22064
diff changeset
  1135
     *     openConnection().getContent(classes)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * @param classes an array of Java types
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     * @return     the content object of this URL that is the first match of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     *               the types specified in the classes array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     *               null if none of the requested types are supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     * @exception  IOException  if an I/O exception occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * @see        java.net.URLConnection#getContent(Class[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     */
22119
da8b8a10483f 8031326: Use Class<?> rather than Class in java.net method signatures
darcy
parents: 22064
diff changeset
  1146
    public final Object getContent(Class<?>[] classes)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
    throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        return openConnection().getContent(classes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * The URLStreamHandler factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     */
25398
f7bdeeb2ca57 8049220: URL.factory data race
plevart
parents: 24865
diff changeset
  1154
    private static volatile URLStreamHandlerFactory factory;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
  1157
     * Sets an application's {@code URLStreamHandlerFactory}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * This method can be called at most once in a given Java Virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * Machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
  1161
     *<p> The {@code URLStreamHandlerFactory} instance is used to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     *construct a stream protocol handler from a protocol name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * <p> If there is a security manager, this method first calls
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
  1165
     * the security manager's {@code checkSetFactory} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * to ensure the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * @param      fac   the desired factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * @exception  Error  if the application has already set a factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * @exception  SecurityException  if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
  1172
     *             {@code checkSetFactory} method doesn't allow
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     *             the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * @see        java.net.URL#URL(java.lang.String, java.lang.String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     *             int, java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     * @see        java.net.URLStreamHandlerFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * @see        SecurityManager#checkSetFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
    public static void setURLStreamHandlerFactory(URLStreamHandlerFactory fac) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        synchronized (streamHandlerLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            if (factory != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
                throw new Error("factory already defined");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
                security.checkSetFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
            handlers.clear();
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1189
25398
f7bdeeb2ca57 8049220: URL.factory data race
plevart
parents: 24865
diff changeset
  1190
            // safe publication of URLStreamHandlerFactory with volatile write
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            factory = fac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
29097
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1195
    private static final URLStreamHandlerFactory defaultFactory = new DefaultFactory();
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1196
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1197
    private static class DefaultFactory implements URLStreamHandlerFactory {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1198
        private static String PREFIX = "sun.net.www.protocol";
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1199
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1200
        public URLStreamHandler createURLStreamHandler(String protocol) {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1201
            String name = PREFIX + "." + protocol + ".Handler";
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1202
            try {
37782
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 37781
diff changeset
  1203
                @SuppressWarnings("deprecation")
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 37781
diff changeset
  1204
                Object o = Class.forName(name).newInstance();
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 37781
diff changeset
  1205
                return (URLStreamHandler)o;
29097
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1206
            } catch (ClassNotFoundException x) {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1207
                // ignore
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1208
            } catch (Exception e) {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1209
                // For compatibility, all Exceptions are ignored.
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1210
                // any number of exceptions can get thrown here
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1211
            }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1212
            return null;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1213
        }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1214
    }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1215
29977
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
  1216
    private static URLStreamHandler lookupViaProperty(String protocol) {
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 36511
diff changeset
  1217
        String packagePrefixList =
37781
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
  1218
                GetPropertyAction.privilegedGetProperty(protocolPathProp);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35399
diff changeset
  1219
        if (packagePrefixList == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35399
diff changeset
  1220
            // not set
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35399
diff changeset
  1221
            return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35399
diff changeset
  1222
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35399
diff changeset
  1223
29977
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
  1224
        String[] packagePrefixes = packagePrefixList.split("\\|");
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
  1225
        URLStreamHandler handler = null;
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
  1226
        for (int i=0; handler == null && i<packagePrefixes.length; i++) {
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
  1227
            String packagePrefix = packagePrefixes[i].trim();
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
  1228
            try {
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
  1229
                String clsName = packagePrefix + "." + protocol + ".Handler";
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
  1230
                Class<?> cls = null;
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
  1231
                try {
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
  1232
                    cls = Class.forName(clsName);
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
  1233
                } catch (ClassNotFoundException e) {
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
  1234
                    ClassLoader cl = ClassLoader.getSystemClassLoader();
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
  1235
                    if (cl != null) {
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
  1236
                        cls = cl.loadClass(clsName);
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
  1237
                    }
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
  1238
                }
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
  1239
                if (cls != null) {
37782
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 37781
diff changeset
  1240
                    @SuppressWarnings("deprecation")
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 37781
diff changeset
  1241
                    Object tmp = cls.newInstance();
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 37781
diff changeset
  1242
                    handler = (URLStreamHandler)tmp;
29977
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
  1243
                }
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
  1244
            } catch (Exception e) {
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
  1245
                // any number of exceptions can get thrown here
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
  1246
            }
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
  1247
        }
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
  1248
        return handler;
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
  1249
    }
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
  1250
29097
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1251
    private static Iterator<URLStreamHandlerProvider> providers() {
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 29977
diff changeset
  1252
        return new Iterator<>() {
29097
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1253
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1254
            ClassLoader cl = ClassLoader.getSystemClassLoader();
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1255
            ServiceLoader<URLStreamHandlerProvider> sl =
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1256
                    ServiceLoader.load(URLStreamHandlerProvider.class, cl);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1257
            Iterator<URLStreamHandlerProvider> i = sl.iterator();
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1258
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1259
            URLStreamHandlerProvider next = null;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1260
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1261
            private boolean getNext() {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1262
                while (next == null) {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1263
                    try {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1264
                        if (!i.hasNext())
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1265
                            return false;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1266
                        next = i.next();
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1267
                    } catch (ServiceConfigurationError sce) {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1268
                        if (sce.getCause() instanceof SecurityException) {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1269
                            // Ignore security exceptions
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1270
                            continue;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1271
                        }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1272
                        throw sce;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1273
                    }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1274
                }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1275
                return true;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1276
            }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1277
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1278
            public boolean hasNext() {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1279
                return getNext();
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1280
            }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1281
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1282
            public URLStreamHandlerProvider next() {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1283
                if (!getNext())
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1284
                    throw new NoSuchElementException();
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1285
                URLStreamHandlerProvider n = next;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1286
                next = null;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1287
                return n;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1288
            }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1289
        };
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1290
    }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1291
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1292
    // Thread-local gate to prevent recursive provider lookups
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1293
    private static ThreadLocal<Object> gate = new ThreadLocal<>();
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1294
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1295
    private static URLStreamHandler lookupViaProviders(final String protocol) {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1296
        if (gate.get() != null)
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1297
            throw new Error("Circular loading of URL stream handler providers detected");
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1298
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1299
        gate.set(gate);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1300
        try {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1301
            return AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 29977
diff changeset
  1302
                new PrivilegedAction<>() {
29097
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1303
                    public URLStreamHandler run() {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1304
                        Iterator<URLStreamHandlerProvider> itr = providers();
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1305
                        while (itr.hasNext()) {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1306
                            URLStreamHandlerProvider f = itr.next();
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1307
                            URLStreamHandler h = f.createURLStreamHandler(protocol);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1308
                            if (h != null)
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1309
                                return h;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1310
                        }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1311
                        return null;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1312
                    }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1313
                });
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1314
        } finally {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1315
            gate.set(null);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1316
        }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1317
    }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1318
35393
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
  1319
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
  1320
    /**
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
  1321
     * Non-overrideable protocols: "jrt" and "file"
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
  1322
     *
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
  1323
     * Character-based comparison for performance reasons; also ensures
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
  1324
     * case-insensitive comparison in a locale-independent fashion.
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
  1325
     */
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
  1326
    static boolean isOverrideable(String protocol) {
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
  1327
        if (protocol.length() == 3) {
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
  1328
            if ((Character.toLowerCase(protocol.charAt(0)) == 'j') &&
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
  1329
                    (Character.toLowerCase(protocol.charAt(1)) == 'r') &&
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
  1330
                    (Character.toLowerCase(protocol.charAt(2)) == 't')) {
29097
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1331
                return false;
35393
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
  1332
            }
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
  1333
        } else if (protocol.length() == 4) {
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
  1334
            if ((Character.toLowerCase(protocol.charAt(0)) == 'f') &&
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
  1335
                    (Character.toLowerCase(protocol.charAt(1)) == 'i') &&
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
  1336
                    (Character.toLowerCase(protocol.charAt(2)) == 'l') &&
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
  1337
                    (Character.toLowerCase(protocol.charAt(3)) == 'e')) {
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
  1338
                return false;
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
  1339
            }
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 35381
diff changeset
  1340
        }
29097
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1341
        return true;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1342
    }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1343
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     * A table of protocol handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     */
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
  1347
    static Hashtable<String,URLStreamHandler> handlers = new Hashtable<>();
29097
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1348
    private static final Object streamHandlerLock = new Object();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
     * Returns the Stream Handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
     * @param protocol the protocol to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
    static URLStreamHandler getURLStreamHandler(String protocol) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
  1356
        URLStreamHandler handler = handlers.get(protocol);
29097
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1357
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1358
        if (handler != null) {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1359
            return handler;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1360
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
29097
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1362
        URLStreamHandlerFactory fac;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1363
        boolean checkedWithFactory = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35399
diff changeset
  1365
        if (isOverrideable(protocol) && jdk.internal.misc.VM.isBooted()) {
25398
f7bdeeb2ca57 8049220: URL.factory data race
plevart
parents: 24865
diff changeset
  1366
            // Use the factory (if any). Volatile read makes
f7bdeeb2ca57 8049220: URL.factory data race
plevart
parents: 24865
diff changeset
  1367
            // URLStreamHandlerFactory appear fully initialized to current thread.
29097
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1368
            fac = factory;
25398
f7bdeeb2ca57 8049220: URL.factory data race
plevart
parents: 24865
diff changeset
  1369
            if (fac != null) {
f7bdeeb2ca57 8049220: URL.factory data race
plevart
parents: 24865
diff changeset
  1370
                handler = fac.createURLStreamHandler(protocol);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
                checkedWithFactory = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
29098
fd65e2764b56 8073667: URL should not use service loader to lookup the jar protocol handler
chegar
parents: 29097
diff changeset
  1374
            if (handler == null && !protocol.equalsIgnoreCase("jar")) {
29097
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1375
                handler = lookupViaProviders(protocol);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1376
            }
29977
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
  1377
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
  1378
            if (handler == null) {
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
  1379
                handler = lookupViaProperty(protocol);
ad35f9986f75 8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents: 29098
diff changeset
  1380
            }
29097
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1381
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
29097
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1383
        synchronized (streamHandlerLock) {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1384
            if (handler == null) {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1385
                // Try the built-in protocol handler
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1386
                handler = defaultFactory.createURLStreamHandler(protocol);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1387
            } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                URLStreamHandler handler2 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
                // Check again with hashtable just in case another
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
                // thread created a handler since we last checked
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
  1392
                handler2 = handlers.get(protocol);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
                if (handler2 != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
                    return handler2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
                // Check with factory if another thread set a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
                // factory since our last check
25398
f7bdeeb2ca57 8049220: URL.factory data race
plevart
parents: 24865
diff changeset
  1400
                if (!checkedWithFactory && (fac = factory) != null) {
29097
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1401
                    handler2 =  fac.createURLStreamHandler(protocol);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                if (handler2 != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
                    // The handler from the factory must be given more
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
                    // importance. Discard the default handler that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
                    // this thread created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
                    handler = handler2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
                }
29097
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1410
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
29097
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1412
            // Insert this handler into the hashtable
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1413
            if (handler != null) {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 27565
diff changeset
  1414
                handlers.put(protocol, handler);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
        return handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
    /**
35280
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1422
     * @serialField    protocol String
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1423
     *
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1424
     * @serialField    host String
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1425
     *
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1426
     * @serialField    port int
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1427
     *
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1428
     * @serialField    authority String
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1429
     *
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1430
     * @serialField    file String
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1431
     *
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1432
     * @serialField    ref String
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1433
     *
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1434
     * @serialField    hashCode int
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1435
     *
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1436
     */
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1437
    private static final ObjectStreamField[] serialPersistentFields = {
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1438
        new ObjectStreamField("protocol", String.class),
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1439
        new ObjectStreamField("host", String.class),
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1440
        new ObjectStreamField("port", int.class),
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1441
        new ObjectStreamField("authority", String.class),
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1442
        new ObjectStreamField("file", String.class),
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1443
        new ObjectStreamField("ref", String.class),
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1444
        new ObjectStreamField("hashCode", int.class), };
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1445
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1446
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
     * WriteObject is called to save the state of the URL to an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
     * ObjectOutputStream. The handler is not saved since it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
     * specific to this system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
     * @serialData the default write object value. When read back in,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
     * the reader must ensure that calling getURLStreamHandler with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
     * the protocol variable returns a valid URLStreamHandler and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
     * throw an IOException if it does not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
    private synchronized void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
        s.defaultWriteObject(); // write the fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
     * readObject is called to restore the state of the URL from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
     * stream.  It reads the components of the URL and finds the local
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
     * stream handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
    private synchronized void readObject(java.io.ObjectInputStream s)
35280
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1468
            throws IOException, ClassNotFoundException {
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1469
        GetField gf = s.readFields();
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1470
        String protocol = (String)gf.get("protocol", null);
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1471
        if (getURLStreamHandler(protocol) == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
            throw new IOException("unknown protocol: " + protocol);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
        }
35280
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1474
        String host = (String)gf.get("host", null);
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1475
        int port = gf.get("port", -1);
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1476
        String authority = (String)gf.get("authority", null);
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1477
        String file = (String)gf.get("file", null);
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1478
        String ref = (String)gf.get("ref", null);
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1479
        int hashCode = gf.get("hashCode", -1);
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1480
        if (authority == null
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1481
                && ((host != null && host.length() > 0) || port != -1)) {
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1482
            if (host == null)
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1483
                host = "";
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1484
            authority = (port == -1) ? host : host + ":" + port;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1485
        }
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1486
        tempState = new UrlDeserializedState(protocol, host, port, authority,
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1487
               file, ref, hashCode);
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1488
    }
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1489
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1490
    /**
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1491
     * Replaces the de-serialized object with an URL object.
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1492
     *
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1493
     * @return a newly created object from deserialized data
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1494
     *
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1495
     * @throws ObjectStreamException if a new object replacing this
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1496
     * object could not be created
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1497
     */
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1498
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1499
   private Object readResolve() throws ObjectStreamException {
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1500
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1501
        URLStreamHandler handler = null;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1502
        // already been checked in readObject
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1503
        handler = getURLStreamHandler(tempState.getProtocol());
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1504
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1505
        URL replacementURL = null;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1506
        if (isBuiltinStreamHandler(handler.getClass().getName())) {
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1507
            replacementURL = fabricateNewURL();
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1508
        } else {
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1509
            replacementURL = setDeserializedFields(handler);
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1510
        }
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1511
        return replacementURL;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1512
    }
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1513
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1514
    private URL setDeserializedFields(URLStreamHandler handler) {
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1515
        URL replacementURL;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1516
        String userInfo = null;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1517
        String protocol = tempState.getProtocol();
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1518
        String host = tempState.getHost();
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1519
        int port = tempState.getPort();
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1520
        String authority = tempState.getAuthority();
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1521
        String file = tempState.getFile();
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1522
        String ref = tempState.getRef();
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1523
        int hashCode = tempState.getHashCode();
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1524
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
        // Construct authority part
35280
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1527
        if (authority == null
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1528
            && ((host != null && host.length() > 0) || port != -1)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
            if (host == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
                host = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
            authority = (port == -1) ? host : host + ":" + port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
            // Handle hosts with userInfo in them
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
            int at = host.lastIndexOf('@');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
            if (at != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
                userInfo = host.substring(0, at);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
                host = host.substring(at+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
        } else if (authority != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
            // Construct user info part
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
            int ind = authority.indexOf('@');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
            if (ind != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
                userInfo = authority.substring(0, ind);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
        // Construct path and query part
35280
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1547
        String path = null;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1548
        String query = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
        if (file != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
            // Fix: only do this if hierarchical?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
            int q = file.lastIndexOf('?');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
            if (q != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
                query = file.substring(q+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
                path = file.substring(0, q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
                path = file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
        }
35280
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1558
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1559
        // Set the object fields.
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1560
        this.protocol = protocol;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1561
        this.host = host;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1562
        this.port = port;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1563
        this.file = file;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1564
        this.authority = authority;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1565
        this.ref = ref;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1566
        this.hashCode = hashCode;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1567
        this.handler = handler;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1568
        this.query = query;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1569
        this.path = path;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1570
        this.userInfo = userInfo;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1571
        replacementURL = this;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1572
        return replacementURL;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1573
    }
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1574
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1575
    private URL fabricateNewURL()
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1576
                throws InvalidObjectException {
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1577
        // create URL string from deserialized object
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1578
        URL replacementURL = null;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1579
        String urlString = tempState.reconstituteUrlString();
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1580
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1581
        try {
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1582
            replacementURL = new URL(urlString);
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1583
        } catch (MalformedURLException mEx) {
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1584
            resetState();
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1585
            InvalidObjectException invoEx = new InvalidObjectException(
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1586
                    "Malformed URL:  " + urlString);
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1587
            invoEx.initCause(mEx);
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1588
            throw invoEx;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1589
        }
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1590
        replacementURL.setSerializedHashCode(tempState.getHashCode());
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1591
        resetState();
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1592
        return replacementURL;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1593
    }
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1594
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1595
    private boolean isBuiltinStreamHandler(String handlerClassName) {
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1596
        return (handlerClassName.startsWith(BUILTIN_HANDLERS_PREFIX));
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1597
    }
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1598
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1599
    private void resetState() {
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1600
        this.protocol = null;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1601
        this.host = null;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1602
        this.port = -1;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1603
        this.file = null;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1604
        this.authority = null;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1605
        this.ref = null;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1606
        this.hashCode = -1;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1607
        this.handler = null;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1608
        this.query = null;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1609
        this.path = null;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1610
        this.userInfo = null;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1611
        this.tempState = null;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1612
    }
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1613
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1614
    private void setSerializedHashCode(int hc) {
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1615
        this.hashCode = hc;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
35280
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1619
final class UrlDeserializedState {
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1620
    private final String protocol;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1621
    private final String host;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1622
    private final int port;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1623
    private final String authority;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1624
    private final String file;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1625
    private final String ref;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1626
    private final int hashCode;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1627
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1628
    public UrlDeserializedState(String protocol,
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1629
                                String host, int port,
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1630
                                String authority, String file,
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1631
                                String ref, int hashCode) {
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1632
        this.protocol = protocol;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1633
        this.host = host;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1634
        this.port = port;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1635
        this.authority = authority;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1636
        this.file = file;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1637
        this.ref = ref;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1638
        this.hashCode = hashCode;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1639
    }
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1640
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1641
    String getProtocol() {
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1642
        return protocol;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1643
    }
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1644
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1645
    String getHost() {
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1646
        return host;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1647
    }
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1648
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1649
    String getAuthority () {
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1650
        return authority;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1651
    }
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1652
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1653
    int getPort() {
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1654
        return port;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1655
    }
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1656
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1657
    String getFile () {
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1658
        return file;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1659
    }
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1660
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1661
    String getRef () {
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1662
        return ref;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1663
    }
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1664
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1665
    int getHashCode () {
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1666
        return hashCode;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1667
    }
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1668
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1669
    String reconstituteUrlString() {
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1670
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1671
        // pre-compute length of StringBuffer
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1672
        int len = protocol.length() + 1;
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1673
        if (authority != null && authority.length() > 0)
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1674
            len += 2 + authority.length();
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1675
        if (file != null) {
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1676
            len += file.length();
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1677
        }
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1678
        if (ref != null)
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1679
            len += 1 + ref.length();
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1680
        StringBuilder result = new StringBuilder(len);
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1681
        result.append(protocol);
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1682
        result.append(":");
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1683
        if (authority != null && authority.length() > 0) {
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1684
            result.append("//");
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1685
            result.append(authority);
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1686
        }
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1687
        if (file != null) {
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1688
            result.append(file);
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1689
        }
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1690
        if (ref != null) {
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1691
            result.append("#");
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1692
            result.append(ref);
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1693
        }
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1694
        return result.toString();
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1695
    }
c607d894f91e 8059054: Better URL processing
msheppar
parents: 34882
diff changeset
  1696
}