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