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