src/java.base/share/classes/java/net/URI.java
author redestad
Thu, 13 Dec 2018 15:31:05 +0100
changeset 53018 8bf9268df0e2
parent 52944 d75110673dc9
child 54206 003cc64366da
permissions -rw-r--r--
8215281: Use String.isEmpty() when applicable in java.base Reviewed-by: dfuchs, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 47216
diff changeset
     2
 * Copyright (c) 2000, 2018, 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: 715
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: 715
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: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
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.InvalidObjectException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.nio.CharBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.nio.charset.CharsetDecoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.nio.charset.CoderResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.nio.charset.CodingErrorAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.nio.charset.CharacterCodingException;
52944
d75110673dc9 8215008: Clear confusion between URL/URI paths and file system paths
dfuchs
parents: 52700
diff changeset
    40
import java.nio.file.Path;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.text.Normalizer;
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 47216
diff changeset
    42
import jdk.internal.access.JavaNetUriAccess;
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 47216
diff changeset
    43
import jdk.internal.access.SharedSecrets;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import sun.nio.cs.ThreadLocalCoders;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.lang.Character;             // for javadoc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.lang.NullPointerException;  // for javadoc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * Represents a Uniform Resource Identifier (URI) reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <p> Aside from some minor deviations noted below, an instance of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * class represents a URI reference as defined by
708
a780486c413c 6630348: Invalid html tags (extra double quote)
chegar
parents: 2
diff changeset
    55
 * <a href="http://www.ietf.org/rfc/rfc2396.txt"><i>RFC&nbsp;2396: Uniform
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Resource Identifiers (URI): Generic Syntax</i></a>, amended by <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * href="http://www.ietf.org/rfc/rfc2732.txt"><i>RFC&nbsp;2732: Format for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * Literal IPv6 Addresses in URLs</i></a>. The Literal IPv6 address format
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * also supports scope_ids. The syntax and usage of scope_ids is described
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <a href="Inet6Address.html#scoped">here</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * This class provides constructors for creating URI instances from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * their components or by parsing their string forms, methods for accessing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * various components of an instance, and methods for normalizing, resolving,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * and relativizing URI instances.  Instances of this class are immutable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
18800
e7fa560afcfb 8020318: Fix doclint issues in java.net
juh
parents: 18156
diff changeset
    67
 * <h3> URI syntax and components </h3>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * At the highest level a URI reference (hereinafter simply "URI") in string
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * form has the syntax
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <blockquote>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
    73
 * [<i>scheme</i><b>{@code :}</b>]<i>scheme-specific-part</i>[<b>{@code #}</b><i>fragment</i>]
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * where square brackets [...] delineate optional components and the characters
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
    77
 * <b>{@code :}</b> and <b>{@code #}</b> stand for themselves.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <p> An <i>absolute</i> URI specifies a scheme; a URI that is not absolute is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * said to be <i>relative</i>.  URIs are also classified according to whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * they are <i>opaque</i> or <i>hierarchical</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <p> An <i>opaque</i> URI is an absolute URI whose scheme-specific part does
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
    84
 * not begin with a slash character ({@code '/'}).  Opaque URIs are not
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * subject to further parsing.  Some examples of opaque URIs are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 44844
diff changeset
    87
 * <blockquote><ul style="list-style-type:none">
52700
b206bdfb9fe2 8213911: Use example.com in java.net and other examples
darcy
parents: 52499
diff changeset
    88
 * <li>{@code mailto:java-net@www.example.com}</li>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 44844
diff changeset
    89
 * <li>{@code news:comp.lang.java}</li>
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 44844
diff changeset
    90
 * <li>{@code urn:isbn:096139210x}</li>
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 44844
diff changeset
    91
 * </ul></blockquote>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * <p> A <i>hierarchical</i> URI is either an absolute URI whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * scheme-specific part begins with a slash character, or a relative URI, that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * is, a URI that does not specify a scheme.  Some examples of hierarchical
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * URIs are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * <blockquote>
38866
de92f990074f 8158881: Doc typo in src/../java/net/URI.java
mli
parents: 35393
diff changeset
    99
 * {@code http://example.com/languages/java/}<br>
de92f990074f 8158881: Doc typo in src/../java/net/URI.java
mli
parents: 35393
diff changeset
   100
 * {@code sample/a/index.html#28}<br>
de92f990074f 8158881: Doc typo in src/../java/net/URI.java
mli
parents: 35393
diff changeset
   101
 * {@code ../../demo/b/index.html}<br>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   102
 * {@code file:///~/calendar}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * <p> A hierarchical URI is subject to further parsing according to the syntax
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * <blockquote>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   108
 * [<i>scheme</i><b>{@code :}</b>][<b>{@code //}</b><i>authority</i>][<i>path</i>][<b>{@code ?}</b><i>query</i>][<b>{@code #}</b><i>fragment</i>]
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   111
 * where the characters <b>{@code :}</b>, <b>{@code /}</b>,
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   112
 * <b>{@code ?}</b>, and <b>{@code #}</b> stand for themselves.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * scheme-specific part of a hierarchical URI consists of the characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * between the scheme and fragment components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * <p> The authority component of a hierarchical URI is, if specified, either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * <i>server-based</i> or <i>registry-based</i>.  A server-based authority
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * parses according to the familiar syntax
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * <blockquote>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   121
 * [<i>user-info</i><b>{@code @}</b>]<i>host</i>[<b>{@code :}</b><i>port</i>]
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   124
 * where the characters <b>{@code @}</b> and <b>{@code :}</b> stand for
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * themselves.  Nearly all URI schemes currently in use are server-based.  An
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * authority component that does not parse in this way is considered to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * registry-based.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * <p> The path component of a hierarchical URI is itself said to be absolute
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   130
 * if it begins with a slash character ({@code '/'}); otherwise it is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * relative.  The path of a hierarchical URI that is either absolute or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * specifies an authority is always absolute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * <p> All told, then, a URI instance has the following nine components:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 *
46152
51d10b05c78e 8186156: Fix a11y and HTML issues in java.net and javax.net packages
jjg
parents: 45437
diff changeset
   136
 * <table class="striped" style="margin-left:2em">
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 44844
diff changeset
   137
 * <caption style="display:none">Describes the components of a URI:scheme,scheme-specific-part,authority,user-info,host,port,path,query,fragment</caption>
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 44844
diff changeset
   138
 * <thead>
46152
51d10b05c78e 8186156: Fix a11y and HTML issues in java.net and javax.net packages
jjg
parents: 45437
diff changeset
   139
 * <tr><th scope="col">Component</th><th scope="col">Type</th></tr>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 44844
diff changeset
   140
 * </thead>
46152
51d10b05c78e 8186156: Fix a11y and HTML issues in java.net and javax.net packages
jjg
parents: 45437
diff changeset
   141
 * <tbody style="text-align:left">
51d10b05c78e 8186156: Fix a11y and HTML issues in java.net and javax.net packages
jjg
parents: 45437
diff changeset
   142
 * <tr><th scope="row">scheme</th><td>{@code String}</td></tr>
51d10b05c78e 8186156: Fix a11y and HTML issues in java.net and javax.net packages
jjg
parents: 45437
diff changeset
   143
 * <tr><th scope="row">scheme-specific-part</th><td>{@code String}</td></tr>
51d10b05c78e 8186156: Fix a11y and HTML issues in java.net and javax.net packages
jjg
parents: 45437
diff changeset
   144
 * <tr><th scope="row">authority</th><td>{@code String}</td></tr>
51d10b05c78e 8186156: Fix a11y and HTML issues in java.net and javax.net packages
jjg
parents: 45437
diff changeset
   145
 * <tr><th scope="row">user-info</th><td>{@code String}</td></tr>
51d10b05c78e 8186156: Fix a11y and HTML issues in java.net and javax.net packages
jjg
parents: 45437
diff changeset
   146
 * <tr><th scope="row">host</th><td>{@code String}</td></tr>
51d10b05c78e 8186156: Fix a11y and HTML issues in java.net and javax.net packages
jjg
parents: 45437
diff changeset
   147
 * <tr><th scope="row">port</th><td>{@code int}</td></tr>
51d10b05c78e 8186156: Fix a11y and HTML issues in java.net and javax.net packages
jjg
parents: 45437
diff changeset
   148
 * <tr><th scope="row">path</th><td>{@code String}</td></tr>
51d10b05c78e 8186156: Fix a11y and HTML issues in java.net and javax.net packages
jjg
parents: 45437
diff changeset
   149
 * <tr><th scope="row">query</th><td>{@code String}</td></tr>
51d10b05c78e 8186156: Fix a11y and HTML issues in java.net and javax.net packages
jjg
parents: 45437
diff changeset
   150
 * <tr><th scope="row">fragment</th><td>{@code String}</td></tr>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 44844
diff changeset
   151
 * </tbody>
46152
51d10b05c78e 8186156: Fix a11y and HTML issues in java.net and javax.net packages
jjg
parents: 45437
diff changeset
   152
 * </table>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * In a given instance any particular component is either <i>undefined</i> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * <i>defined</i> with a distinct value.  Undefined string components are
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   156
 * represented by {@code null}, while undefined integer components are
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   157
 * represented by {@code -1}.  A string component may be defined to have the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * empty string as its value; this is not equivalent to that component being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * <p> Whether a particular component is or is not defined in an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * depends upon the type of the URI being represented.  An absolute URI has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * scheme component.  An opaque URI has a scheme, a scheme-specific part, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * possibly a fragment, but has no other components.  A hierarchical URI always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * has a path (though it may be empty) and a scheme-specific-part (which at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * least contains the path), and may have any of the other components.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * authority component is present and is server-based then the host component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * will be defined and the user-information and port components may be defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * <h4> Operations on URI instances </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 * The key operations supported by this class are those of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 * <i>normalization</i>, <i>resolution</i>, and <i>relativization</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   176
 * <p> <i>Normalization</i> is the process of removing unnecessary {@code "."}
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   177
 * and {@code ".."} segments from the path component of a hierarchical URI.
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   178
 * Each {@code "."} segment is simply removed.  A {@code ".."} segment is
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   179
 * removed only if it is preceded by a non-{@code ".."} segment.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 * Normalization has no effect upon opaque URIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 * <p> <i>Resolution</i> is the process of resolving one URI against another,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 * <i>base</i> URI.  The resulting URI is constructed from components of both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 * URIs in the manner specified by RFC&nbsp;2396, taking components from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 * base URI for those not specified in the original.  For hierarchical URIs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 * the path of the original is resolved against the path of the base and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 * normalized.  The result, for example, of resolving
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 * <blockquote>
38866
de92f990074f 8158881: Doc typo in src/../java/net/URI.java
mli
parents: 35393
diff changeset
   190
 * {@code sample/a/index.html#28}
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   191
 * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   192
 * &nbsp;&nbsp;&nbsp;&nbsp;(1)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
 *
38866
de92f990074f 8158881: Doc typo in src/../java/net/URI.java
mli
parents: 35393
diff changeset
   195
 * against the base URI {@code http://example.com/languages/java/} is the result
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 * URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 * <blockquote>
38866
de92f990074f 8158881: Doc typo in src/../java/net/URI.java
mli
parents: 35393
diff changeset
   199
 * {@code http://example.com/languages/java/sample/a/index.html#28}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 * Resolving the relative URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 * <blockquote>
38866
de92f990074f 8158881: Doc typo in src/../java/net/URI.java
mli
parents: 35393
diff changeset
   205
 * {@code ../../demo/b/index.html}&nbsp;&nbsp;&nbsp;&nbsp;(2)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 * against this result yields, in turn,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
 * <blockquote>
38866
de92f990074f 8158881: Doc typo in src/../java/net/URI.java
mli
parents: 35393
diff changeset
   211
 * {@code http://example.com/languages/java/demo/b/index.html}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
 * Resolution of both absolute and relative URIs, and of both absolute and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 * relative paths in the case of hierarchical URIs, is supported.  Resolving
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   216
 * the URI {@code file:///~calendar} against any other URI simply yields the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 * original URI, since it is absolute.  Resolving the relative URI (2) above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
 * against the relative base URI (1) yields the normalized, but still relative,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
 * URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
 * <blockquote>
38866
de92f990074f 8158881: Doc typo in src/../java/net/URI.java
mli
parents: 35393
diff changeset
   222
 * {@code demo/b/index.html}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
 * <p> <i>Relativization</i>, finally, is the inverse of resolution: For any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
 * two normalized URIs <i>u</i> and&nbsp;<i>v</i>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
 * <blockquote>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   229
 *   <i>u</i>{@code .relativize(}<i>u</i>{@code .resolve(}<i>v</i>{@code )).equals(}<i>v</i>{@code )}&nbsp;&nbsp;and<br>
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   230
 *   <i>u</i>{@code .resolve(}<i>u</i>{@code .relativize(}<i>v</i>{@code )).equals(}<i>v</i>{@code )}&nbsp;&nbsp;.<br>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
 * This operation is often useful when constructing a document containing URIs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
 * that must be made relative to the base URI of the document wherever
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
 * possible.  For example, relativizing the URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
 * <blockquote>
38866
de92f990074f 8158881: Doc typo in src/../java/net/URI.java
mli
parents: 35393
diff changeset
   238
 * {@code http://example.com/languages/java/sample/a/index.html#28}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
 * against the base URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
 * <blockquote>
38866
de92f990074f 8158881: Doc typo in src/../java/net/URI.java
mli
parents: 35393
diff changeset
   244
 * {@code http://example.com/languages/java/}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
 *
38866
de92f990074f 8158881: Doc typo in src/../java/net/URI.java
mli
parents: 35393
diff changeset
   247
 * yields the relative URI {@code sample/a/index.html#28}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
 * <h4> Character categories </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
 * RFC&nbsp;2396 specifies precisely which characters are permitted in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
 * various components of a URI reference.  The following categories, most of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
 * which are taken from that specification, are used below to describe these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
 * constraints:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
 *
46152
51d10b05c78e 8186156: Fix a11y and HTML issues in java.net and javax.net packages
jjg
parents: 45437
diff changeset
   257
 * <table class="striped" style="margin-left:2em">
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 44844
diff changeset
   258
 * <caption style="display:none">Describes categories alpha,digit,alphanum,unreserved,punct,reserved,escaped,and other</caption>
46152
51d10b05c78e 8186156: Fix a11y and HTML issues in java.net and javax.net packages
jjg
parents: 45437
diff changeset
   259
 *   <thead>
51d10b05c78e 8186156: Fix a11y and HTML issues in java.net and javax.net packages
jjg
parents: 45437
diff changeset
   260
 *   <tr><th scope="col">Category</th><th scope="col">Description</th></tr>
51d10b05c78e 8186156: Fix a11y and HTML issues in java.net and javax.net packages
jjg
parents: 45437
diff changeset
   261
 *   </thead>
51d10b05c78e 8186156: Fix a11y and HTML issues in java.net and javax.net packages
jjg
parents: 45437
diff changeset
   262
 *   <tbody style="text-align:left">
51d10b05c78e 8186156: Fix a11y and HTML issues in java.net and javax.net packages
jjg
parents: 45437
diff changeset
   263
 *   <tr><th scope="row" style="vertical-align:top">alpha</th>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
 *       <td>The US-ASCII alphabetic characters,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   265
 *        {@code 'A'}&nbsp;through&nbsp;{@code 'Z'}
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   266
 *        and {@code 'a'}&nbsp;through&nbsp;{@code 'z'}</td></tr>
46152
51d10b05c78e 8186156: Fix a11y and HTML issues in java.net and javax.net packages
jjg
parents: 45437
diff changeset
   267
 *   <tr><th scope="row" style="vertical-align:top">digit</th>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
 *       <td>The US-ASCII decimal digit characters,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   269
 *       {@code '0'}&nbsp;through&nbsp;{@code '9'}</td></tr>
46152
51d10b05c78e 8186156: Fix a11y and HTML issues in java.net and javax.net packages
jjg
parents: 45437
diff changeset
   270
 *   <tr><th scope="row" style="vertical-align:top">alphanum</th>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
 *       <td>All <i>alpha</i> and <i>digit</i> characters</td></tr>
46152
51d10b05c78e 8186156: Fix a11y and HTML issues in java.net and javax.net packages
jjg
parents: 45437
diff changeset
   272
 *   <tr><th scope="row" style="vertical-align:top">unreserved</th>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
 *       <td>All <i>alphanum</i> characters together with those in the string
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   274
 *        {@code "_-!.~'()*"}</td></tr>
46152
51d10b05c78e 8186156: Fix a11y and HTML issues in java.net and javax.net packages
jjg
parents: 45437
diff changeset
   275
 *   <tr><th scope="row" style="vertical-align:top">punct</th>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   276
 *       <td>The characters in the string {@code ",;:$&+="}</td></tr>
46152
51d10b05c78e 8186156: Fix a11y and HTML issues in java.net and javax.net packages
jjg
parents: 45437
diff changeset
   277
 *   <tr><th scope="row" style="vertical-align:top">reserved</th>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
 *       <td>All <i>punct</i> characters together with those in the string
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   279
 *        {@code "?/[]@"}</td></tr>
46152
51d10b05c78e 8186156: Fix a11y and HTML issues in java.net and javax.net packages
jjg
parents: 45437
diff changeset
   280
 *   <tr><th scope="row" style="vertical-align:top">escaped</th>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
 *       <td>Escaped octets, that is, triplets consisting of the percent
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   282
 *           character ({@code '%'}) followed by two hexadecimal digits
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   283
 *           ({@code '0'}-{@code '9'}, {@code 'A'}-{@code 'F'}, and
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   284
 *           {@code 'a'}-{@code 'f'})</td></tr>
46152
51d10b05c78e 8186156: Fix a11y and HTML issues in java.net and javax.net packages
jjg
parents: 45437
diff changeset
   285
 *   <tr><th scope="row" style="vertical-align:top">other</th>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
 *       <td>The Unicode characters that are not in the US-ASCII character set,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
 *           are not control characters (according to the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
 *           java.lang.Character#isISOControl(char) Character.isISOControl}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
 *           method), and are not space characters (according to the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
 *           java.lang.Character#isSpaceChar(char) Character.isSpaceChar}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
 *           method)&nbsp;&nbsp;<i>(<b>Deviation from RFC 2396</b>, which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
 *           limited to US-ASCII)</i></td></tr>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 44844
diff changeset
   293
 * </tbody>
46152
51d10b05c78e 8186156: Fix a11y and HTML issues in java.net and javax.net packages
jjg
parents: 45437
diff changeset
   294
 * </table>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
 *
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 41555
diff changeset
   296
 * <p><a id="legal-chars"></a> The set of all legal URI characters consists of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
 * the <i>unreserved</i>, <i>reserved</i>, <i>escaped</i>, and <i>other</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
 * characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
 * <h4> Escaped octets, quotation, encoding, and decoding </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
 * RFC 2396 allows escaped octets to appear in the user-info, path, query, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
 * fragment components.  Escaping serves two purposes in URIs:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
 *   <li><p> To <i>encode</i> non-US-ASCII characters when a URI is required to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
 *   conform strictly to RFC&nbsp;2396 by not containing any <i>other</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
 *   characters.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
 *   <li><p> To <i>quote</i> characters that are otherwise illegal in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
 *   component.  The user-info, path, query, and fragment components differ
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
 *   slightly in terms of which characters are considered legal and illegal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
 *   </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
 * These purposes are served in this class by three related operations:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
 *
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 41555
diff changeset
   323
 *   <li><p><a id="encode"></a> A character is <i>encoded</i> by replacing it
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
 *   with the sequence of escaped octets that represent that character in the
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   325
 *   UTF-8 character set.  The Euro currency symbol ({@code '\u005Cu20AC'}),
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   326
 *   for example, is encoded as {@code "%E2%82%AC"}.  <i>(<b>Deviation from
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
 *   RFC&nbsp;2396</b>, which does not specify any particular character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
 *   set.)</i> </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
 *
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 41555
diff changeset
   330
 *   <li><p><a id="quote"></a> An illegal character is <i>quoted</i> simply by
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
 *   encoding it.  The space character, for example, is quoted by replacing it
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   332
 *   with {@code "%20"}.  UTF-8 contains US-ASCII, hence for US-ASCII
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
 *   characters this transformation has exactly the effect required by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
 *   RFC&nbsp;2396. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
 *
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 41555
diff changeset
   336
 *   <li><p><a id="decode"></a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
 *   A sequence of escaped octets is <i>decoded</i> by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
 *   replacing it with the sequence of characters that it represents in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
 *   UTF-8 character set.  UTF-8 contains US-ASCII, hence decoding has the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
 *   effect of de-quoting any quoted US-ASCII characters as well as that of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
 *   decoding any encoded non-US-ASCII characters.  If a <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
 *   href="../nio/charset/CharsetDecoder.html#ce">decoding error</a> occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
 *   when decoding the escaped octets then the erroneous octets are replaced by
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   344
 *   {@code '\u005CuFFFD'}, the Unicode replacement character.  </p></li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
 * These operations are exposed in the constructors and methods of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
 * as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
 *
18800
e7fa560afcfb 8020318: Fix doclint issues in java.net
juh
parents: 18156
diff changeset
   353
 *   <li><p> The {@linkplain #URI(java.lang.String) single-argument
e7fa560afcfb 8020318: Fix doclint issues in java.net
juh
parents: 18156
diff changeset
   354
 *   constructor} requires any illegal characters in its argument to be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
 *   quoted and preserves any escaped octets and <i>other</i> characters that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
 *   are present.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
 *
18800
e7fa560afcfb 8020318: Fix doclint issues in java.net
juh
parents: 18156
diff changeset
   358
 *   <li><p> The {@linkplain
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
 *   #URI(java.lang.String,java.lang.String,java.lang.String,int,java.lang.String,java.lang.String,java.lang.String)
18800
e7fa560afcfb 8020318: Fix doclint issues in java.net
juh
parents: 18156
diff changeset
   360
 *   multi-argument constructors} quote illegal characters as
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
 *   required by the components in which they appear.  The percent character
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   362
 *   ({@code '%'}) is always quoted by these constructors.  Any <i>other</i>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
 *   characters are preserved.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
 *   <li><p> The {@link #getRawUserInfo() getRawUserInfo}, {@link #getRawPath()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
 *   getRawPath}, {@link #getRawQuery() getRawQuery}, {@link #getRawFragment()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
 *   getRawFragment}, {@link #getRawAuthority() getRawAuthority}, and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
 *   #getRawSchemeSpecificPart() getRawSchemeSpecificPart} methods return the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
 *   values of their corresponding components in raw form, without interpreting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
 *   any escaped octets.  The strings returned by these methods may contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
 *   both escaped octets and <i>other</i> characters, and will not contain any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
 *   illegal characters.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
 *   <li><p> The {@link #getUserInfo() getUserInfo}, {@link #getPath()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
 *   getPath}, {@link #getQuery() getQuery}, {@link #getFragment()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
 *   getFragment}, {@link #getAuthority() getAuthority}, and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
 *   #getSchemeSpecificPart() getSchemeSpecificPart} methods decode any escaped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
 *   octets in their corresponding components.  The strings returned by these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
 *   methods may contain both <i>other</i> characters and illegal characters,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
 *   and will not contain any escaped octets.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
 *   <li><p> The {@link #toString() toString} method returns a URI string with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
 *   all necessary quotation but which may contain <i>other</i> characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
 *   </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
 *   <li><p> The {@link #toASCIIString() toASCIIString} method returns a fully
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
 *   quoted and encoded URI string that does not contain any <i>other</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
 *   characters.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
 * <h4> Identities </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
 * For any URI <i>u</i>, it is always the case that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
 * <blockquote>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   398
 * {@code new URI(}<i>u</i>{@code .toString()).equals(}<i>u</i>{@code )}&nbsp;.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
 * For any URI <i>u</i> that does not contain redundant syntax such as two
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   402
 * slashes before an empty authority (as in {@code file:///tmp/}&nbsp;) or a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
 * colon following a host name but no port (as in
52700
b206bdfb9fe2 8213911: Use example.com in java.net and other examples
darcy
parents: 52499
diff changeset
   404
 * {@code http://www.example.com:}&nbsp;), and that does not encode characters
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
 * except those that must be quoted, the following identities also hold:
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 20455
diff changeset
   406
 * <pre>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   407
 *     new URI(<i>u</i>.getScheme(),
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   408
 *             <i>u</i>.getSchemeSpecificPart(),
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   409
 *             <i>u</i>.getFragment())
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   410
 *     .equals(<i>u</i>)</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
 * in all cases,
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 20455
diff changeset
   412
 * <pre>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   413
 *     new URI(<i>u</i>.getScheme(),
26875
c6c5d6a2765e 8059450: Not quite correct code sample in javadoc
igerasim
parents: 26720
diff changeset
   414
 *             <i>u</i>.getAuthority(),
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   415
 *             <i>u</i>.getPath(), <i>u</i>.getQuery(),
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   416
 *             <i>u</i>.getFragment())
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   417
 *     .equals(<i>u</i>)</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
 * if <i>u</i> is hierarchical, and
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 20455
diff changeset
   419
 * <pre>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   420
 *     new URI(<i>u</i>.getScheme(),
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   421
 *             <i>u</i>.getUserInfo(), <i>u</i>.getHost(), <i>u</i>.getPort(),
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   422
 *             <i>u</i>.getPath(), <i>u</i>.getQuery(),
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   423
 *             <i>u</i>.getFragment())
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   424
 *     .equals(<i>u</i>)</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
 * if <i>u</i> is hierarchical and has either no authority or a server-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
 * authority.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
 * <h4> URIs, URLs, and URNs </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
 * A URI is a uniform resource <i>identifier</i> while a URL is a uniform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
 * resource <i>locator</i>.  Hence every URL is a URI, abstractly speaking, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
 * not every URI is a URL.  This is because there is another subcategory of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
 * URIs, uniform resource <i>names</i> (URNs), which name resources but do not
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   435
 * specify how to locate them.  The {@code mailto}, {@code news}, and
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   436
 * {@code isbn} URIs shown above are examples of URNs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
 * <p> The conceptual distinction between URIs and URLs is reflected in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
 * differences between this class and the {@link URL} class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
 * <p> An instance of this class represents a URI reference in the syntactic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
 * sense defined by RFC&nbsp;2396.  A URI may be either absolute or relative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
 * A URI string is parsed according to the generic syntax without regard to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
 * scheme, if any, that it specifies.  No lookup of the host, if any, is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
 * performed, and no scheme-dependent stream handler is constructed.  Equality,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
 * hashing, and comparison are defined strictly in terms of the character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
 * content of the instance.  In other words, a URI instance is little more than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
 * a structured string that supports the syntactic, scheme-independent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
 * operations of comparison, normalization, resolution, and relativization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
 * <p> An instance of the {@link URL} class, by contrast, represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
 * syntactic components of a URL together with some of the information required
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
 * to access the resource that it describes.  A URL must be absolute, that is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
 * it must always specify a scheme.  A URL string is parsed according to its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
 * scheme.  A stream handler is always established for a URL, and in fact it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
 * impossible to create a URL instance for a scheme for which no handler is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
 * available.  Equality and hashing depend upon both the scheme and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
 * Internet address of the host, if any; comparison is not defined.  In other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
 * words, a URL is a structured string that supports the syntactic operation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
 * resolution as well as the network I/O operations of looking up the host and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
 * opening a connection to the specified resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
 *
52944
d75110673dc9 8215008: Clear confusion between URL/URI paths and file system paths
dfuchs
parents: 52700
diff changeset
   463
 * @apiNote
d75110673dc9 8215008: Clear confusion between URL/URI paths and file system paths
dfuchs
parents: 52700
diff changeset
   464
 *
d75110673dc9 8215008: Clear confusion between URL/URI paths and file system paths
dfuchs
parents: 52700
diff changeset
   465
 * 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
   466
 * 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
   467
 * 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
   468
 * 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
   469
 * 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
   470
 * can be used to create a {@link URI} from a file path.
d75110673dc9 8215008: Clear confusion between URL/URI paths and file system paths
dfuchs
parents: 52700
diff changeset
   471
 * Applications should never try to {@linkplain
d75110673dc9 8215008: Clear confusion between URL/URI paths and file system paths
dfuchs
parents: 52700
diff changeset
   472
 * #URI(String, String, String, int, String, String, String)
d75110673dc9 8215008: Clear confusion between URL/URI paths and file system paths
dfuchs
parents: 52700
diff changeset
   473
 * construct}, {@linkplain #URI(String) parse}, or
d75110673dc9 8215008: Clear confusion between URL/URI paths and file system paths
dfuchs
parents: 52700
diff changeset
   474
 * {@linkplain #resolve(String) resolve} a {@code URI}
d75110673dc9 8215008: Clear confusion between URL/URI paths and file system paths
dfuchs
parents: 52700
diff changeset
   475
 * 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
   476
 * instance.
d75110673dc9 8215008: Clear confusion between URL/URI paths and file system paths
dfuchs
parents: 52700
diff changeset
   477
 * <p>
d75110673dc9 8215008: Clear confusion between URL/URI paths and file system paths
dfuchs
parents: 52700
diff changeset
   478
 * 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
   479
 * 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
   480
 * 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
   481
 * the recommendations advised in <a
d75110673dc9 8215008: Clear confusion between URL/URI paths and file system paths
dfuchs
parents: 52700
diff changeset
   482
 * 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
   483
 * Section 7, Security Considerations</a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
 *
5820
4f5e99470724 6967036: Need to fix links with // in Javadoc comments
ohair
parents: 5627
diff changeset
   488
 * @see <a href="http://www.ietf.org/rfc/rfc2279.txt"><i>RFC&nbsp;2279: UTF-8, a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
 * transformation format of ISO 10646</i></a>, <br><a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
 * href="http://www.ietf.org/rfc/rfc2373.txt"><i>RFC&nbsp;2373: IPv6 Addressing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
 * Architecture</i></a>, <br><a
708
a780486c413c 6630348: Invalid html tags (extra double quote)
chegar
parents: 2
diff changeset
   492
 * href="http://www.ietf.org/rfc/rfc2396.txt"><i>RFC&nbsp;2396: Uniform
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
 * Resource Identifiers (URI): Generic Syntax</i></a>, <br><a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
 * href="http://www.ietf.org/rfc/rfc2732.txt"><i>RFC&nbsp;2732: Format for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
 * Literal IPv6 Addresses in URLs</i></a>, <br><a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
 * href="URISyntaxException.html">URISyntaxException</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
public final class URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    implements Comparable<URI>, Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    // Note: Comments containing the word "ASSERT" indicate places where a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    // throw of an InternalError should be replaced by an appropriate assertion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    // statement once asserts are enabled in the build.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    static final long serialVersionUID = -6052424284110960213L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    // -- Properties and components of this instance --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    // Components of all URIs: [<scheme>:]<scheme-specific-part>[#<fragment>]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    private transient String scheme;            // null ==> relative URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    private transient String fragment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    // Hierarchical URI components: [//<authority>]<path>[?<query>]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    private transient String authority;         // Registry or server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    // Server-based authority: [<userInfo>@]<host>[:<port>]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    private transient String userInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    private transient String host;              // null ==> registry-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    private transient int port = -1;            // -1 ==> undefined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    // Remaining components of hierarchical URIs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    private transient String path;              // null ==> opaque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    private transient String query;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
34783
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
   528
    // The remaining fields may be computed on demand, which is safe even in
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
   529
    // the face of multiple threads racing to initialize them
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
   530
    private transient String schemeSpecificPart;
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
   531
    private transient int hash;        // Zero ==> undefined
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
   532
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
   533
    private transient String decodedUserInfo;
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
   534
    private transient String decodedAuthority;
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
   535
    private transient String decodedPath;
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
   536
    private transient String decodedQuery;
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
   537
    private transient String decodedFragment;
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
   538
    private transient String decodedSchemeSpecificPart;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * The string form of this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    private volatile String string;             // The only serializable field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    // -- Constructors and factories --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    private URI() { }                           // Used internally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * Constructs a URI by parsing the given string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * <p> This constructor parses the given string exactly as specified by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * grammar in <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * href="http://www.ietf.org/rfc/rfc2396.txt">RFC&nbsp;2396</a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * Appendix&nbsp;A, <b><i>except for the following deviations:</i></b> </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     *
19049
543b417ea7b2 8021421: More doclint fixes in java.net
chegar
parents: 18800
diff changeset
   561
     * <ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     *   <li><p> An empty authority component is permitted as long as it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     *   followed by a non-empty path, a query component, or a fragment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     *   component.  This allows the parsing of URIs such as
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   566
     *   {@code "file:///foo/bar"}, which seems to be the intent of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     *   RFC&nbsp;2396 although the grammar does not permit it.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     *   authority component is empty then the user-information, host, and port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     *   components are undefined. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *   <li><p> Empty relative paths are permitted; this seems to be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     *   intent of RFC&nbsp;2396 although the grammar does not permit it.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     *   primary consequence of this deviation is that a standalone fragment
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   574
     *   such as {@code "#foo"} parses as a relative URI with an empty path
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     *   and the given fragment, and can be usefully <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     *   href="#resolve-frag">resolved</a> against a base URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     *   <li><p> IPv4 addresses in host components are parsed rigorously, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     *   specified by <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     *   href="http://www.ietf.org/rfc/rfc2732.txt">RFC&nbsp;2732</a>: Each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     *   element of a dotted-quad address must contain no more than three
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     *   decimal digits.  Each element is further constrained to have a value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     *   no greater than 255. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *   <li> <p> Hostnames in host components that comprise only a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     *   domain label are permitted to start with an <i>alphanum</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     *   character. This seems to be the intent of <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     *   href="http://www.ietf.org/rfc/rfc2396.txt">RFC&nbsp;2396</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     *   section&nbsp;3.2.2 although the grammar does not permit it. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     *   consequence of this deviation is that the authority component of a
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   591
     *   hierarchical URI such as {@code s://123}, will parse as a server-based
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     *   authority. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     *   <li><p> IPv6 addresses are permitted for the host component.  An IPv6
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   595
     *   address must be enclosed in square brackets ({@code '['} and
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   596
     *   {@code ']'}) as specified by <a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     *   href="http://www.ietf.org/rfc/rfc2732.txt">RFC&nbsp;2732</a>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     *   IPv6 address itself must parse according to <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     *   href="http://www.ietf.org/rfc/rfc2373.txt">RFC&nbsp;2373</a>.  IPv6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     *   addresses are further constrained to describe no more than sixteen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     *   bytes of address information, a constraint implicit in RFC&nbsp;2373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     *   but not expressible in the grammar. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     *   <li><p> Characters in the <i>other</i> category are permitted wherever
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     *   RFC&nbsp;2396 permits <i>escaped</i> octets, that is, in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     *   user-information, path, query, and fragment components, as well as in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     *   the authority component if the authority is registry-based.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     *   allows URIs to contain Unicode characters beyond those in the US-ASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     *   character set. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * @param  str   The string to be parsed into a URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * @throws  NullPointerException
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   616
     *          If {@code str} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * @throws  URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     *          If the given string violates RFC&nbsp;2396, as augmented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     *          by the above deviations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    public URI(String str) throws URISyntaxException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        new Parser(str).parse(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * Constructs a hierarchical URI from the given components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * <p> If a scheme is given then the path, if also given, must either be
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   630
     * empty or begin with a slash character ({@code '/'}).  Otherwise a
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   631
     * component of the new URI may be left undefined by passing {@code null}
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   632
     * for the corresponding parameter or, in the case of the {@code port}
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   633
     * parameter, by passing {@code -1}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * <p> This constructor first builds a URI string from the given components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * according to the rules specified in <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * href="http://www.ietf.org/rfc/rfc2396.txt">RFC&nbsp;2396</a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * section&nbsp;5.2, step&nbsp;7: </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     *   <li><p> Initially, the result string is empty. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     *   <li><p> If a scheme is given then it is appended to the result,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   645
     *   followed by a colon character ({@code ':'}).  </p></li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     *   <li><p> If user information, a host, or a port are given then the
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   648
     *   string {@code "//"} is appended.  </p></li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     *   <li><p> If user information is given then it is appended, followed by
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   651
     *   a commercial-at character ({@code '@'}).  Any character not in the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     *   <i>unreserved</i>, <i>punct</i>, <i>escaped</i>, or <i>other</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     *   categories is <a href="#quote">quoted</a>.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     *   <li><p> If a host is given then it is appended.  If the host is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     *   literal IPv6 address but is not enclosed in square brackets
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   657
     *   ({@code '['} and {@code ']'}) then the square brackets are added.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     *   </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     *   <li><p> If a port number is given then a colon character
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   661
     *   ({@code ':'}) is appended, followed by the port number in decimal.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     *   </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     *   <li><p> If a path is given then it is appended.  Any character not in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     *   the <i>unreserved</i>, <i>punct</i>, <i>escaped</i>, or <i>other</i>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   666
     *   categories, and not equal to the slash character ({@code '/'}) or the
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   667
     *   commercial-at character ({@code '@'}), is quoted.  </p></li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     *   <li><p> If a query is given then a question-mark character
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   670
     *   ({@code '?'}) is appended, followed by the query.  Any character that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     *   is not a <a href="#legal-chars">legal URI character</a> is quoted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     *   </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     *   <li><p> Finally, if a fragment is given then a hash character
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   675
     *   ({@code '#'}) is appended, followed by the fragment.  Any character
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     *   that is not a legal URI character is quoted.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * <p> The resulting URI string is then parsed as if by invoking the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * #URI(String)} constructor and then invoking the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * #parseServerAuthority()} method upon the result; this may cause a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * URISyntaxException} to be thrown.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * @param   scheme    Scheme name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * @param   userInfo  User name and authorization information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * @param   host      Host name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * @param   port      Port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * @param   path      Path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * @param   query     Query
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * @param   fragment  Fragment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * @throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     *         If both a scheme and a path are given but the path is relative,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     *         if the URI string constructed from the given components violates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     *         RFC&nbsp;2396, or if the authority component of the string is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     *         present but cannot be parsed as a server-based authority
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    public URI(String scheme,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
               String userInfo, String host, int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
               String path, String query, String fragment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        String s = toString(scheme, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                            null, userInfo, host, port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                            path, query, fragment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        checkPath(s, scheme, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        new Parser(s).parse(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * Constructs a hierarchical URI from the given components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * <p> If a scheme is given then the path, if also given, must either be
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   715
     * empty or begin with a slash character ({@code '/'}).  Otherwise a
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   716
     * component of the new URI may be left undefined by passing {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * for the corresponding parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * <p> This constructor first builds a URI string from the given components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * according to the rules specified in <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * href="http://www.ietf.org/rfc/rfc2396.txt">RFC&nbsp;2396</a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * section&nbsp;5.2, step&nbsp;7: </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     *   <li><p> Initially, the result string is empty.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     *   <li><p> If a scheme is given then it is appended to the result,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   729
     *   followed by a colon character ({@code ':'}).  </p></li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   731
     *   <li><p> If an authority is given then the string {@code "//"} is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     *   appended, followed by the authority.  If the authority contains a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     *   literal IPv6 address then the address must be enclosed in square
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   734
     *   brackets ({@code '['} and {@code ']'}).  Any character not in the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     *   <i>unreserved</i>, <i>punct</i>, <i>escaped</i>, or <i>other</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     *   categories, and not equal to the commercial-at character
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   737
     *   ({@code '@'}), is <a href="#quote">quoted</a>.  </p></li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     *   <li><p> If a path is given then it is appended.  Any character not in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     *   the <i>unreserved</i>, <i>punct</i>, <i>escaped</i>, or <i>other</i>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   741
     *   categories, and not equal to the slash character ({@code '/'}) or the
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   742
     *   commercial-at character ({@code '@'}), is quoted.  </p></li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     *   <li><p> If a query is given then a question-mark character
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   745
     *   ({@code '?'}) is appended, followed by the query.  Any character that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     *   is not a <a href="#legal-chars">legal URI character</a> is quoted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     *   </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     *   <li><p> Finally, if a fragment is given then a hash character
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   750
     *   ({@code '#'}) is appended, followed by the fragment.  Any character
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     *   that is not a legal URI character is quoted.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * <p> The resulting URI string is then parsed as if by invoking the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * #URI(String)} constructor and then invoking the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * #parseServerAuthority()} method upon the result; this may cause a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * URISyntaxException} to be thrown.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * @param   scheme     Scheme name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * @param   authority  Authority
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * @param   path       Path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * @param   query      Query
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * @param   fragment   Fragment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * @throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     *         If both a scheme and a path are given but the path is relative,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     *         if the URI string constructed from the given components violates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     *         RFC&nbsp;2396, or if the authority component of the string is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     *         present but cannot be parsed as a server-based authority
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    public URI(String scheme,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
               String authority,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
               String path, String query, String fragment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        String s = toString(scheme, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                            authority, null, null, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                            path, query, fragment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        checkPath(s, scheme, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        new Parser(s).parse(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * Constructs a hierarchical URI from the given components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   787
     * <p> A component may be left undefined by passing {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * <p> This convenience constructor works as if by invoking the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * seven-argument constructor as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   792
     * <blockquote>
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   793
     * {@code new} {@link #URI(String, String, String, int, String, String, String)
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   794
     * URI}{@code (scheme, null, host, -1, path, null, fragment);}
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   795
     * </blockquote>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * @param   scheme    Scheme name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * @param   host      Host name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * @param   path      Path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * @param   fragment  Fragment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * @throws  URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     *          If the URI string constructed from the given components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     *          violates RFC&nbsp;2396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    public URI(String scheme, String host, String path, String fragment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        this(scheme, null, host, -1, path, null, fragment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * Constructs a URI from the given components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   815
     * <p> A component may be left undefined by passing {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * <p> This constructor first builds a URI in string form using the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * components as follows:  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     *   <li><p> Initially, the result string is empty.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     *   <li><p> If a scheme is given then it is appended to the result,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   825
     *   followed by a colon character ({@code ':'}).  </p></li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     *   <li><p> If a scheme-specific part is given then it is appended.  Any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     *   character that is not a <a href="#legal-chars">legal URI character</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     *   is <a href="#quote">quoted</a>.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     *   <li><p> Finally, if a fragment is given then a hash character
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   832
     *   ({@code '#'}) is appended to the string, followed by the fragment.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     *   Any character that is not a legal URI character is quoted.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * <p> The resulting URI string is then parsed in order to create the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * URI instance as if by invoking the {@link #URI(String)} constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * this may cause a {@link URISyntaxException} to be thrown.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * @param   scheme    Scheme name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * @param   ssp       Scheme-specific part
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * @param   fragment  Fragment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * @throws  URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     *          If the URI string constructed from the given components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     *          violates RFC&nbsp;2396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
    public URI(String scheme, String ssp, String fragment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        new Parser(toString(scheme, ssp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                            null, null, null, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                            null, null, fragment))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            .parse(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    /**
41555
8a2a5a88376a 8168073: Speed up URI creation during module bootstrap
redestad
parents: 38866
diff changeset
   859
     * Constructs a simple URI consisting of only a scheme and a pre-validated
8a2a5a88376a 8168073: Speed up URI creation during module bootstrap
redestad
parents: 38866
diff changeset
   860
     * path. Provides a fast-path for some internal cases.
8a2a5a88376a 8168073: Speed up URI creation during module bootstrap
redestad
parents: 38866
diff changeset
   861
     */
8a2a5a88376a 8168073: Speed up URI creation during module bootstrap
redestad
parents: 38866
diff changeset
   862
    URI(String scheme, String path) {
8a2a5a88376a 8168073: Speed up URI creation during module bootstrap
redestad
parents: 38866
diff changeset
   863
        assert validSchemeAndPath(scheme, path);
8a2a5a88376a 8168073: Speed up URI creation during module bootstrap
redestad
parents: 38866
diff changeset
   864
        this.scheme = scheme;
8a2a5a88376a 8168073: Speed up URI creation during module bootstrap
redestad
parents: 38866
diff changeset
   865
        this.path = path;
8a2a5a88376a 8168073: Speed up URI creation during module bootstrap
redestad
parents: 38866
diff changeset
   866
    }
8a2a5a88376a 8168073: Speed up URI creation during module bootstrap
redestad
parents: 38866
diff changeset
   867
8a2a5a88376a 8168073: Speed up URI creation during module bootstrap
redestad
parents: 38866
diff changeset
   868
    private static boolean validSchemeAndPath(String scheme, String path) {
8a2a5a88376a 8168073: Speed up URI creation during module bootstrap
redestad
parents: 38866
diff changeset
   869
        try {
8a2a5a88376a 8168073: Speed up URI creation during module bootstrap
redestad
parents: 38866
diff changeset
   870
            URI u = new URI(scheme + ":" + path);
8a2a5a88376a 8168073: Speed up URI creation during module bootstrap
redestad
parents: 38866
diff changeset
   871
            return scheme.equals(u.scheme) && path.equals(u.path);
8a2a5a88376a 8168073: Speed up URI creation during module bootstrap
redestad
parents: 38866
diff changeset
   872
        } catch (URISyntaxException e) {
8a2a5a88376a 8168073: Speed up URI creation during module bootstrap
redestad
parents: 38866
diff changeset
   873
            return false;
8a2a5a88376a 8168073: Speed up URI creation during module bootstrap
redestad
parents: 38866
diff changeset
   874
        }
8a2a5a88376a 8168073: Speed up URI creation during module bootstrap
redestad
parents: 38866
diff changeset
   875
    }
8a2a5a88376a 8168073: Speed up URI creation during module bootstrap
redestad
parents: 38866
diff changeset
   876
8a2a5a88376a 8168073: Speed up URI creation during module bootstrap
redestad
parents: 38866
diff changeset
   877
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * Creates a URI by parsing the given string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * <p> This convenience factory method works as if by invoking the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * #URI(String)} constructor; any {@link URISyntaxException} thrown by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * constructor is caught and wrapped in a new {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * IllegalArgumentException} object, which is then thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * <p> This method is provided for use in situations where it is known that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * the given string is a legal URI, for example for URI constants declared
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 52427
diff changeset
   887
     * within a program, and so it would be considered a programming error
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * for the string not to parse as such.  The constructors, which throw
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 52427
diff changeset
   889
     * {@link URISyntaxException} directly, should be used in situations where a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * URI is being constructed from user input or from some other source that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * may be prone to errors.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * @param  str   The string to be parsed into a URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * @return The new URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * @throws  NullPointerException
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   897
     *          If {@code str} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     *          If the given string violates RFC&nbsp;2396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    public static URI create(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            return new URI(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        } catch (URISyntaxException x) {
6307
613f5033f5f2 6339649: URI.create should include a detail message when throwing IllegalArgumentException
michaelm
parents: 5820
diff changeset
   906
            throw new IllegalArgumentException(x.getMessage(), x);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    // -- Operations --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * Attempts to parse this URI's authority component, if defined, into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * user-information, host, and port components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * <p> If this URI's authority component has already been recognized as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * being server-based then it will already have been parsed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * user-information, host, and port components.  In this case, or if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * URI has no authority component, this method simply returns this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * <p> Otherwise this method attempts once more to parse the authority
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * component into user-information, host, and port components, and throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * an exception describing why the authority component could not be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * in that way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * <p> This method is provided because the generic URI syntax specified in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * <a href="http://www.ietf.org/rfc/rfc2396.txt">RFC&nbsp;2396</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * cannot always distinguish a malformed server-based authority from a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * legitimate registry-based authority.  It must therefore treat some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * instances of the former as instances of the latter.  The authority
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   932
     * component in the URI string {@code "//foo:bar"}, for example, is not a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * legal server-based authority but it is legal as a registry-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * authority.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * <p> In many common situations, for example when working URIs that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * known to be either URNs or URLs, the hierarchical URIs being used will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * always be server-based.  They therefore must either be parsed as such or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * treated as an error.  In these cases a statement such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * <blockquote>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   942
     * {@code URI }<i>u</i>{@code  = new URI(str).parseServerAuthority();}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * <p> can be used to ensure that <i>u</i> always refers to a URI that, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * it has an authority component, has a server-based authority with proper
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * user-information, host, and port components.  Invoking this method also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * ensures that if the authority could not be parsed in that way then an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * appropriate diagnostic message can be issued based upon the exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * that is thrown. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * @return  A URI whose authority field has been parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     *          as a server-based authority
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * @throws  URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     *          If the authority component of this URI is defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     *          but cannot be parsed as a server-based authority
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     *          according to RFC&nbsp;2396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    public URI parseServerAuthority()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        // We could be clever and cache the error message and index from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        // exception thrown during the original parse, but that would require
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        // either more fields or a more-obscure representation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        if ((host != null) || (authority == null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            return this;
34783
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
   968
        new Parser(toString()).parse(true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * Normalizes this URI's path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * <p> If this URI is opaque, or if its path is already in normal form,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * then this URI is returned.  Otherwise a new URI is constructed that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * identical to this URI except that its path is computed by normalizing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * this URI's path in a manner consistent with <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * href="http://www.ietf.org/rfc/rfc2396.txt">RFC&nbsp;2396</a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * section&nbsp;5.2, step&nbsp;6, sub-steps&nbsp;c through&nbsp;f; that is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   985
     *   <li><p> All {@code "."} segments are removed. </p></li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   987
     *   <li><p> If a {@code ".."} segment is preceded by a non-{@code ".."}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     *   segment then both of these segments are removed.  This step is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     *   repeated until it is no longer applicable. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     *   <li><p> If the path is relative, and if its first segment contains a
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   992
     *   colon character ({@code ':'}), then a {@code "."} segment is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     *   prepended.  This prevents a relative URI with a path such as
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   994
     *   {@code "a:b/c/d"} from later being re-parsed as an opaque URI with a
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   995
     *   scheme of {@code "a"} and a scheme-specific part of {@code "b/c/d"}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     *   <b><i>(Deviation from RFC&nbsp;2396)</i></b> </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1000
     * <p> A normalized path will begin with one or more {@code ".."} segments
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1001
     * if there were insufficient non-{@code ".."} segments preceding them to
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1002
     * allow their removal.  A normalized path will begin with a {@code "."}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * segment if one was inserted by step 3 above.  Otherwise, a normalized
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1004
     * path will not contain any {@code "."} or {@code ".."} segments. </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * @return  A URI equivalent to this URI,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     *          but whose path is in normal form
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    public URI normalize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        return normalize(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * Resolves the given URI against this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * <p> If the given URI is already absolute, or if this URI is opaque, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * the given URI is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     *
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 41555
diff changeset
  1019
     * <p><a id="resolve-frag"></a> If the given URI's fragment component is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * defined, its path component is empty, and its scheme, authority, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * query components are undefined, then a URI with the given fragment but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * with all other components equal to those of this URI is returned.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     * allows a URI representing a standalone fragment reference, such as
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1024
     * {@code "#foo"}, to be usefully resolved against a base URI.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * <p> Otherwise this method constructs a new hierarchical URI in a manner
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * consistent with <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * href="http://www.ietf.org/rfc/rfc2396.txt">RFC&nbsp;2396</a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * section&nbsp;5.2; that is: </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     *   <li><p> A new URI is constructed with this URI's scheme and the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     *   URI's query and fragment components. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     *   <li><p> If the given URI has an authority component then the new URI's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     *   authority and path are taken from the given URI. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     *   <li><p> Otherwise the new URI's authority component is copied from
8562
72c0c44969c1 7018137: HTML4 compliance issues
chegar
parents: 7668
diff changeset
  1040
     *   this URI, and its path is computed as follows: </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     *
19049
543b417ea7b2 8021421: More doclint fixes in java.net
chegar
parents: 18800
diff changeset
  1042
     *   <ol>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     *     <li><p> If the given URI's path is absolute then the new URI's path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     *     is taken from the given URI. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     *     <li><p> Otherwise the given URI's path is relative, and so the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     *     URI's path is computed by resolving the path of the given URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     *     against the path of this URI.  This is done by concatenating all but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     *     the last segment of this URI's path, if any, with the given URI's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     *     path and then normalizing the result as if by invoking the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     *     #normalize() normalize} method. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     *
8562
72c0c44969c1 7018137: HTML4 compliance issues
chegar
parents: 7668
diff changeset
  1054
     *   </ol></li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * <p> The result of this method is absolute if, and only if, either this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * URI is absolute or the given URI is absolute.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * @param  uri  The URI to be resolved against this URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * @return The resulting URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     * @throws  NullPointerException
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1065
     *          If {@code uri} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
    public URI resolve(URI uri) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        return resolve(this, uri);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * Constructs a new URI by parsing the given string and then resolving it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     * against this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * <p> This convenience method works as if invoking it were equivalent to
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1076
     * evaluating the expression {@link #resolve(java.net.URI)
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1077
     * resolve}{@code (URI.}{@link #create(String) create}{@code (str))}. </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * @param  str   The string to be parsed into a URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * @return The resulting URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * @throws  NullPointerException
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1083
     *          If {@code str} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     *          If the given string violates RFC&nbsp;2396
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
    public URI resolve(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        return resolve(URI.create(str));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * Relativizes the given URI against this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * <p> The relativization of the given URI against this URI is computed as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * follows: </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     *   <li><p> If either this URI or the given URI are opaque, or if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     *   scheme and authority components of the two URIs are not identical, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     *   if the path of this URI is not a prefix of the path of the given URI,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     *   then the given URI is returned. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     *   <li><p> Otherwise a new relative hierarchical URI is constructed with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     *   query and fragment components taken from the given URI and with a path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     *   component computed by removing this URI's path from the beginning of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     *   the given URI's path. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     * @param  uri  The URI to be relativized against this URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     * @return The resulting URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     * @throws  NullPointerException
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1116
     *          If {@code uri} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    public URI relativize(URI uri) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        return relativize(this, uri);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * Constructs a URL from this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * <p> This convenience method works as if invoking it were equivalent to
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1126
     * evaluating the expression {@code new URL(this.toString())} after
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * first checking that this URI is absolute. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     * @return  A URL constructed from this URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     *          If this URL is not absolute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * @throws  MalformedURLException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     *          If a protocol handler for the URL could not be found,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     *          or if some other error occurred while constructing the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     */
35393
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 34939
diff changeset
  1138
    public URL toURL() throws MalformedURLException {
12d55e1947f7 8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents: 34939
diff changeset
  1139
        return URL.fromURI(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
    // -- Component access methods --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * Returns the scheme component of this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     * <p> The scheme component of a URI, if defined, only contains characters
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1148
     * in the <i>alphanum</i> category and in the string {@code "-.+"}.  A
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * scheme always starts with an <i>alpha</i> character. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * The scheme component of a URI cannot contain escaped octets, hence this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * method does not perform any decoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     * @return  The scheme component of this URI,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1155
     *          or {@code null} if the scheme is undefined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
    public String getScheme() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        return scheme;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * Tells whether or not this URI is absolute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * <p> A URI is absolute if, and only if, it has a scheme component. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1166
     * @return  {@code true} if, and only if, this URI is absolute
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
    public boolean isAbsolute() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        return scheme != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * Tells whether or not this URI is opaque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     * <p> A URI is opaque if, and only if, it is absolute and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     * scheme-specific part does not begin with a slash character ('/').
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * An opaque URI has a scheme, a scheme-specific part, and possibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     * a fragment; all other components are undefined. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1180
     * @return  {@code true} if, and only if, this URI is opaque
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
    public boolean isOpaque() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        return path == null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     * Returns the raw scheme-specific part of this URI.  The scheme-specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     * part is never undefined, though it may be empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * <p> The scheme-specific part of a URI only contains legal URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     * characters. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     * @return  The raw scheme-specific part of this URI
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1194
     *          (never {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
    public String getRawSchemeSpecificPart() {
34783
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1197
        String part = schemeSpecificPart;
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1198
        if (part != null) {
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1199
            return part;
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1200
        }
34939
4fd867517aec 8146686: Create the schemeSpecificPart for non-opaque URIs lazily
redestad
parents: 34887
diff changeset
  1201
4fd867517aec 8146686: Create the schemeSpecificPart for non-opaque URIs lazily
redestad
parents: 34887
diff changeset
  1202
        String s = string;
4fd867517aec 8146686: Create the schemeSpecificPart for non-opaque URIs lazily
redestad
parents: 34887
diff changeset
  1203
        if (s != null) {
4fd867517aec 8146686: Create the schemeSpecificPart for non-opaque URIs lazily
redestad
parents: 34887
diff changeset
  1204
            // if string is defined, components will have been parsed
4fd867517aec 8146686: Create the schemeSpecificPart for non-opaque URIs lazily
redestad
parents: 34887
diff changeset
  1205
            int start = 0;
4fd867517aec 8146686: Create the schemeSpecificPart for non-opaque URIs lazily
redestad
parents: 34887
diff changeset
  1206
            int end = s.length();
4fd867517aec 8146686: Create the schemeSpecificPart for non-opaque URIs lazily
redestad
parents: 34887
diff changeset
  1207
            if (scheme != null) {
4fd867517aec 8146686: Create the schemeSpecificPart for non-opaque URIs lazily
redestad
parents: 34887
diff changeset
  1208
                start = scheme.length() + 1;
4fd867517aec 8146686: Create the schemeSpecificPart for non-opaque URIs lazily
redestad
parents: 34887
diff changeset
  1209
            }
4fd867517aec 8146686: Create the schemeSpecificPart for non-opaque URIs lazily
redestad
parents: 34887
diff changeset
  1210
            if (fragment != null) {
4fd867517aec 8146686: Create the schemeSpecificPart for non-opaque URIs lazily
redestad
parents: 34887
diff changeset
  1211
                end -= fragment.length() + 1;
4fd867517aec 8146686: Create the schemeSpecificPart for non-opaque URIs lazily
redestad
parents: 34887
diff changeset
  1212
            }
4fd867517aec 8146686: Create the schemeSpecificPart for non-opaque URIs lazily
redestad
parents: 34887
diff changeset
  1213
            if (path != null && path.length() == end - start) {
4fd867517aec 8146686: Create the schemeSpecificPart for non-opaque URIs lazily
redestad
parents: 34887
diff changeset
  1214
                part = path;
4fd867517aec 8146686: Create the schemeSpecificPart for non-opaque URIs lazily
redestad
parents: 34887
diff changeset
  1215
            } else {
4fd867517aec 8146686: Create the schemeSpecificPart for non-opaque URIs lazily
redestad
parents: 34887
diff changeset
  1216
                part = s.substring(start, end);
4fd867517aec 8146686: Create the schemeSpecificPart for non-opaque URIs lazily
redestad
parents: 34887
diff changeset
  1217
            }
4fd867517aec 8146686: Create the schemeSpecificPart for non-opaque URIs lazily
redestad
parents: 34887
diff changeset
  1218
        } else {
4fd867517aec 8146686: Create the schemeSpecificPart for non-opaque URIs lazily
redestad
parents: 34887
diff changeset
  1219
            StringBuilder sb = new StringBuilder();
4fd867517aec 8146686: Create the schemeSpecificPart for non-opaque URIs lazily
redestad
parents: 34887
diff changeset
  1220
            appendSchemeSpecificPart(sb, null, getAuthority(), getUserInfo(),
34783
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1221
                                 host, port, getPath(), getQuery());
34939
4fd867517aec 8146686: Create the schemeSpecificPart for non-opaque URIs lazily
redestad
parents: 34887
diff changeset
  1222
            part = sb.toString();
34783
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1223
        }
34939
4fd867517aec 8146686: Create the schemeSpecificPart for non-opaque URIs lazily
redestad
parents: 34887
diff changeset
  1224
        return schemeSpecificPart = part;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     * Returns the decoded scheme-specific part of this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     * <p> The string returned by this method is equal to that returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     * {@link #getRawSchemeSpecificPart() getRawSchemeSpecificPart} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     * except that all sequences of escaped octets are <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     * href="#decode">decoded</a>.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     * @return  The decoded scheme-specific part of this URI
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1236
     *          (never {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
    public String getSchemeSpecificPart() {
34783
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1239
        String part = decodedSchemeSpecificPart;
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1240
        if (part == null) {
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1241
            decodedSchemeSpecificPart = part = decode(getRawSchemeSpecificPart());
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1242
        }
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1243
        return part;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
     * Returns the raw authority component of this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     * <p> The authority component of a URI, if defined, only contains the
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1250
     * commercial-at character ({@code '@'}) and characters in the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
     * <i>unreserved</i>, <i>punct</i>, <i>escaped</i>, and <i>other</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     * categories.  If the authority is server-based then it is further
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     * constrained to have valid user-information, host, and port
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * components. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * @return  The raw authority component of this URI,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1257
     *          or {@code null} if the authority is undefined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
    public String getRawAuthority() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        return authority;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * Returns the decoded authority component of this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     * <p> The string returned by this method is equal to that returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     * {@link #getRawAuthority() getRawAuthority} method except that all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     * sequences of escaped octets are <a href="#decode">decoded</a>.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     * @return  The decoded authority component of this URI,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1271
     *          or {@code null} if the authority is undefined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
    public String getAuthority() {
34783
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1274
        String auth = decodedAuthority;
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1275
        if ((auth == null) && (authority != null)) {
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1276
            decodedAuthority = auth = decode(authority);
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1277
        }
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1278
        return auth;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     * Returns the raw user-information component of this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     * <p> The user-information component of a URI, if defined, only contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     * characters in the <i>unreserved</i>, <i>punct</i>, <i>escaped</i>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     * <i>other</i> categories. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
     * @return  The raw user-information component of this URI,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1289
     *          or {@code null} if the user information is undefined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
    public String getRawUserInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
        return userInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     * Returns the decoded user-information component of this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     * <p> The string returned by this method is equal to that returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     * {@link #getRawUserInfo() getRawUserInfo} method except that all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
     * sequences of escaped octets are <a href="#decode">decoded</a>.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
     * @return  The decoded user-information component of this URI,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1303
     *          or {@code null} if the user information is undefined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
    public String getUserInfo() {
34783
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1306
        String user = decodedUserInfo;
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1307
        if ((user == null) && (userInfo != null)) {
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1308
            decodedUserInfo = user = decode(userInfo);
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1309
        }
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1310
        return user;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     * Returns the host component of this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     * <p> The host component of a URI, if defined, will have one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
     * following forms: </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     *
19049
543b417ea7b2 8021421: More doclint fixes in java.net
chegar
parents: 18800
diff changeset
  1319
     * <ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     *   <li><p> A domain name consisting of one or more <i>labels</i>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1322
     *   separated by period characters ({@code '.'}), optionally followed by
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     *   a period character.  Each label consists of <i>alphanum</i> characters
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1324
     *   as well as hyphen characters ({@code '-'}), though hyphens never
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
     *   occur as the first or last characters in a label. The rightmost
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     *   label of a domain name consisting of two or more labels, begins
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     *   with an <i>alpha</i> character. </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     *   <li><p> A dotted-quad IPv4 address of the form
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1330
     *   <i>digit</i>{@code +.}<i>digit</i>{@code +.}<i>digit</i>{@code +.}<i>digit</i>{@code +},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     *   where no <i>digit</i> sequence is longer than three characters and no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     *   sequence has a value larger than 255. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1334
     *   <li><p> An IPv6 address enclosed in square brackets ({@code '['} and
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1335
     *   {@code ']'}) and consisting of hexadecimal digits, colon characters
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1336
     *   ({@code ':'}), and possibly an embedded IPv4 address.  The full
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     *   syntax of IPv6 addresses is specified in <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     *   href="http://www.ietf.org/rfc/rfc2373.txt"><i>RFC&nbsp;2373: IPv6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     *   Addressing Architecture</i></a>.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
     * The host component of a URI cannot contain escaped octets, hence this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
     * method does not perform any decoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     * @return  The host component of this URI,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1347
     *          or {@code null} if the host is undefined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
    public String getHost() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
        return host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
     * Returns the port number of this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     * <p> The port component of a URI, if defined, is a non-negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
     * integer. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     * @return  The port component of this URI,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1360
     *          or {@code -1} if the port is undefined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
    public int getPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        return port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     * Returns the raw path component of this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
     * <p> The path component of a URI, if defined, only contains the slash
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1370
     * character ({@code '/'}), the commercial-at character ({@code '@'}),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     * and characters in the <i>unreserved</i>, <i>punct</i>, <i>escaped</i>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     * and <i>other</i> categories. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
     * @return  The path component of this URI,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1375
     *          or {@code null} if the path is undefined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
    public String getRawPath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        return path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
     * Returns the decoded path component of this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
     * <p> The string returned by this method is equal to that returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
     * {@link #getRawPath() getRawPath} method except that all sequences of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
     * escaped octets are <a href="#decode">decoded</a>.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     * @return  The decoded path component of this URI,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1389
     *          or {@code null} if the path is undefined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
    public String getPath() {
34783
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1392
        String decoded = decodedPath;
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1393
        if ((decoded == null) && (path != null)) {
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1394
            decodedPath = decoded = decode(path);
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1395
        }
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1396
        return decoded;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     * Returns the raw query component of this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     * <p> The query component of a URI, if defined, only contains legal URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     * characters. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
     * @return  The raw query component of this URI,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1406
     *          or {@code null} if the query is undefined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
    public String getRawQuery() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
        return query;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
     * Returns the decoded query component of this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
     * <p> The string returned by this method is equal to that returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
     * {@link #getRawQuery() getRawQuery} method except that all sequences of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
     * escaped octets are <a href="#decode">decoded</a>.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     * @return  The decoded query component of this URI,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1420
     *          or {@code null} if the query is undefined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
    public String getQuery() {
34783
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1423
        String decoded = decodedQuery;
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1424
        if ((decoded == null) && (query != null)) {
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1425
            decodedQuery = decoded = decode(query, false);
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1426
        }
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1427
        return decoded;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
     * Returns the raw fragment component of this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
     * <p> The fragment component of a URI, if defined, only contains legal URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
     * characters. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
     * @return  The raw fragment component of this URI,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1437
     *          or {@code null} if the fragment is undefined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
    public String getRawFragment() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
        return fragment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
     * Returns the decoded fragment component of this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
     * <p> The string returned by this method is equal to that returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
     * {@link #getRawFragment() getRawFragment} method except that all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
     * sequences of escaped octets are <a href="#decode">decoded</a>.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
     * @return  The decoded fragment component of this URI,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1451
     *          or {@code null} if the fragment is undefined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
    public String getFragment() {
34783
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1454
        String decoded = decodedFragment;
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1455
        if ((decoded == null) && (fragment != null)) {
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1456
            decodedFragment = decoded = decode(fragment, false);
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1457
        }
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1458
        return decoded;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
    // -- Equality, comparison, hash code, toString, and serialization --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
     * Tests this URI for equality with another object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
     * <p> If the given object is not a URI then this method immediately
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1468
     * returns {@code false}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
     * <p> For two URIs to be considered equal requires that either both are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
     * opaque or both are hierarchical.  Their schemes must either both be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
     * undefined or else be equal without regard to case. Their fragments
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
     * must either both be undefined or else be equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
     * <p> For two opaque URIs to be considered equal, their scheme-specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
     * parts must be equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
     * <p> For two hierarchical URIs to be considered equal, their paths must
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
     * be equal and their queries must either both be undefined or else be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
     * equal.  Their authorities must either both be undefined, or both be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
     * registry-based, or both be server-based.  If their authorities are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
     * defined and are registry-based, then they must be equal.  If their
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
     * authorities are defined and are server-based, then their hosts must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
     * equal without regard to case, their port numbers must be equal, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
     * their user-information components must be equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
     * <p> When testing the user-information, path, query, fragment, authority,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
     * or scheme-specific parts of two URIs for equality, the raw forms rather
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
     * than the encoded forms of these components are compared and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
     * hexadecimal digits of escaped octets are compared without regard to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
     * case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
     * <p> This method satisfies the general contract of the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
     * java.lang.Object#equals(Object) Object.equals} method. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
     * @param   ob   The object to which this object is to be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1498
     * @return  {@code true} if, and only if, the given object is a URI that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
     *          is identical to this URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
    public boolean equals(Object ob) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
        if (ob == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        if (!(ob instanceof URI))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
        URI that = (URI)ob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
        if (this.isOpaque() != that.isOpaque()) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
        if (!equalIgnoringCase(this.scheme, that.scheme)) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
        if (!equal(this.fragment, that.fragment)) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
        // Opaque
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
        if (this.isOpaque())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
            return equal(this.schemeSpecificPart, that.schemeSpecificPart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
        // Hierarchical
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
        if (!equal(this.path, that.path)) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
        if (!equal(this.query, that.query)) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
        // Authorities
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
        if (this.authority == that.authority) return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
        if (this.host != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
            // Server-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
            if (!equal(this.userInfo, that.userInfo)) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
            if (!equalIgnoringCase(this.host, that.host)) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
            if (this.port != that.port) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
        } else if (this.authority != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
            // Registry-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
            if (!equal(this.authority, that.authority)) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
        } else if (this.authority != that.authority) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
     * Returns a hash-code value for this URI.  The hash code is based upon all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
     * of the URI's components, and satisfies the general contract of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
     * {@link java.lang.Object#hashCode() Object.hashCode} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
     * @return  A hash-code value for this URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
    public int hashCode() {
34783
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1544
        int h = hash;
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1545
        if (h == 0) {
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1546
            h = hashIgnoringCase(0, scheme);
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1547
            h = hash(h, fragment);
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1548
            if (isOpaque()) {
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1549
                h = hash(h, schemeSpecificPart);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
            } else {
34783
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1551
                h = hash(h, path);
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1552
                h = hash(h, query);
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1553
                if (host != null) {
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1554
                    h = hash(h, userInfo);
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1555
                    h = hashIgnoringCase(h, host);
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1556
                    h += 1949 * port;
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1557
                } else {
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1558
                    h = hash(h, authority);
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1559
                }
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1560
            }
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1561
            if (h != 0) {
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1562
                hash = h;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
        return h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
     * Compares this URI to another object, which must be a URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
     * <p> When comparing corresponding components of two URIs, if one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
     * component is undefined but the other is defined then the first is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
     * considered to be less than the second.  Unless otherwise noted, string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
     * components are ordered according to their natural, case-sensitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
     * ordering as defined by the {@link java.lang.String#compareTo(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
     * String.compareTo} method.  String components that are subject to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
     * encoding are compared by comparing their raw forms rather than their
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
     * encoded forms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
     * <p> The ordering of URIs is defined as follows: </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
     *
19049
543b417ea7b2 8021421: More doclint fixes in java.net
chegar
parents: 18800
diff changeset
  1582
     * <ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
     *   <li><p> Two URIs with different schemes are ordered according the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
     *   ordering of their schemes, without regard to case. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
     *   <li><p> A hierarchical URI is considered to be less than an opaque URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
     *   with an identical scheme. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
     *   <li><p> Two opaque URIs with identical schemes are ordered according
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
     *   to the ordering of their scheme-specific parts. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
     *   <li><p> Two opaque URIs with identical schemes and scheme-specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
     *   parts are ordered according to the ordering of their
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
     *   fragments. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
     *   <li><p> Two hierarchical URIs with identical schemes are ordered
8562
72c0c44969c1 7018137: HTML4 compliance issues
chegar
parents: 7668
diff changeset
  1598
     *   according to the ordering of their authority components: </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
     *
19049
543b417ea7b2 8021421: More doclint fixes in java.net
chegar
parents: 18800
diff changeset
  1600
     *   <ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
     *     <li><p> If both authority components are server-based then the URIs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
     *     are ordered according to their user-information components; if these
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
     *     components are identical then the URIs are ordered according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
     *     ordering of their hosts, without regard to case; if the hosts are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
     *     identical then the URIs are ordered according to the ordering of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
     *     their ports. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
     *     <li><p> If one or both authority components are registry-based then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
     *     the URIs are ordered according to the ordering of their authority
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
     *     components. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
     *
8562
72c0c44969c1 7018137: HTML4 compliance issues
chegar
parents: 7668
diff changeset
  1613
     *   </ul></li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
     *   <li><p> Finally, two hierarchical URIs with identical schemes and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
     *   authority components are ordered according to the ordering of their
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
     *   paths; if their paths are identical then they are ordered according to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
     *   the ordering of their queries; if the queries are identical then they
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
     *   are ordered according to the order of their fragments. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
     * <p> This method satisfies the general contract of the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
     * java.lang.Comparable#compareTo(Object) Comparable.compareTo}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
     * method. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
     * @param   that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
     *          The object to which this URI is to be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
     * @return  A negative integer, zero, or a positive integer as this URI is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
     *          less than, equal to, or greater than the given URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
     * @throws  ClassCastException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
     *          If the given object is not a URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
    public int compareTo(URI that) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
        int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
        if ((c = compareIgnoringCase(this.scheme, that.scheme)) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
            return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
        if (this.isOpaque()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
            if (that.isOpaque()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
                // Both opaque
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
                if ((c = compare(this.schemeSpecificPart,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
                                 that.schemeSpecificPart)) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
                    return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
                return compare(this.fragment, that.fragment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
            return +1;                  // Opaque > hierarchical
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
        } else if (that.isOpaque()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
            return -1;                  // Hierarchical < opaque
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
        // Hierarchical
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
        if ((this.host != null) && (that.host != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
            // Both server-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
            if ((c = compare(this.userInfo, that.userInfo)) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
                return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
            if ((c = compareIgnoringCase(this.host, that.host)) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
                return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
            if ((c = this.port - that.port) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
                return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
            // If one or both authorities are registry-based then we simply
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
            // compare them in the usual, case-sensitive way.  If one is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
            // registry-based and one is server-based then the strings are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
            // guaranteed to be unequal, hence the comparison will never return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
            // zero and the compareTo and equals methods will remain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
            // consistent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
            if ((c = compare(this.authority, that.authority)) != 0) return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
        if ((c = compare(this.path, that.path)) != 0) return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
        if ((c = compare(this.query, that.query)) != 0) return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
        return compare(this.fragment, that.fragment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
     * Returns the content of this URI as a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
     * <p> If this URI was created by invoking one of the constructors in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
     * class then a string equivalent to the original input string, or to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
     * string computed from the originally-given components, as appropriate, is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
     * returned.  Otherwise this URI was created by normalization, resolution,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
     * or relativization, and so a string is constructed from this URI's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
     * components according to the rules specified in <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
     * href="http://www.ietf.org/rfc/rfc2396.txt">RFC&nbsp;2396</a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
     * section&nbsp;5.2, step&nbsp;7. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
     * @return  The string form of this URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
    public String toString() {
34783
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1694
        String s = string;
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1695
        if (s == null) {
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1696
            s = defineString();
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1697
        }
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1698
        return s;
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1699
    }
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1700
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1701
    private String defineString() {
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1702
        String s = string;
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1703
        if (s != null) {
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1704
            return s;
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1705
        }
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1706
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1707
        StringBuilder sb = new StringBuilder();
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1708
        if (scheme != null) {
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1709
            sb.append(scheme);
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1710
            sb.append(':');
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1711
        }
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1712
        if (isOpaque()) {
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1713
            sb.append(schemeSpecificPart);
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1714
        } else {
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1715
            if (host != null) {
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1716
                sb.append("//");
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1717
                if (userInfo != null) {
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1718
                    sb.append(userInfo);
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1719
                    sb.append('@');
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1720
                }
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1721
                boolean needBrackets = ((host.indexOf(':') >= 0)
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1722
                        && !host.startsWith("[")
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1723
                        && !host.endsWith("]"));
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1724
                if (needBrackets) sb.append('[');
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1725
                sb.append(host);
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1726
                if (needBrackets) sb.append(']');
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1727
                if (port != -1) {
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1728
                    sb.append(':');
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1729
                    sb.append(port);
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1730
                }
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1731
            } else if (authority != null) {
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1732
                sb.append("//");
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1733
                sb.append(authority);
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1734
            }
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1735
            if (path != null)
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1736
                sb.append(path);
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1737
            if (query != null) {
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1738
                sb.append('?');
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1739
                sb.append(query);
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1740
            }
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1741
        }
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1742
        if (fragment != null) {
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1743
            sb.append('#');
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1744
            sb.append(fragment);
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1745
        }
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1746
        return string = sb.toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
     * Returns the content of this URI as a US-ASCII string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
     * <p> If this URI does not contain any characters in the <i>other</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
     * category then an invocation of this method will return the same value as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
     * an invocation of the {@link #toString() toString} method.  Otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
     * this method works as if by invoking that method and then <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
     * href="#encode">encoding</a> the result.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
     * @return  The string form of this URI, encoded as needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
     *          so that it only contains characters in the US-ASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
     *          charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
    public String toASCIIString() {
34783
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1763
        return encode(toString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
    // -- Serialization support --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
     * Saves the content of this URI to the given serial stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1772
     * <p> The only serializable field of a URI instance is its {@code string}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
     * field.  That field is given a value, if it does not have one already,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
     * and then the {@link java.io.ObjectOutputStream#defaultWriteObject()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
     * method of the given object-output stream is invoked. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
     * @param  os  The object-output stream to which this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
     *             is to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
    private void writeObject(ObjectOutputStream os)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
        defineString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
        os.defaultWriteObject();        // Writes the string field only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
     * Reconstitutes a URI from the given serial stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
     * <p> The {@link java.io.ObjectInputStream#defaultReadObject()} method is
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1791
     * invoked to read the value of the {@code string} field.  The result is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
     * then parsed in the usual way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
     * @param  is  The object-input stream from which this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
     *             is being read
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
    private void readObject(ObjectInputStream is)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
        throws ClassNotFoundException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
        port = -1;                      // Argh
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
        is.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
            new Parser(string).parse(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
        } catch (URISyntaxException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
            IOException y = new InvalidObjectException("Invalid URI");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
            y.initCause(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
            throw y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
    // -- End of public methods --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
    // -- Utility methods for string-field comparison and hashing --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
    // These methods return appropriate values for null string arguments,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
    // thereby simplifying the equals, hashCode, and compareTo methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
    // The case-ignoring methods should only be applied to strings whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
    // characters are all known to be US-ASCII.  Because of this restriction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
    // these methods are faster than the similar methods in the String class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
    // US-ASCII only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
    private static int toLower(char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
        if ((c >= 'A') && (c <= 'Z'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
            return c + ('a' - 'A');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
15272
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1831
    // US-ASCII only
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1832
    private static int toUpper(char c) {
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1833
        if ((c >= 'a') && (c <= 'z'))
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1834
            return c - ('a' - 'A');
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1835
        return c;
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1836
    }
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1837
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
    private static boolean equal(String s, String t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
        if (s == t) return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
        if ((s != null) && (t != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
            if (s.length() != t.length())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
            if (s.indexOf('%') < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
                return s.equals(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
            int n = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
            for (int i = 0; i < n;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
                char c = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
                char d = t.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
                if (c != '%') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
                    if (c != d)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
                    i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
                }
10422
83581a2cf49d 7041800: URI.equals may incorrectly return true with escaped octets
chegar
parents: 9035
diff changeset
  1855
                if (d != '%')
83581a2cf49d 7041800: URI.equals may incorrectly return true with escaped octets
chegar
parents: 9035
diff changeset
  1856
                    return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
                if (toLower(s.charAt(i)) != toLower(t.charAt(i)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
                if (toLower(s.charAt(i)) != toLower(t.charAt(i)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
    // US-ASCII only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
    private static boolean equalIgnoringCase(String s, String t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
        if (s == t) return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
        if ((s != null) && (t != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
            int n = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
            if (t.length() != n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
                if (toLower(s.charAt(i)) != toLower(t.charAt(i)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
    private static int hash(int hash, String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
        if (s == null) return hash;
15272
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1888
        return s.indexOf('%') < 0 ? hash * 127 + s.hashCode()
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1889
                                  : normalizedHash(hash, s);
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1890
    }
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1891
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1892
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1893
    private static int normalizedHash(int hash, String s) {
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1894
        int h = 0;
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1895
        for (int index = 0; index < s.length(); index++) {
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1896
            char ch = s.charAt(index);
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1897
            h = 31 * h + ch;
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1898
            if (ch == '%') {
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1899
                /*
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1900
                 * Process the next two encoded characters
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1901
                 */
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1902
                for (int i = index + 1; i < index + 3; i++)
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1903
                    h = 31 * h + toUpper(s.charAt(i));
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1904
                index += 2;
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1905
            }
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1906
        }
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1907
        return hash * 127 + h;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
    // US-ASCII only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
    private static int hashIgnoringCase(int hash, String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
        if (s == null) return hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
        int h = hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
        int n = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
        for (int i = 0; i < n; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
            h = 31 * h + toLower(s.charAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
        return h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
    private static int compare(String s, String t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
        if (s == t) return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
        if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
            if (t != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
                return s.compareTo(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
                return +1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
    // US-ASCII only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
    private static int compareIgnoringCase(String s, String t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
        if (s == t) return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
        if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
            if (t != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
                int sn = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
                int tn = t.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
                int n = sn < tn ? sn : tn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
                for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
                    int c = toLower(s.charAt(i)) - toLower(t.charAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
                    if (c != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
                        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
                return sn - tn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
            return +1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
    // -- String construction --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
    // If a scheme is given then the path, if given, must be absolute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
    private static void checkPath(String s, String scheme, String path)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
        throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
        if (scheme != null) {
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52944
diff changeset
  1962
            if (path != null && !path.isEmpty() && path.charAt(0) != '/')
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52944
diff changeset
  1963
                throw new URISyntaxException(s, "Relative path in absolute URI");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
34783
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  1967
    private void appendAuthority(StringBuilder sb,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
                                 String authority,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
                                 String userInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
                                 String host,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
                                 int port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
        if (host != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
            sb.append("//");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
            if (userInfo != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
                sb.append(quote(userInfo, L_USERINFO, H_USERINFO));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
                sb.append('@');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
            boolean needBrackets = ((host.indexOf(':') >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
                                    && !host.startsWith("[")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
                                    && !host.endsWith("]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
            if (needBrackets) sb.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
            sb.append(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
            if (needBrackets) sb.append(']');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
            if (port != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
                sb.append(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
                sb.append(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
        } else if (authority != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
            sb.append("//");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
            if (authority.startsWith("[")) {
8778
f2ef7d12277f 7023363: URI("ftp", "[www.abc.com]", "/dir1/dir2", "query", "frag") should throw URISyntaxException
chegar
parents: 8562
diff changeset
  1992
                // authority should (but may not) contain an embedded IPv6 address
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 23725
diff changeset
  1993
                int end = authority.indexOf(']');
8778
f2ef7d12277f 7023363: URI("ftp", "[www.abc.com]", "/dir1/dir2", "query", "frag") should throw URISyntaxException
chegar
parents: 8562
diff changeset
  1994
                String doquote = authority, dontquote = "";
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 23725
diff changeset
  1995
                if (end != -1 && authority.indexOf(':') != -1) {
8778
f2ef7d12277f 7023363: URI("ftp", "[www.abc.com]", "/dir1/dir2", "query", "frag") should throw URISyntaxException
chegar
parents: 8562
diff changeset
  1996
                    // the authority contains an IPv6 address
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
                    if (end == authority.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
                        dontquote = authority;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
                        doquote = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
                    } else {
8778
f2ef7d12277f 7023363: URI("ftp", "[www.abc.com]", "/dir1/dir2", "query", "frag") should throw URISyntaxException
chegar
parents: 8562
diff changeset
  2001
                        dontquote = authority.substring(0 , end + 1);
f2ef7d12277f 7023363: URI("ftp", "[www.abc.com]", "/dir1/dir2", "query", "frag") should throw URISyntaxException
chegar
parents: 8562
diff changeset
  2002
                        doquote = authority.substring(end + 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
                    }
8778
f2ef7d12277f 7023363: URI("ftp", "[www.abc.com]", "/dir1/dir2", "query", "frag") should throw URISyntaxException
chegar
parents: 8562
diff changeset
  2004
                }
f2ef7d12277f 7023363: URI("ftp", "[www.abc.com]", "/dir1/dir2", "query", "frag") should throw URISyntaxException
chegar
parents: 8562
diff changeset
  2005
                sb.append(dontquote);
f2ef7d12277f 7023363: URI("ftp", "[www.abc.com]", "/dir1/dir2", "query", "frag") should throw URISyntaxException
chegar
parents: 8562
diff changeset
  2006
                sb.append(quote(doquote,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
                            L_REG_NAME | L_SERVER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
                            H_REG_NAME | H_SERVER));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
                sb.append(quote(authority,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
                            L_REG_NAME | L_SERVER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
                            H_REG_NAME | H_SERVER));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
34783
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  2017
    private void appendSchemeSpecificPart(StringBuilder sb,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
                                          String opaquePart,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
                                          String authority,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
                                          String userInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
                                          String host,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
                                          int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
                                          String path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
                                          String query)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
        if (opaquePart != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
            /* check if SSP begins with an IPv6 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
             * because we must not quote a literal IPv6 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
            if (opaquePart.startsWith("//[")) {
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 23725
diff changeset
  2031
                int end =  opaquePart.indexOf(']');
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 23725
diff changeset
  2032
                if (end != -1 && opaquePart.indexOf(':')!=-1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
                    String doquote, dontquote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
                    if (end == opaquePart.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
                        dontquote = opaquePart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
                        doquote = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
                        dontquote = opaquePart.substring(0,end+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
                        doquote = opaquePart.substring(end+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
                    sb.append (dontquote);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
                    sb.append(quote(doquote, L_URIC, H_URIC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
                sb.append(quote(opaquePart, L_URIC, H_URIC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
            appendAuthority(sb, authority, userInfo, host, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
            if (path != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
                sb.append(quote(path, L_PATH, H_PATH));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
            if (query != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
                sb.append('?');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
                sb.append(quote(query, L_URIC, H_URIC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
34783
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  2058
    private void appendFragment(StringBuilder sb, String fragment) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
        if (fragment != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
            sb.append('#');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
            sb.append(quote(fragment, L_URIC, H_URIC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
    private String toString(String scheme,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
                            String opaquePart,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
                            String authority,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
                            String userInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
                            String host,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
                            int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
                            String path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
                            String query,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
                            String fragment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
    {
34783
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  2075
        StringBuilder sb = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
        if (scheme != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
            sb.append(scheme);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
            sb.append(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
        appendSchemeSpecificPart(sb, opaquePart,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
                                 authority, userInfo, host, port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
                                 path, query);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
        appendFragment(sb, fragment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
    // -- Normalization, resolution, and relativization --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
    // RFC2396 5.2 (6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
    private static String resolvePath(String base, String child,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
                                      boolean absolute)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
        int i = base.lastIndexOf('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
        int cn = child.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
        String path = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
        if (cn == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
            // 5.2 (6a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
            if (i >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
                path = base.substring(0, i + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
        } else {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 24685
diff changeset
  2102
            StringBuilder sb = new StringBuilder(base.length() + cn);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
            // 5.2 (6a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
            if (i >= 0)
31471
ae27c6f1d8bf 8077242: (str) Optimize AbstractStringBuilder.append(CharSequence, int, int) for String argument
igerasim
parents: 28567
diff changeset
  2105
                sb.append(base, 0, i + 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
            // 5.2 (6b)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
            sb.append(child);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
            path = sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
        // 5.2 (6c-f)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
        String np = normalize(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
        // 5.2 (6g): If the result is absolute but the path begins with "../",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
        // then we simply leave the path as-is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
        return np;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
    // RFC2396 5.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
    private static URI resolve(URI base, URI child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
        // check if child if opaque first so that NPE is thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
        // if child is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
        if (child.isOpaque() || base.isOpaque())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
            return child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
        // 5.2 (2): Reference to current document (lone fragment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
        if ((child.scheme == null) && (child.authority == null)
34887
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  2129
            && child.path.isEmpty() && (child.fragment != null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
            && (child.query == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
            if ((base.fragment != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
                && child.fragment.equals(base.fragment)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
                return base;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
            URI ru = new URI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
            ru.scheme = base.scheme;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
            ru.authority = base.authority;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
            ru.userInfo = base.userInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
            ru.host = base.host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
            ru.port = base.port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
            ru.path = base.path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
            ru.fragment = child.fragment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
            ru.query = base.query;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
            return ru;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
        // 5.2 (3): Child is absolute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
        if (child.scheme != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
            return child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
        URI ru = new URI();             // Resolved URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
        ru.scheme = base.scheme;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
        ru.query = child.query;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
        ru.fragment = child.fragment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
        // 5.2 (4): Authority
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
        if (child.authority == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
            ru.authority = base.authority;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
            ru.host = base.host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
            ru.userInfo = base.userInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
            ru.port = base.port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
            String cp = (child.path == null) ? "" : child.path;
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52944
diff changeset
  2164
            if (!cp.isEmpty() && cp.charAt(0) == '/') {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
                // 5.2 (5): Child path is absolute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
                ru.path = child.path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
                // 5.2 (6): Resolve relative path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
                ru.path = resolvePath(base.path, cp, base.isAbsolute());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
            ru.authority = child.authority;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
            ru.host = child.host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
            ru.userInfo = child.userInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
            ru.host = child.host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
            ru.port = child.port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
            ru.path = child.path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
        // 5.2 (7): Recombine (nothing to do here)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
        return ru;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
    // If the given URI's path is normal then return the URI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
    // o.w., return a new URI containing the normalized path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
    private static URI normalize(URI u) {
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52944
diff changeset
  2188
        if (u.isOpaque() || u.path == null || u.path.isEmpty())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
            return u;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
        String np = normalize(u.path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
        if (np == u.path)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
            return u;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
        URI v = new URI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
        v.scheme = u.scheme;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
        v.fragment = u.fragment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
        v.authority = u.authority;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
        v.userInfo = u.userInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
        v.host = u.host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
        v.port = u.port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
        v.path = np;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
        v.query = u.query;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
        return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
    // If both URIs are hierarchical, their scheme and authority components are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
    // identical, and the base path is a prefix of the child's path, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
    // return a relative URI that, when resolved against the base, yields the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
    // child; otherwise, return the child.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
    private static URI relativize(URI base, URI child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
        // check if child if opaque first so that NPE is thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
        // if child is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
        if (child.isOpaque() || base.isOpaque())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
            return child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
        if (!equalIgnoringCase(base.scheme, child.scheme)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
            || !equal(base.authority, child.authority))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
            return child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
        String bp = normalize(base.path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
        String cp = normalize(child.path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
        if (!bp.equals(cp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
            if (!bp.endsWith("/"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
                bp = bp + "/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
            if (!cp.startsWith(bp))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
                return child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
        URI v = new URI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
        v.path = cp.substring(bp.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
        v.query = child.query;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
        v.fragment = child.fragment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
        return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
    // -- Path normalization --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
    // The following algorithm for path normalization avoids the creation of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
    // string object for each segment, as well as the use of a string buffer to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
    // compute the final result, by using a single char array and editing it in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
    // place.  The array is first split into segments, replacing each slash
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
    // with '\0' and creating a segment-index array, each element of which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
    // the index of the first char in the corresponding segment.  We then walk
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
    // through both arrays, removing ".", "..", and other segments as necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
    // by setting their entries in the index array to -1.  Finally, the two
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
    // arrays are used to rejoin the segments and compute the final result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
    // This code is based upon src/solaris/native/java/io/canonicalize_md.c
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
    // Check the given path to see if it might need normalization.  A path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
    // might need normalization if it contains duplicate slashes, a "."
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
    // segment, or a ".." segment.  Return -1 if no further normalization is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
    // possible, otherwise return the number of segments found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
    // This method takes a string argument rather than a char array so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
    // this test can be performed without invoking path.toCharArray().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
    //
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31471
diff changeset
  2262
    private static int needsNormalization(String path) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
        boolean normal = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
        int ns = 0;                     // Number of segments
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
        int end = path.length() - 1;    // Index of last char in path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
        int p = 0;                      // Index of next char in path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
        // Skip initial slashes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
        while (p <= end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
            if (path.charAt(p) != '/') break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
            p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
        if (p > 1) normal = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
        // Scan segments
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
        while (p <= end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
            // Looking at "." or ".." ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
            if ((path.charAt(p) == '.')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
                && ((p == end)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
                    || ((path.charAt(p + 1) == '/')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
                        || ((path.charAt(p + 1) == '.')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
                            && ((p + 1 == end)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
                                || (path.charAt(p + 2) == '/')))))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
                normal = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
            ns++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
            // Find beginning of next segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
            while (p <= end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
                if (path.charAt(p++) != '/')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
                // Skip redundant slashes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
                while (p <= end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
                    if (path.charAt(p) != '/') break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
                    normal = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
                    p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
        return normal ? -1 : ns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
    // Split the given path into segments, replacing slashes with nulls and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
    // filling in the given segment-index array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
    // Preconditions:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
    //   segs.length == Number of segments in path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
    // Postconditions:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
    //   All slashes in path replaced by '\0'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
    //   segs[i] == Index of first char in segment i (0 <= i < segs.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
    //
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31471
diff changeset
  2319
    private static void split(char[] path, int[] segs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
        int end = path.length - 1;      // Index of last char in path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
        int p = 0;                      // Index of next char in path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
        int i = 0;                      // Index of current segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
        // Skip initial slashes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
        while (p <= end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
            if (path[p] != '/') break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
            path[p] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
            p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
        while (p <= end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
            // Note start of segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
            segs[i++] = p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
            // Find beginning of next segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
            while (p <= end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
                if (path[p++] != '/')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
                path[p - 1] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
                // Skip redundant slashes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
                while (p <= end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
                    if (path[p] != '/') break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
                    path[p++] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
        if (i != segs.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
            throw new InternalError();  // ASSERT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
    // Join the segments in the given path according to the given segment-index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
    // array, ignoring those segments whose index entries have been set to -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
    // and inserting slashes as needed.  Return the length of the resulting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
    // path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
    // Preconditions:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
    //   segs[i] == -1 implies segment i is to be ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
    //   path computed by split, as above, with '\0' having replaced '/'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
    // Postconditions:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
    //   path[0] .. path[return value] == Resulting path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
    //
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31471
diff changeset
  2368
    private static int join(char[] path, int[] segs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
        int ns = segs.length;           // Number of segments
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
        int end = path.length - 1;      // Index of last char in path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
        int p = 0;                      // Index of next path char to write
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
        if (path[p] == '\0') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
            // Restore initial slash for absolute paths
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
            path[p++] = '/';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
        for (int i = 0; i < ns; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
            int q = segs[i];            // Current segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
            if (q == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
                // Ignore this segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
            if (p == q) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
                // We're already at this segment, so just skip to its end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
                while ((p <= end) && (path[p] != '\0'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
                    p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
                if (p <= end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
                    // Preserve trailing slash
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
                    path[p++] = '/';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
            } else if (p < q) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
                // Copy q down to p
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
                while ((q <= end) && (path[q] != '\0'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
                    path[p++] = path[q++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
                if (q <= end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
                    // Preserve trailing slash
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
                    path[p++] = '/';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
                throw new InternalError(); // ASSERT false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
        return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
    // Remove "." segments from the given path, and remove segment pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
    // consisting of a non-".." segment followed by a ".." segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
    private static void removeDots(char[] path, int[] segs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
        int ns = segs.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
        int end = path.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
        for (int i = 0; i < ns; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
            int dots = 0;               // Number of dots found (0, 1, or 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
            // Find next occurrence of "." or ".."
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
                int p = segs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
                if (path[p] == '.') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
                    if (p == end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
                        dots = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
                    } else if (path[p + 1] == '\0') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
                        dots = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
                    } else if ((path[p + 1] == '.')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
                               && ((p + 1 == end)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
                                   || (path[p + 2] == '\0'))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
                        dots = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
            } while (i < ns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
            if ((i > ns) || (dots == 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
            if (dots == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
                // Remove this occurrence of "."
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
                segs[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
                // If there is a preceding non-".." segment, remove both that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
                // segment and this occurrence of ".."; otherwise, leave this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
                // ".." segment as-is.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
                int j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
                for (j = i - 1; j >= 0; j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
                    if (segs[j] != -1) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
                if (j >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
                    int q = segs[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
                    if (!((path[q] == '.')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
                          && (path[q + 1] == '.')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
                          && (path[q + 2] == '\0'))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
                        segs[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
                        segs[j] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
    // DEVIATION: If the normalized path is relative, and if the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
    // segment could be parsed as a scheme name, then prepend a "." segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
    private static void maybeAddLeadingDot(char[] path, int[] segs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
        if (path[0] == '\0')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
            // The path is absolute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
        int ns = segs.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
        int f = 0;                      // Index of first segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
        while (f < ns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
            if (segs[f] >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
            f++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
        if ((f >= ns) || (f == 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
            // The path is empty, or else the original first segment survived,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
            // in which case we already know that no leading "." is needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
        int p = segs[f];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
        while ((p < path.length) && (path[p] != ':') && (path[p] != '\0')) p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
        if (p >= path.length || path[p] == '\0')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
            // No colon in first segment, so no "." needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
        // At this point we know that the first segment is unused,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
        // hence we can insert a "." segment at that position
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
        path[0] = '.';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
        path[1] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
        segs[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
    // Normalize the given path string.  A normal path string has no empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
    // segments (i.e., occurrences of "//"), no segments equal to ".", and no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
    // segments equal to ".." that are preceded by a segment not equal to "..".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
    // In contrast to Unix-style pathname normalization, for URI paths we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
    // always retain trailing slashes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
    private static String normalize(String ps) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
        // Does this path need normalization?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
        int ns = needsNormalization(ps);        // Number of segments
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
        if (ns < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
            // Nope -- just return it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
            return ps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
        char[] path = ps.toCharArray();         // Path in char-array form
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
        // Split path into segments
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
        int[] segs = new int[ns];               // Segment-index array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
        split(path, segs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
        // Remove dots
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
        removeDots(path, segs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
        // Prevent scheme-name confusion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
        maybeAddLeadingDot(path, segs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
        // Join the remaining segments and return the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
        String s = new String(path, 0, join(path, segs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
        if (s.equals(ps)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
            // string was already normalized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
            return ps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
    // -- Character classes for parsing --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
    // RFC2396 precisely specifies which characters in the US-ASCII charset are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
    // permissible in the various components of a URI reference.  We here
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
    // define a set of mask pairs to aid in enforcing these restrictions.  Each
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
    // mask pair consists of two longs, a low mask and a high mask.  Taken
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
    // together they represent a 128-bit mask, where bit i is set iff the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
    // character with value i is permitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
    // This approach is more efficient than sequentially searching arrays of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
    // permitted characters.  It could be made still more efficient by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
    // precompiling the mask information so that a character's presence in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
    // given mask could be determined by a single table lookup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2551
    // To save startup time, we manually calculate the low-/highMask constants.
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2552
    // For reference, the following methods were used to calculate the values:
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2553
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
    // Compute the low-order mask for the characters in the given string
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2555
    //     private static long lowMask(String chars) {
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2556
    //        int n = chars.length();
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2557
    //        long m = 0;
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2558
    //        for (int i = 0; i < n; i++) {
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2559
    //            char c = chars.charAt(i);
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2560
    //            if (c < 64)
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2561
    //                m |= (1L << c);
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2562
    //        }
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2563
    //        return m;
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2564
    //    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
    // Compute the high-order mask for the characters in the given string
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2567
    //    private static long highMask(String chars) {
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2568
    //        int n = chars.length();
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2569
    //        long m = 0;
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2570
    //        for (int i = 0; i < n; i++) {
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2571
    //            char c = chars.charAt(i);
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2572
    //            if ((c >= 64) && (c < 128))
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2573
    //                m |= (1L << (c - 64));
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2574
    //        }
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2575
    //        return m;
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2576
    //    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
    // Compute a low-order mask for the characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
    // between first and last, inclusive
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2580
    //    private static long lowMask(char first, char last) {
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2581
    //        long m = 0;
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2582
    //        int f = Math.max(Math.min(first, 63), 0);
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2583
    //        int l = Math.max(Math.min(last, 63), 0);
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2584
    //        for (int i = f; i <= l; i++)
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2585
    //            m |= 1L << i;
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2586
    //        return m;
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2587
    //    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
    // Compute a high-order mask for the characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
    // between first and last, inclusive
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2591
    //    private static long highMask(char first, char last) {
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2592
    //        long m = 0;
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2593
    //        int f = Math.max(Math.min(first, 127), 64) - 64;
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2594
    //        int l = Math.max(Math.min(last, 127), 64) - 64;
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2595
    //        for (int i = f; i <= l; i++)
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2596
    //            m |= 1L << i;
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2597
    //        return m;
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2598
    //    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
    // Tell whether the given character is permitted by the given mask pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
    private static boolean match(char c, long lowMask, long highMask) {
5612
c0d1673e1ca6 6773270: java.net.URI fails to escape \u0000
michaelm
parents: 715
diff changeset
  2602
        if (c == 0) // 0 doesn't have a slot in the mask. So, it never matches.
c0d1673e1ca6 6773270: java.net.URI fails to escape \u0000
michaelm
parents: 715
diff changeset
  2603
            return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
        if (c < 64)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
            return ((1L << c) & lowMask) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
        if (c < 128)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
            return ((1L << (c - 64)) & highMask) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
    // Character-class masks, in reverse order from RFC2396 because
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
    // initializers for static fields cannot make forward references.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
    // digit    = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
    //            "8" | "9"
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2616
    private static final long L_DIGIT = 0x3FF000000000000L; // lowMask('0', '9');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
    private static final long H_DIGIT = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
    // upalpha  = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
    //            "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
    //            "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
    private static final long L_UPALPHA = 0L;
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2623
    private static final long H_UPALPHA = 0x7FFFFFEL; // highMask('A', 'Z');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
    // lowalpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
    //            "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
    //            "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
    private static final long L_LOWALPHA = 0L;
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2629
    private static final long H_LOWALPHA = 0x7FFFFFE00000000L; // highMask('a', 'z');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
    // alpha         = lowalpha | upalpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
    private static final long L_ALPHA = L_LOWALPHA | L_UPALPHA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
    private static final long H_ALPHA = H_LOWALPHA | H_UPALPHA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
    // alphanum      = alpha | digit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
    private static final long L_ALPHANUM = L_DIGIT | L_ALPHA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
    private static final long H_ALPHANUM = H_DIGIT | H_ALPHA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
    // hex           = digit | "A" | "B" | "C" | "D" | "E" | "F" |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
    //                         "a" | "b" | "c" | "d" | "e" | "f"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
    private static final long L_HEX = L_DIGIT;
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2642
    private static final long H_HEX = 0x7E0000007EL; // highMask('A', 'F') | highMask('a', 'f');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
    // mark          = "-" | "_" | "." | "!" | "~" | "*" | "'" |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
    //                 "(" | ")"
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2646
    private static final long L_MARK = 0x678200000000L; // lowMask("-_.!~*'()");
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2647
    private static final long H_MARK = 0x4000000080000000L; // highMask("-_.!~*'()");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
    // unreserved    = alphanum | mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
    private static final long L_UNRESERVED = L_ALPHANUM | L_MARK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
    private static final long H_UNRESERVED = H_ALPHANUM | H_MARK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
    // reserved      = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
    //                 "$" | "," | "[" | "]"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
    // Added per RFC2732: "[", "]"
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2656
    private static final long L_RESERVED = 0xAC00985000000000L; // lowMask(";/?:@&=+$,[]");
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2657
    private static final long H_RESERVED = 0x28000001L; // highMask(";/?:@&=+$,[]");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
    // The zero'th bit is used to indicate that escape pairs and non-US-ASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
    // characters are allowed; this is handled by the scanEscape method below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
    private static final long L_ESCAPED = 1L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
    private static final long H_ESCAPED = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
    // uric          = reserved | unreserved | escaped
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
    private static final long L_URIC = L_RESERVED | L_UNRESERVED | L_ESCAPED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
    private static final long H_URIC = H_RESERVED | H_UNRESERVED | H_ESCAPED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
    // pchar         = unreserved | escaped |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
    //                 ":" | "@" | "&" | "=" | "+" | "$" | ","
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
    private static final long L_PCHAR
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2671
        = L_UNRESERVED | L_ESCAPED | 0x2400185000000000L; // lowMask(":@&=+$,");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
    private static final long H_PCHAR
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2673
        = H_UNRESERVED | H_ESCAPED | 0x1L; // highMask(":@&=+$,");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
    // All valid path characters
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2676
    private static final long L_PATH = L_PCHAR | 0x800800000000000L; // lowMask(";/");
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2677
    private static final long H_PATH = H_PCHAR; // highMask(";/") == 0x0L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
    // Dash, for use in domainlabel and toplabel
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2680
    private static final long L_DASH = 0x200000000000L; // lowMask("-");
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2681
    private static final long H_DASH = 0x0L; // highMask("-");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
    // Dot, for use in hostnames
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2684
    private static final long L_DOT = 0x400000000000L; // lowMask(".");
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2685
    private static final long H_DOT = 0x0L; // highMask(".");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
    // userinfo      = *( unreserved | escaped |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
    //                    ";" | ":" | "&" | "=" | "+" | "$" | "," )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
    private static final long L_USERINFO
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2690
        = L_UNRESERVED | L_ESCAPED | 0x2C00185000000000L; // lowMask(";:&=+$,");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
    private static final long H_USERINFO
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2692
        = H_UNRESERVED | H_ESCAPED; // | highMask(";:&=+$,") == 0L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
    // reg_name      = 1*( unreserved | escaped | "$" | "," |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
    //                     ";" | ":" | "@" | "&" | "=" | "+" )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
    private static final long L_REG_NAME
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2697
        = L_UNRESERVED | L_ESCAPED | 0x2C00185000000000L; // lowMask("$,;:@&=+");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
    private static final long H_REG_NAME
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2699
        = H_UNRESERVED | H_ESCAPED | 0x1L; // highMask("$,;:@&=+");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
    // All valid characters for server-based authorities
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
    private static final long L_SERVER
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2703
        = L_USERINFO | L_ALPHANUM | L_DASH | 0x400400000000000L; // lowMask(".:@[]");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
    private static final long H_SERVER
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2705
        = H_USERINFO | H_ALPHANUM | H_DASH | 0x28000001L; // highMask(".:@[]");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
    // Special case of server authority that represents an IPv6 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
    // In this case, a % does not signify an escape sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
    private static final long L_SERVER_PERCENT
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2710
        = L_SERVER | 0x2000000000L; // lowMask("%");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
    private static final long H_SERVER_PERCENT
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2712
        = H_SERVER; // | highMask("%") == 0L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
    // scheme        = alpha *( alpha | digit | "+" | "-" | "." )
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2715
    private static final long L_SCHEME = L_ALPHA | L_DIGIT | 0x680000000000L; // lowMask("+-.");
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2716
    private static final long H_SCHEME = H_ALPHA | H_DIGIT; // | highMask("+-.") == 0L
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
28567
4121cce98397 6933879: URISyntaxException when non-alphanumeric characters are present in scope_id
kshefov
parents: 26875
diff changeset
  2718
    // scope_id = alpha | digit | "_" | "."
4121cce98397 6933879: URISyntaxException when non-alphanumeric characters are present in scope_id
kshefov
parents: 26875
diff changeset
  2719
    private static final long L_SCOPE_ID
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2720
        = L_ALPHANUM | 0x400000000000L; // lowMask("_.");
28567
4121cce98397 6933879: URISyntaxException when non-alphanumeric characters are present in scope_id
kshefov
parents: 26875
diff changeset
  2721
    private static final long H_SCOPE_ID
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 46152
diff changeset
  2722
        = H_ALPHANUM | 0x80000000L; // highMask("_.");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
    // -- Escaping and encoding --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31471
diff changeset
  2726
    private static final char[] hexDigits = {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
        '0', '1', '2', '3', '4', '5', '6', '7',
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
        '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
34783
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  2731
    private static void appendEscape(StringBuilder sb, byte b) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
        sb.append('%');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
        sb.append(hexDigits[(b >> 4) & 0x0f]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
        sb.append(hexDigits[(b >> 0) & 0x0f]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
34783
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  2737
    private static void appendEncoded(StringBuilder sb, char c) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
        ByteBuffer bb = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
            bb = ThreadLocalCoders.encoderFor("UTF-8")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
                .encode(CharBuffer.wrap("" + c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
        } catch (CharacterCodingException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
            assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
        while (bb.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
            int b = bb.get() & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
            if (b >= 0x80)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
                appendEscape(sb, (byte)b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
                sb.append((char)b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
    // Quote any characters in s that are not permitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
    // by the given mask pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
    private static String quote(String s, long lowMask, long highMask) {
34783
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  2758
        StringBuilder sb = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
        boolean allowNonASCII = ((lowMask & L_ESCAPED) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
        for (int i = 0; i < s.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
            char c = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
            if (c < '\u0080') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
                if (!match(c, lowMask, highMask)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
                    if (sb == null) {
34783
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  2765
                        sb = new StringBuilder();
31471
ae27c6f1d8bf 8077242: (str) Optimize AbstractStringBuilder.append(CharSequence, int, int) for String argument
igerasim
parents: 28567
diff changeset
  2766
                        sb.append(s, 0, i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
                    appendEscape(sb, (byte)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
                    if (sb != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
                        sb.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
            } else if (allowNonASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
                       && (Character.isSpaceChar(c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
                           || Character.isISOControl(c))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2776
                if (sb == null) {
34783
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  2777
                    sb = new StringBuilder();
31471
ae27c6f1d8bf 8077242: (str) Optimize AbstractStringBuilder.append(CharSequence, int, int) for String argument
igerasim
parents: 28567
diff changeset
  2778
                    sb.append(s, 0, i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
                appendEncoded(sb, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
                if (sb != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
                    sb.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
        return (sb == null) ? s : sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
    // Encodes all characters >= \u0080 into escaped, normalized UTF-8 octets,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
    // assuming that s is otherwise legal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
    private static String encode(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
        int n = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
        if (n == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
            return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
        // First check whether we actually need to encode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
        for (int i = 0;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
            if (s.charAt(i) >= '\u0080')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
            if (++i >= n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
                return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
        String ns = Normalizer.normalize(s, Normalizer.Form.NFC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
        ByteBuffer bb = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
            bb = ThreadLocalCoders.encoderFor("UTF-8")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
                .encode(CharBuffer.wrap(ns));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
        } catch (CharacterCodingException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
            assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
34783
337afb24ec6c 8145862: Improve lazy initialization of fields in java.net.URI
redestad
parents: 34774
diff changeset
  2814
        StringBuilder sb = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
        while (bb.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
            int b = bb.get() & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
            if (b >= 0x80)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
                appendEscape(sb, (byte)b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
                sb.append((char)b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
    private static int decode(char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
        if ((c >= '0') && (c <= '9'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
            return c - '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
        if ((c >= 'a') && (c <= 'f'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
            return c - 'a' + 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
        if ((c >= 'A') && (c <= 'F'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
            return c - 'A' + 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
        assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
    private static byte decode(char c1, char c2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
        return (byte)(  ((decode(c1) & 0xf) << 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
                      | ((decode(c2) & 0xf) << 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
    // Evaluates all escapes in s, applying UTF-8 decoding if needed.  Assumes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
    // that escapes are well-formed syntactically, i.e., of the form %XX.  If a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
    // sequence of escaped octets is not valid UTF-8 then the erroneous octets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
    // are replaced with '\uFFFD'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
    // Exception: any "%" found between "[]" is left alone. It is an IPv6 literal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
    //            with a scope_id
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
    private static String decode(String s) {
23725
0190e5c0e70c 8037396: URI getQuery() and getFragment() don't decode properly
michaelm
parents: 21334
diff changeset
  2849
        return decode(s, true);
0190e5c0e70c 8037396: URI getQuery() and getFragment() don't decode properly
michaelm
parents: 21334
diff changeset
  2850
    }
0190e5c0e70c 8037396: URI getQuery() and getFragment() don't decode properly
michaelm
parents: 21334
diff changeset
  2851
0190e5c0e70c 8037396: URI getQuery() and getFragment() don't decode properly
michaelm
parents: 21334
diff changeset
  2852
    // This method was introduced as a generalization of URI.decode method
0190e5c0e70c 8037396: URI getQuery() and getFragment() don't decode properly
michaelm
parents: 21334
diff changeset
  2853
    // to provide a fix for JDK-8037396
0190e5c0e70c 8037396: URI getQuery() and getFragment() don't decode properly
michaelm
parents: 21334
diff changeset
  2854
    private static String decode(String s, boolean ignorePercentInBrackets) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
        if (s == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
            return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
        int n = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
        if (n == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
            return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
        if (s.indexOf('%') < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
            return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 24685
diff changeset
  2863
        StringBuilder sb = new StringBuilder(n);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
        ByteBuffer bb = ByteBuffer.allocate(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
        CharBuffer cb = CharBuffer.allocate(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
        CharsetDecoder dec = ThreadLocalCoders.decoderFor("UTF-8")
23725
0190e5c0e70c 8037396: URI getQuery() and getFragment() don't decode properly
michaelm
parents: 21334
diff changeset
  2867
                .onMalformedInput(CodingErrorAction.REPLACE)
0190e5c0e70c 8037396: URI getQuery() and getFragment() don't decode properly
michaelm
parents: 21334
diff changeset
  2868
                .onUnmappableCharacter(CodingErrorAction.REPLACE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
        // This is not horribly efficient, but it will do for now
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
        char c = s.charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
        boolean betweenBrackets = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
        for (int i = 0; i < n;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
            assert c == s.charAt(i);    // Loop invariant
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
            if (c == '[') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
                betweenBrackets = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
            } else if (betweenBrackets && c == ']') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
                betweenBrackets = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
            }
23725
0190e5c0e70c 8037396: URI getQuery() and getFragment() don't decode properly
michaelm
parents: 21334
diff changeset
  2881
            if (c != '%' || (betweenBrackets && ignorePercentInBrackets)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
                sb.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
                if (++i >= n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
                c = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
            bb.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
            int ui = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
                assert (n - i >= 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
                bb.put(decode(s.charAt(++i), s.charAt(++i)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
                if (++i >= n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2894
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2895
                c = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
                if (c != '%')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2897
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
            bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
            cb.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
            dec.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
            CoderResult cr = dec.decode(bb, cb, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
            assert cr.isUnderflow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
            cr = dec.flush(cb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
            assert cr.isUnderflow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
            sb.append(cb.flip().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
    // -- Parsing --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
    // For convenience we wrap the input URI string in a new instance of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
    // following internal class.  This saves always having to pass the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
    // string as an argument to each internal scan/parse method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
    private class Parser {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
        private String input;           // URI input string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2922
        private boolean requireServerAuthority = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
        Parser(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
            input = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
            string = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
        // -- Methods for throwing URISyntaxException in various ways --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
        private void fail(String reason) throws URISyntaxException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
            throw new URISyntaxException(input, reason);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
        private void fail(String reason, int p) throws URISyntaxException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
            throw new URISyntaxException(input, reason, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
        private void failExpecting(String expected, int p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
            throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
            fail("Expected " + expected, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
        // -- Simple access to the input string --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
        // Tells whether start < end and, if so, whether charAt(start) == c
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
        private boolean at(int start, int end, char c) {
34887
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  2951
            return (start < end) && (input.charAt(start) == c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
        // Tells whether start + s.length() < end and, if so,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
        // whether the chars at the start position match s exactly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
        private boolean at(int start, int end, String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
            int p = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2959
            int sn = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
            if (sn > end - p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
            while (i < sn) {
34887
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  2964
                if (input.charAt(p++) != s.charAt(i)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
            return (i == sn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
        // -- Scanning --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
        // The various scan and parse methods that follow use a uniform
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
        // convention of taking the current start position and end index as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
        // their first two arguments.  The start is inclusive while the end is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2978
        // exclusive, just as in the String class, i.e., a start/end pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
        // denotes the left-open interval [start, end) of the input string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2980
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
        // These methods never proceed past the end position.  They may return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
        // -1 to indicate outright failure, but more often they simply return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
        // the position of the first char after the last char scanned.  Thus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2984
        // a typical idiom is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2985
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2986
        //     int p = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
        //     int q = scan(p, end, ...);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
        //     if (q > p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
        //         // We scanned something
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2990
        //         ...;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2991
        //     else if (q == p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2992
        //         // We scanned nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2993
        //         ...;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2994
        //     else if (q == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
        //         // Something went wrong
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
        //         ...;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2997
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
        // Scan a specific char: If the char at the given start position is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3000
        // equal to c, return the index of the next char; otherwise, return the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3001
        // start position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
        private int scan(int start, int end, char c) {
34887
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3004
            if ((start < end) && (input.charAt(start) == c))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3005
                return start + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
            return start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
        // Scan forward from the given start position.  Stop at the first char
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
        // in the err string (in which case -1 is returned), or the first char
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
        // in the stop string (in which case the index of the preceding char is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
        // returned), or the end of the input string (in which case the length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
        // of the input string is returned).  May return the start position if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
        // nothing matches.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
        private int scan(int start, int end, String err, String stop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
            int p = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
            while (p < end) {
34887
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3019
                char c = input.charAt(p);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
                if (err.indexOf(c) >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
                if (stop.indexOf(c) >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
                p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3025
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3026
            return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3027
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3028
34887
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3029
        // Scan forward from the given start position.  Stop at the first char
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3030
        // in the stop string (in which case the index of the preceding char is
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3031
        // returned), or the end of the input string (in which case the length
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3032
        // of the input string is returned).  May return the start position if
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3033
        // nothing matches.
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3034
        //
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3035
        private int scan(int start, int end, String stop) {
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3036
            int p = start;
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3037
            while (p < end) {
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3038
                char c = input.charAt(p);
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3039
                if (stop.indexOf(c) >= 0)
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3040
                    break;
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3041
                p++;
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3042
            }
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3043
            return p;
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3044
        }
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3045
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3046
        // Scan a potential escape sequence, starting at the given position,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3047
        // with the given first char (i.e., charAt(start) == c).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3048
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3049
        // This method assumes that if escapes are allowed then visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3050
        // non-US-ASCII chars are also allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3051
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3052
        private int scanEscape(int start, int n, char first)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3053
            throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3054
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3055
            int p = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3056
            char c = first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3057
            if (c == '%') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3058
                // Process escape pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3059
                if ((p + 3 <= n)
34887
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3060
                    && match(input.charAt(p + 1), L_HEX, H_HEX)
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3061
                    && match(input.charAt(p + 2), L_HEX, H_HEX)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3062
                    return p + 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3063
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3064
                fail("Malformed escape pair", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3065
            } else if ((c > 128)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3066
                       && !Character.isSpaceChar(c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3067
                       && !Character.isISOControl(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3068
                // Allow unescaped but visible non-US-ASCII chars
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3069
                return p + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3070
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3071
            return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3072
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3073
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3074
        // Scan chars that match the given mask pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3075
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
        private int scan(int start, int n, long lowMask, long highMask)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
            throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3078
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3079
            int p = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3080
            while (p < n) {
34887
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3081
                char c = input.charAt(p);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
                if (match(c, lowMask, highMask)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3083
                    p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3084
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3085
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3086
                if ((lowMask & L_ESCAPED) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3087
                    int q = scanEscape(p, n, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3088
                    if (q > p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3089
                        p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3090
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3091
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3093
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3094
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3095
            return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
        // Check that each of the chars in [start, end) matches the given mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
        private void checkChars(int start, int end,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3101
                                long lowMask, long highMask,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
                                String what)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
            throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
            int p = scan(start, end, lowMask, highMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3106
            if (p < end)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3107
                fail("Illegal character in " + what, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3110
        // Check that the char at position p matches the given mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3111
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3112
        private void checkChar(int p,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3113
                               long lowMask, long highMask,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3114
                               String what)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3115
            throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3116
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3117
            checkChars(p, p + 1, lowMask, highMask, what);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3120
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3121
        // -- Parsing --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3123
        // [<scheme>:]<scheme-specific-part>[#<fragment>]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3124
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3125
        void parse(boolean rsa) throws URISyntaxException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3126
            requireServerAuthority = rsa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3127
            int n = input.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3128
            int p = scan(0, n, "/?#", ":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3129
            if ((p >= 0) && at(p, n, ':')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3130
                if (p == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3131
                    failExpecting("scheme name", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3132
                checkChar(0, L_ALPHA, H_ALPHA, "scheme name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3133
                checkChars(1, p, L_SCHEME, H_SCHEME, "scheme name");
34887
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3134
                scheme = input.substring(0, p);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3135
                p++;                    // Skip ':'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3136
                if (at(p, n, '/')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3137
                    p = parseHierarchical(p, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3138
                } else {
34939
4fd867517aec 8146686: Create the schemeSpecificPart for non-opaque URIs lazily
redestad
parents: 34887
diff changeset
  3139
                    // opaque; need to create the schemeSpecificPart
34887
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3140
                    int q = scan(p, n, "#");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3141
                    if (q <= p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3142
                        failExpecting("scheme-specific part", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3143
                    checkChars(p, q, L_URIC, H_URIC, "opaque part");
34939
4fd867517aec 8146686: Create the schemeSpecificPart for non-opaque URIs lazily
redestad
parents: 34887
diff changeset
  3144
                    schemeSpecificPart = input.substring(p, q);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3145
                    p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3146
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3147
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3148
                p = parseHierarchical(0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3150
            if (at(p, n, '#')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3151
                checkChars(p + 1, n, L_URIC, H_URIC, "fragment");
34887
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3152
                fragment = input.substring(p + 1, n);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3153
                p = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3155
            if (p < n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3156
                fail("end of URI", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3158
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3159
        // [//authority]<path>[?<query>]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3160
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3161
        // DEVIATION from RFC2396: We allow an empty authority component as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3162
        // long as it's followed by a non-empty path, query component, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3163
        // fragment component.  This is so that URIs such as "file:///foo/bar"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3164
        // will parse.  This seems to be the intent of RFC2396, though the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3165
        // grammar does not permit it.  If the authority is empty then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3166
        // userInfo, host, and port components are undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3167
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3168
        // DEVIATION from RFC2396: We allow empty relative paths.  This seems
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3169
        // to be the intent of RFC2396, but the grammar does not permit it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3170
        // The primary consequence of this deviation is that "#f" parses as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3171
        // relative URI with an empty path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3172
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3173
        private int parseHierarchical(int start, int n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3174
            throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3175
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3176
            int p = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3177
            if (at(p, n, '/') && at(p + 1, n, '/')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3178
                p += 2;
34887
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3179
                int q = scan(p, n, "/?#");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3180
                if (q > p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3181
                    p = parseAuthority(p, q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3182
                } else if (q < n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3183
                    // DEVIATION: Allow empty authority prior to non-empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3184
                    // path, query component or fragment identifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3185
                } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3186
                    failExpecting("authority", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3187
            }
34887
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3188
            int q = scan(p, n, "?#"); // DEVIATION: May be empty
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3189
            checkChars(p, q, L_PATH, H_PATH, "path");
34887
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3190
            path = input.substring(p, q);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3191
            p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3192
            if (at(p, n, '?')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3193
                p++;
34887
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3194
                q = scan(p, n, "#");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3195
                checkChars(p, q, L_URIC, H_URIC, "query");
34887
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3196
                query = input.substring(p, q);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3197
                p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3199
            return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3201
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3202
        // authority     = server | reg_name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3203
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3204
        // Ambiguity: An authority that is a registry name rather than a server
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3205
        // might have a prefix that parses as a server.  We use the fact that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3206
        // the authority component is always followed by '/' or the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3207
        // input string to resolve this: If the complete authority did not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3208
        // parse as a server then we try to parse it as a registry name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3209
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3210
        private int parseAuthority(int start, int n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3211
            throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3212
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3213
            int p = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3214
            int q = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3215
            URISyntaxException ex = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3217
            boolean serverChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3218
            boolean regChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3219
34887
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3220
            if (scan(p, n, "]") > p) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3221
                // contains a literal IPv6 address, therefore % is allowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3222
                serverChars = (scan(p, n, L_SERVER_PERCENT, H_SERVER_PERCENT) == n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3223
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3224
                serverChars = (scan(p, n, L_SERVER, H_SERVER) == n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3225
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3226
            regChars = (scan(p, n, L_REG_NAME, H_REG_NAME) == n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3227
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3228
            if (regChars && !serverChars) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3229
                // Must be a registry-based authority
34887
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3230
                authority = input.substring(p, n);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3231
                return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3232
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3233
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3234
            if (serverChars) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3235
                // Might be (probably is) a server-based authority, so attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3236
                // to parse it as such.  If the attempt fails, try to treat it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3237
                // as a registry-based authority.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3238
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3239
                    q = parseServer(p, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3240
                    if (q < n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3241
                        failExpecting("end of authority", q);
34887
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3242
                    authority = input.substring(p, n);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3243
                } catch (URISyntaxException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3244
                    // Undo results of failed parse
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3245
                    userInfo = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3246
                    host = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3247
                    port = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3248
                    if (requireServerAuthority) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3249
                        // If we're insisting upon a server-based authority,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3250
                        // then just re-throw the exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3251
                        throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3252
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3253
                        // Save the exception in case it doesn't parse as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3254
                        // registry either
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3255
                        ex = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3256
                        q = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3257
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3258
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3260
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3261
            if (q < n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3262
                if (regChars) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3263
                    // Registry-based authority
34887
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3264
                    authority = input.substring(p, n);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3265
                } else if (ex != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3266
                    // Re-throw exception; it was probably due to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3267
                    // a malformed IPv6 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3268
                    throw ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3269
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3270
                    fail("Illegal character in authority", q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3271
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3272
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3273
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3274
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3276
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3277
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3278
        // [<userinfo>@]<host>[:<port>]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3279
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3280
        private int parseServer(int start, int n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3281
            throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3282
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3283
            int p = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3284
            int q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3285
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3286
            // userinfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3287
            q = scan(p, n, "/?#", "@");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3288
            if ((q >= p) && at(q, n, '@')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3289
                checkChars(p, q, L_USERINFO, H_USERINFO, "user info");
34887
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3290
                userInfo = input.substring(p, q);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3291
                p = q + 1;              // Skip '@'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3292
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3293
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3294
            // hostname, IPv4 address, or IPv6 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3295
            if (at(p, n, '[')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3296
                // DEVIATION from RFC2396: Support IPv6 addresses, per RFC2732
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3297
                p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3298
                q = scan(p, n, "/?#", "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3299
                if ((q > p) && at(q, n, ']')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3300
                    // look for a "%" scope id
34887
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3301
                    int r = scan (p, q, "%");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3302
                    if (r > p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3303
                        parseIPv6Reference(p, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3304
                        if (r+1 == q) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3305
                            fail ("scope id expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3306
                        }
28567
4121cce98397 6933879: URISyntaxException when non-alphanumeric characters are present in scope_id
kshefov
parents: 26875
diff changeset
  3307
                        checkChars (r+1, q, L_SCOPE_ID, H_SCOPE_ID,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3308
                                                "scope id");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3309
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3310
                        parseIPv6Reference(p, q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3311
                    }
34887
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3312
                    host = input.substring(p-1, q+1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3313
                    p = q + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3314
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3315
                    failExpecting("closing bracket for IPv6 address", q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3316
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3317
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3318
                q = parseIPv4Address(p, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3319
                if (q <= p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3320
                    q = parseHostname(p, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3321
                p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3322
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3323
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3324
            // port
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3325
            if (at(p, n, ':')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3326
                p++;
34887
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3327
                q = scan(p, n, "/");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3328
                if (q > p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3329
                    checkChars(p, q, L_DIGIT, H_DIGIT, "port number");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3330
                    try {
26720
6b160d97c51d 8055032: Improve numerical parsing in java.net and sun.net
redestad
parents: 25859
diff changeset
  3331
                        port = Integer.parseInt(input, p, q, 10);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3332
                    } catch (NumberFormatException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3333
                        fail("Malformed port number", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3334
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3335
                    p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3336
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3338
            if (p < n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3339
                failExpecting("port number", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3340
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3341
            return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3343
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3344
        // Scan a string of decimal digits whose value fits in a byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3345
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3346
        private int scanByte(int start, int n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3347
            throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3348
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3349
            int p = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3350
            int q = scan(p, n, L_DIGIT, H_DIGIT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3351
            if (q <= p) return q;
26720
6b160d97c51d 8055032: Improve numerical parsing in java.net and sun.net
redestad
parents: 25859
diff changeset
  3352
            if (Integer.parseInt(input, p, q, 10) > 255) return p;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3353
            return q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3355
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3356
        // Scan an IPv4 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3357
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3358
        // If the strict argument is true then we require that the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3359
        // interval contain nothing besides an IPv4 address; if it is false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3360
        // then we only require that it start with an IPv4 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3361
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3362
        // If the interval does not contain or start with (depending upon the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3363
        // strict argument) a legal IPv4 address characters then we return -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3364
        // immediately; otherwise we insist that these characters parse as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3365
        // legal IPv4 address and throw an exception on failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3366
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3367
        // We assume that any string of decimal digits and dots must be an IPv4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3368
        // address.  It won't parse as a hostname anyway, so making that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3369
        // assumption here allows more meaningful exceptions to be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3370
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3371
        private int scanIPv4Address(int start, int n, boolean strict)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3372
            throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3373
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3374
            int p = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3375
            int q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3376
            int m = scan(p, n, L_DIGIT | L_DOT, H_DIGIT | H_DOT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3377
            if ((m <= p) || (strict && (m != n)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3378
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3379
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3380
                // Per RFC2732: At most three digits per byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3381
                // Further constraint: Each element fits in a byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3382
                if ((q = scanByte(p, m)) <= p) break;   p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3383
                if ((q = scan(p, m, '.')) <= p) break;  p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3384
                if ((q = scanByte(p, m)) <= p) break;   p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3385
                if ((q = scan(p, m, '.')) <= p) break;  p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3386
                if ((q = scanByte(p, m)) <= p) break;   p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3387
                if ((q = scan(p, m, '.')) <= p) break;  p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3388
                if ((q = scanByte(p, m)) <= p) break;   p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3389
                if (q < m) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3390
                return q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3391
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3392
            fail("Malformed IPv4 address", q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3393
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3395
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3396
        // Take an IPv4 address: Throw an exception if the given interval
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3397
        // contains anything except an IPv4 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3398
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3399
        private int takeIPv4Address(int start, int n, String expected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3400
            throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3401
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3402
            int p = scanIPv4Address(start, n, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3403
            if (p <= start)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3404
                failExpecting(expected, start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3405
            return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3407
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3408
        // Attempt to parse an IPv4 address, returning -1 on failure but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3409
        // allowing the given interval to contain [:<characters>] after
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3410
        // the IPv4 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3411
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3412
        private int parseIPv4Address(int start, int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3413
            int p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3414
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3415
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3416
                p = scanIPv4Address(start, n, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3417
            } catch (URISyntaxException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3418
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3419
            } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3420
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3421
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3422
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3423
            if (p > start && p < n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3424
                // IPv4 address is followed by something - check that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3425
                // it's a ":" as this is the only valid character to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3426
                // follow an address.
34887
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3427
                if (input.charAt(p) != ':') {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3428
                    p = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3429
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3430
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3431
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3432
            if (p > start)
34887
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3433
                host = input.substring(start, p);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3434
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3435
            return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3437
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3438
        // hostname      = domainlabel [ "." ] | 1*( domainlabel "." ) toplabel [ "." ]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3439
        // domainlabel   = alphanum | alphanum *( alphanum | "-" ) alphanum
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3440
        // toplabel      = alpha | alpha *( alphanum | "-" ) alphanum
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3441
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3442
        private int parseHostname(int start, int n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3443
            throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3444
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3445
            int p = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3446
            int q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3447
            int l = -1;                 // Start of last parsed label
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3448
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3449
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3450
                // domainlabel = alphanum [ *( alphanum | "-" ) alphanum ]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3451
                q = scan(p, n, L_ALPHANUM, H_ALPHANUM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3452
                if (q <= p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3453
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3454
                l = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3455
                if (q > p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3456
                    p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3457
                    q = scan(p, n, L_ALPHANUM | L_DASH, H_ALPHANUM | H_DASH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3458
                    if (q > p) {
34887
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3459
                        if (input.charAt(q - 1) == '-')
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3460
                            fail("Illegal character in hostname", q - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3461
                        p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3462
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3463
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3464
                q = scan(p, n, '.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3465
                if (q <= p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3466
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3467
                p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3468
            } while (p < n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3469
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3470
            if ((p < n) && !at(p, n, ':'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3471
                fail("Illegal character in hostname", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3472
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3473
            if (l < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3474
                failExpecting("hostname", start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3475
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3476
            // for a fully qualified hostname check that the rightmost
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3477
            // label starts with an alpha character.
34887
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3478
            if (l > start && !match(input.charAt(l), L_ALPHA, H_ALPHA)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3479
                fail("Illegal character in hostname", l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3480
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3481
34887
fcac26ad0c56 8146526: Improve java.net.URI$Parser startup characteristics
redestad
parents: 34783
diff changeset
  3482
            host = input.substring(start, p);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3483
            return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3485
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3486
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3487
        // IPv6 address parsing, from RFC2373: IPv6 Addressing Architecture
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3488
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3489
        // Bug: The grammar in RFC2373 Appendix B does not allow addresses of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3490
        // the form ::12.34.56.78, which are clearly shown in the examples
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3491
        // earlier in the document.  Here is the original grammar:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3492
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3493
        //   IPv6address = hexpart [ ":" IPv4address ]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3494
        //   hexpart     = hexseq | hexseq "::" [ hexseq ] | "::" [ hexseq ]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3495
        //   hexseq      = hex4 *( ":" hex4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3496
        //   hex4        = 1*4HEXDIG
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3497
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3498
        // We therefore use the following revised grammar:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3499
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3500
        //   IPv6address = hexseq [ ":" IPv4address ]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3501
        //                 | hexseq [ "::" [ hexpost ] ]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3502
        //                 | "::" [ hexpost ]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3503
        //   hexpost     = hexseq | hexseq ":" IPv4address | IPv4address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3504
        //   hexseq      = hex4 *( ":" hex4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3505
        //   hex4        = 1*4HEXDIG
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3506
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3507
        // This covers all and only the following cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3508
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3509
        //   hexseq
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3510
        //   hexseq : IPv4address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3511
        //   hexseq ::
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3512
        //   hexseq :: hexseq
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3513
        //   hexseq :: hexseq : IPv4address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3514
        //   hexseq :: IPv4address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3515
        //   :: hexseq
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3516
        //   :: hexseq : IPv4address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3517
        //   :: IPv4address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3518
        //   ::
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3519
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3520
        // Additionally we constrain the IPv6 address as follows :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3521
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3522
        //  i.  IPv6 addresses without compressed zeros should contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3523
        //      exactly 16 bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3524
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3525
        //  ii. IPv6 addresses with compressed zeros should contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3526
        //      less than 16 bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3527
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3528
        private int ipv6byteCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3529
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3530
        private int parseIPv6Reference(int start, int n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3531
            throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3532
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3533
            int p = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3534
            int q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3535
            boolean compressedZeros = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3536
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3537
            q = scanHexSeq(p, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3538
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3539
            if (q > p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3540
                p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3541
                if (at(p, n, "::")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3542
                    compressedZeros = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3543
                    p = scanHexPost(p + 2, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3544
                } else if (at(p, n, ':')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3545
                    p = takeIPv4Address(p + 1,  n, "IPv4 address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3546
                    ipv6byteCount += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3547
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3548
            } else if (at(p, n, "::")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3549
                compressedZeros = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3550
                p = scanHexPost(p + 2, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3551
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3552
            if (p < n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3553
                fail("Malformed IPv6 address", start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3554
            if (ipv6byteCount > 16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3555
                fail("IPv6 address too long", start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3556
            if (!compressedZeros && ipv6byteCount < 16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3557
                fail("IPv6 address too short", start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3558
            if (compressedZeros && ipv6byteCount == 16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3559
                fail("Malformed IPv6 address", start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3560
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3561
            return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3562
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3563
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3564
        private int scanHexPost(int start, int n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3565
            throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3566
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3567
            int p = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3568
            int q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3569
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3570
            if (p == n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3571
                return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3572
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3573
            q = scanHexSeq(p, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3574
            if (q > p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3575
                p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3576
                if (at(p, n, ':')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3577
                    p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3578
                    p = takeIPv4Address(p, n, "hex digits or IPv4 address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3579
                    ipv6byteCount += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3580
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3581
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3582
                p = takeIPv4Address(p, n, "hex digits or IPv4 address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3583
                ipv6byteCount += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3584
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3585
            return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3586
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3587
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3588
        // Scan a hex sequence; return -1 if one could not be scanned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3589
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3590
        private int scanHexSeq(int start, int n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3591
            throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3592
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3593
            int p = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3594
            int q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3595
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3596
            q = scan(p, n, L_HEX, H_HEX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3597
            if (q <= p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3598
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3599
            if (at(q, n, '.'))          // Beginning of IPv4 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3600
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3601
            if (q > p + 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3602
                fail("IPv6 hexadecimal digit sequence too long", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3603
            ipv6byteCount += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3604
            p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3605
            while (p < n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3606
                if (!at(p, n, ':'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3607
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3608
                if (at(p + 1, n, ':'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3609
                    break;              // "::"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3610
                p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3611
                q = scan(p, n, L_HEX, H_HEX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3612
                if (q <= p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3613
                    failExpecting("digits for an IPv6 address", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3614
                if (at(q, n, '.')) {    // Beginning of IPv4 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3615
                    p--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3616
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3617
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3618
                if (q > p + 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3619
                    fail("IPv6 hexadecimal digit sequence too long", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3620
                ipv6byteCount += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3621
                p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3622
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3623
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3624
            return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3625
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3626
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3627
    }
41555
8a2a5a88376a 8168073: Speed up URI creation during module bootstrap
redestad
parents: 38866
diff changeset
  3628
    static {
8a2a5a88376a 8168073: Speed up URI creation during module bootstrap
redestad
parents: 38866
diff changeset
  3629
        SharedSecrets.setJavaNetUriAccess(
8a2a5a88376a 8168073: Speed up URI creation during module bootstrap
redestad
parents: 38866
diff changeset
  3630
            new JavaNetUriAccess() {
8a2a5a88376a 8168073: Speed up URI creation during module bootstrap
redestad
parents: 38866
diff changeset
  3631
                public URI create(String scheme, String path) {
8a2a5a88376a 8168073: Speed up URI creation during module bootstrap
redestad
parents: 38866
diff changeset
  3632
                    return new URI(scheme, path);
8a2a5a88376a 8168073: Speed up URI creation during module bootstrap
redestad
parents: 38866
diff changeset
  3633
                }
8a2a5a88376a 8168073: Speed up URI creation during module bootstrap
redestad
parents: 38866
diff changeset
  3634
            }
8a2a5a88376a 8168073: Speed up URI creation during module bootstrap
redestad
parents: 38866
diff changeset
  3635
        );
8a2a5a88376a 8168073: Speed up URI creation during module bootstrap
redestad
parents: 38866
diff changeset
  3636
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3637
}