src/java.base/share/classes/sun/net/www/ParseUtil.java
author redestad
Thu, 13 Dec 2018 15:31:05 +0100
changeset 53018 8bf9268df0e2
parent 48897 3f19b5965355
child 54688 96ad739cfc39
permissions -rw-r--r--
8215281: Use String.isEmpty() when applicable in java.base Reviewed-by: dfuchs, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1998, 2007, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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 sun.net.www;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.URI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.URISyntaxException;
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.CharacterCodingException;
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
    36
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.nio.cs.ThreadLocalCoders;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.nio.charset.CharsetDecoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.nio.charset.CoderResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.nio.charset.CodingErrorAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * A class that contains useful routines common to sun.net.www
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author  Mike McCloskey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
48897
3f19b5965355 8197849: Misc improvements to jar resource handling
redestad
parents: 47216
diff changeset
    47
public final class ParseUtil {
3f19b5965355 8197849: Misc improvements to jar resource handling
redestad
parents: 47216
diff changeset
    48
3f19b5965355 8197849: Misc improvements to jar resource handling
redestad
parents: 47216
diff changeset
    49
    private ParseUtil() {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Constructs an encoded version of the specified path string suitable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * for use in the construction of a URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * A path separator is replaced by a forward slash. The string is UTF8
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * encoded. The % escape sequence is used for characters that are above
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * 0x7F or those defined in RFC2396 as reserved or excluded in the path
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * component of a URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public static String encodePath(String path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        return encodePath(path, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * flag indicates whether path uses platform dependent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * File.separatorChar or not. True indicates path uses platform
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * dependent File.separatorChar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public static String encodePath(String path, boolean flag) {
42992
c692f1d73e14 8170785: Excessive allocation in ParseUtil.encodePath
redestad
parents: 32649
diff changeset
    69
        if (flag && File.separatorChar != '/') {
c692f1d73e14 8170785: Excessive allocation in ParseUtil.encodePath
redestad
parents: 32649
diff changeset
    70
            return encodePath(path, 0, File.separatorChar);
c692f1d73e14 8170785: Excessive allocation in ParseUtil.encodePath
redestad
parents: 32649
diff changeset
    71
        } else {
c692f1d73e14 8170785: Excessive allocation in ParseUtil.encodePath
redestad
parents: 32649
diff changeset
    72
            int index = firstEncodeIndex(path);
c692f1d73e14 8170785: Excessive allocation in ParseUtil.encodePath
redestad
parents: 32649
diff changeset
    73
            if (index > -1) {
c692f1d73e14 8170785: Excessive allocation in ParseUtil.encodePath
redestad
parents: 32649
diff changeset
    74
                return encodePath(path, index, '/');
c692f1d73e14 8170785: Excessive allocation in ParseUtil.encodePath
redestad
parents: 32649
diff changeset
    75
            } else {
c692f1d73e14 8170785: Excessive allocation in ParseUtil.encodePath
redestad
parents: 32649
diff changeset
    76
                return path;
c692f1d73e14 8170785: Excessive allocation in ParseUtil.encodePath
redestad
parents: 32649
diff changeset
    77
            }
c692f1d73e14 8170785: Excessive allocation in ParseUtil.encodePath
redestad
parents: 32649
diff changeset
    78
        }
c692f1d73e14 8170785: Excessive allocation in ParseUtil.encodePath
redestad
parents: 32649
diff changeset
    79
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
42992
c692f1d73e14 8170785: Excessive allocation in ParseUtil.encodePath
redestad
parents: 32649
diff changeset
    81
    private static int firstEncodeIndex(String path) {
c692f1d73e14 8170785: Excessive allocation in ParseUtil.encodePath
redestad
parents: 32649
diff changeset
    82
        int len = path.length();
c692f1d73e14 8170785: Excessive allocation in ParseUtil.encodePath
redestad
parents: 32649
diff changeset
    83
        for (int i = 0; i < len; i++) {
c692f1d73e14 8170785: Excessive allocation in ParseUtil.encodePath
redestad
parents: 32649
diff changeset
    84
            char c = path.charAt(i);
48897
3f19b5965355 8197849: Misc improvements to jar resource handling
redestad
parents: 47216
diff changeset
    85
            // Ordering in the following test is performance sensitive,
3f19b5965355 8197849: Misc improvements to jar resource handling
redestad
parents: 47216
diff changeset
    86
            // and typically paths have most chars in the a-z range, then
3f19b5965355 8197849: Misc improvements to jar resource handling
redestad
parents: 47216
diff changeset
    87
            // in the symbol range '&'-':' (includes '.', '/' and '0'-'9')
3f19b5965355 8197849: Misc improvements to jar resource handling
redestad
parents: 47216
diff changeset
    88
            // and more rarely in the A-Z range.
3f19b5965355 8197849: Misc improvements to jar resource handling
redestad
parents: 47216
diff changeset
    89
            if (c >= 'a' && c <= 'z' ||
3f19b5965355 8197849: Misc improvements to jar resource handling
redestad
parents: 47216
diff changeset
    90
                c >= '&' && c <= ':' ||
3f19b5965355 8197849: Misc improvements to jar resource handling
redestad
parents: 47216
diff changeset
    91
                c >= 'A' && c <= 'Z') {
42992
c692f1d73e14 8170785: Excessive allocation in ParseUtil.encodePath
redestad
parents: 32649
diff changeset
    92
                continue;
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
    93
            } else if (c > 0x007F || match(c, L_ENCODED, H_ENCODED)) {
42992
c692f1d73e14 8170785: Excessive allocation in ParseUtil.encodePath
redestad
parents: 32649
diff changeset
    94
                return i;
c692f1d73e14 8170785: Excessive allocation in ParseUtil.encodePath
redestad
parents: 32649
diff changeset
    95
            }
c692f1d73e14 8170785: Excessive allocation in ParseUtil.encodePath
redestad
parents: 32649
diff changeset
    96
        }
c692f1d73e14 8170785: Excessive allocation in ParseUtil.encodePath
redestad
parents: 32649
diff changeset
    97
        return -1;
c692f1d73e14 8170785: Excessive allocation in ParseUtil.encodePath
redestad
parents: 32649
diff changeset
    98
    }
c692f1d73e14 8170785: Excessive allocation in ParseUtil.encodePath
redestad
parents: 32649
diff changeset
    99
c692f1d73e14 8170785: Excessive allocation in ParseUtil.encodePath
redestad
parents: 32649
diff changeset
   100
    private static String encodePath(String path, int index, char sep) {
c692f1d73e14 8170785: Excessive allocation in ParseUtil.encodePath
redestad
parents: 32649
diff changeset
   101
        char[] pathCC = path.toCharArray();
c692f1d73e14 8170785: Excessive allocation in ParseUtil.encodePath
redestad
parents: 32649
diff changeset
   102
        char[] retCC = new char[pathCC.length * 2 + 16 - index];
c692f1d73e14 8170785: Excessive allocation in ParseUtil.encodePath
redestad
parents: 32649
diff changeset
   103
        if (index > 0) {
c692f1d73e14 8170785: Excessive allocation in ParseUtil.encodePath
redestad
parents: 32649
diff changeset
   104
            System.arraycopy(pathCC, 0, retCC, 0, index);
c692f1d73e14 8170785: Excessive allocation in ParseUtil.encodePath
redestad
parents: 32649
diff changeset
   105
        }
c692f1d73e14 8170785: Excessive allocation in ParseUtil.encodePath
redestad
parents: 32649
diff changeset
   106
        int retLen = index;
c692f1d73e14 8170785: Excessive allocation in ParseUtil.encodePath
redestad
parents: 32649
diff changeset
   107
c692f1d73e14 8170785: Excessive allocation in ParseUtil.encodePath
redestad
parents: 32649
diff changeset
   108
        for (int i = index; i < pathCC.length; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            char c = pathCC[i];
42992
c692f1d73e14 8170785: Excessive allocation in ParseUtil.encodePath
redestad
parents: 32649
diff changeset
   110
            if (c == sep)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                retCC[retLen++] = '/';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                if (c <= 0x007F) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    if (c >= 'a' && c <= 'z' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                        c >= 'A' && c <= 'Z' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                        c >= '0' && c <= '9') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                        retCC[retLen++] = c;
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   118
                    } else if (match(c, L_ENCODED, H_ENCODED)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                        retLen = escape(retCC, c, retLen);
42992
c692f1d73e14 8170785: Excessive allocation in ParseUtil.encodePath
redestad
parents: 32649
diff changeset
   120
                    } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                        retCC[retLen++] = c;
42992
c692f1d73e14 8170785: Excessive allocation in ParseUtil.encodePath
redestad
parents: 32649
diff changeset
   122
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                } else if (c > 0x07FF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    retLen = escape(retCC, (char)(0xE0 | ((c >> 12) & 0x0F)), retLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    retLen = escape(retCC, (char)(0x80 | ((c >>  6) & 0x3F)), retLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    retLen = escape(retCC, (char)(0x80 | ((c >>  0) & 0x3F)), retLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    retLen = escape(retCC, (char)(0xC0 | ((c >>  6) & 0x1F)), retLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    retLen = escape(retCC, (char)(0x80 | ((c >>  0) & 0x3F)), retLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            //worst case scenario for character [0x7ff-] every single
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            //character will be encoded into 9 characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            if (retLen + 9 > retCC.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                int newLen = retCC.length * 2 + 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                if (newLen < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    newLen = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                char[] buf = new char[newLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                System.arraycopy(retCC, 0, buf, 0, retLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                retCC = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return new String(retCC, 0, retLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Appends the URL escape sequence for the specified char to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * specified StringBuffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    private static int escape(char[] cc, char c, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        cc[index++] = '%';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        cc[index++] = Character.forDigit((c >> 4) & 0xF, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        cc[index++] = Character.forDigit(c & 0xF, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Un-escape and return the character at position i in string s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    private static byte unescape(String s, int i) {
26720
6b160d97c51d 8055032: Improve numerical parsing in java.net and sun.net
redestad
parents: 25859
diff changeset
   162
        return (byte) Integer.parseInt(s, i + 1, i + 3, 16);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Returns a new String constructed from the specified String by replacing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * the URL escape sequences and UTF8 encoding with the characters they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * represent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public static String decode(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        int n = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if ((n == 0) || (s.indexOf('%') < 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        StringBuilder sb = new StringBuilder(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        ByteBuffer bb = ByteBuffer.allocate(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        CharBuffer cb = CharBuffer.allocate(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        CharsetDecoder dec = ThreadLocalCoders.decoderFor("UTF-8")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            .onMalformedInput(CodingErrorAction.REPORT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            .onUnmappableCharacter(CodingErrorAction.REPORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        char c = s.charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        for (int i = 0; i < n;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            assert c == s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            if (c != '%') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                sb.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                if (++i >= n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                c = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            bb.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            int ui = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                assert (n - i >= 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    bb.put(unescape(s, i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                i += 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                if (i >= n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                c = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                if (c != '%')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            cb.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            dec.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            CoderResult cr = dec.decode(bb, cb, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            if (cr.isError())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                throw new IllegalArgumentException("Error decoding percent encoded characters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            cr = dec.flush(cb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            if (cr.isError())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                throw new IllegalArgumentException("Error decoding percent encoded characters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            sb.append(cb.flip().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Returns a canonical version of the specified string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
48897
3f19b5965355 8197849: Misc improvements to jar resource handling
redestad
parents: 47216
diff changeset
   227
    public static String canonizeString(String file) {
3f19b5965355 8197849: Misc improvements to jar resource handling
redestad
parents: 47216
diff changeset
   228
        int len = file.length();
3f19b5965355 8197849: Misc improvements to jar resource handling
redestad
parents: 47216
diff changeset
   229
        if (len == 0 || (file.indexOf("./") == -1 && file.charAt(len - 1) != '.')) {
3f19b5965355 8197849: Misc improvements to jar resource handling
redestad
parents: 47216
diff changeset
   230
            return file;
3f19b5965355 8197849: Misc improvements to jar resource handling
redestad
parents: 47216
diff changeset
   231
        } else {
3f19b5965355 8197849: Misc improvements to jar resource handling
redestad
parents: 47216
diff changeset
   232
            return doCanonize(file);
3f19b5965355 8197849: Misc improvements to jar resource handling
redestad
parents: 47216
diff changeset
   233
        }
3f19b5965355 8197849: Misc improvements to jar resource handling
redestad
parents: 47216
diff changeset
   234
    }
3f19b5965355 8197849: Misc improvements to jar resource handling
redestad
parents: 47216
diff changeset
   235
3f19b5965355 8197849: Misc improvements to jar resource handling
redestad
parents: 47216
diff changeset
   236
    private static String doCanonize(String file) {
3f19b5965355 8197849: Misc improvements to jar resource handling
redestad
parents: 47216
diff changeset
   237
        int i, lim;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        // Remove embedded /../
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        while ((i = file.indexOf("/../")) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            if ((lim = file.lastIndexOf('/', i - 1)) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                file = file.substring(0, lim) + file.substring(i + 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                file = file.substring(i + 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        // Remove embedded /./
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        while ((i = file.indexOf("/./")) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            file = file.substring(0, i) + file.substring(i + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        // Remove trailing ..
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        while (file.endsWith("/..")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            i = file.indexOf("/..");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            if ((lim = file.lastIndexOf('/', i - 1)) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                file = file.substring(0, lim+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                file = file.substring(0, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        // Remove trailing .
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        if (file.endsWith("/."))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            file = file.substring(0, file.length() -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        return file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public static URL fileToEncodedURL(File file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        throws MalformedURLException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        String path = file.getAbsolutePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        path = ParseUtil.encodePath(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        if (!path.startsWith("/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            path = "/" + path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if (!path.endsWith("/") && file.isDirectory()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            path = path + "/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return new URL("file", "", path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    public static java.net.URI toURI(URL url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        String protocol = url.getProtocol();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        String auth = url.getAuthority();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        String path = url.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        String query = url.getQuery();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        String ref = url.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        if (path != null && !(path.startsWith("/")))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            path = "/" + path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        // In java.net.URI class, a port number of -1 implies the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        // port number. So get it stripped off before creating URI instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        if (auth != null && auth.endsWith(":-1"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            auth = auth.substring(0, auth.length() - 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        java.net.URI uri;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            uri = createURI(protocol, auth, path, query, ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        } catch (java.net.URISyntaxException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            uri = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        return uri;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    // createURI() and its auxiliary code are cloned from java.net.URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    // Most of the code are just copy and paste, except that quote()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    // has been modified to avoid double-escape.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    // Usually it is unacceptable, but we're forced to do it because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    // otherwise we need to change public API, namely java.net.URI's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    // multi-argument constructors. It turns out that the changes cause
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    // incompatibilities so can't be done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    private static URI createURI(String scheme,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                                 String authority,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                                 String path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                                 String query,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                                 String fragment) throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        String s = toString(scheme, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                            authority, null, null, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                            path, query, fragment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        checkPath(s, scheme, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        return new URI(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    private static String toString(String scheme,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                            String opaquePart,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                            String authority,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                            String userInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                            String host,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                            int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                            String path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                            String query,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                            String fragment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        if (scheme != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            sb.append(scheme);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            sb.append(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        appendSchemeSpecificPart(sb, opaquePart,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                                 authority, userInfo, host, port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                                 path, query);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        appendFragment(sb, fragment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    private static void appendSchemeSpecificPart(StringBuffer sb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                                          String opaquePart,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                                          String authority,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                                          String userInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                                          String host,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                                          int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                                          String path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                                          String query)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        if (opaquePart != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            /* check if SSP begins with an IPv6 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
             * because we must not quote a literal IPv6 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            if (opaquePart.startsWith("//[")) {
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 5506
diff changeset
   365
                int end =  opaquePart.indexOf(']');
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 5506
diff changeset
   366
                if (end != -1 && opaquePart.indexOf(':')!=-1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    String doquote, dontquote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    if (end == opaquePart.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                        dontquote = opaquePart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                        doquote = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                        dontquote = opaquePart.substring(0,end+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                        doquote = opaquePart.substring(end+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    sb.append (dontquote);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                    sb.append(quote(doquote, L_URIC, H_URIC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                sb.append(quote(opaquePart, L_URIC, H_URIC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            appendAuthority(sb, authority, userInfo, host, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            if (path != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                sb.append(quote(path, L_PATH, H_PATH));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            if (query != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                sb.append('?');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                sb.append(quote(query, L_URIC, H_URIC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    private static void appendAuthority(StringBuffer sb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                                 String authority,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                                 String userInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                                 String host,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                                 int port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        if (host != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            sb.append("//");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            if (userInfo != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                sb.append(quote(userInfo, L_USERINFO, H_USERINFO));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                sb.append('@');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            boolean needBrackets = ((host.indexOf(':') >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                                    && !host.startsWith("[")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                                    && !host.endsWith("]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            if (needBrackets) sb.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            sb.append(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            if (needBrackets) sb.append(']');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            if (port != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                sb.append(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                sb.append(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        } else if (authority != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            sb.append("//");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            if (authority.startsWith("[")) {
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 5506
diff changeset
   417
                int end = authority.indexOf(']');
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 5506
diff changeset
   418
                if (end != -1 && authority.indexOf(':')!=-1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    String doquote, dontquote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    if (end == authority.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                        dontquote = authority;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                        doquote = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                        dontquote = authority.substring(0,end+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                        doquote = authority.substring(end+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                    sb.append (dontquote);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    sb.append(quote(doquote,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                            L_REG_NAME | L_SERVER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                            H_REG_NAME | H_SERVER));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                sb.append(quote(authority,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                            L_REG_NAME | L_SERVER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                            H_REG_NAME | H_SERVER));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    private static void appendFragment(StringBuffer sb, String fragment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        if (fragment != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            sb.append('#');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            sb.append(quote(fragment, L_URIC, H_URIC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    // Quote any characters in s that are not permitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    // by the given mask pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    private static String quote(String s, long lowMask, long highMask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        int n = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        StringBuffer sb = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        boolean allowNonASCII = ((lowMask & L_ESCAPED) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        for (int i = 0; i < s.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            char c = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            if (c < '\u0080') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                if (!match(c, lowMask, highMask) && !isEscaped(s, i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                    if (sb == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                        sb = new StringBuffer();
31471
ae27c6f1d8bf 8077242: (str) Optimize AbstractStringBuilder.append(CharSequence, int, int) for String argument
igerasim
parents: 26720
diff changeset
   460
                        sb.append(s, 0, i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                    appendEscape(sb, (byte)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    if (sb != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                        sb.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            } else if (allowNonASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                       && (Character.isSpaceChar(c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                           || Character.isISOControl(c))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                if (sb == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                    sb = new StringBuffer();
31471
ae27c6f1d8bf 8077242: (str) Optimize AbstractStringBuilder.append(CharSequence, int, int) for String argument
igerasim
parents: 26720
diff changeset
   472
                    sb.append(s, 0, i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                appendEncoded(sb, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                if (sb != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                    sb.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        return (sb == null) ? s : sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    // To check if the given string has an escaped triplet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    // at the given position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    private static boolean isEscaped(String s, int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        if (s == null || (s.length() <= (pos + 2)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        return s.charAt(pos) == '%'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
               && match(s.charAt(pos + 1), L_HEX, H_HEX)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
               && match(s.charAt(pos + 2), L_HEX, H_HEX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    private static void appendEncoded(StringBuffer sb, char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        ByteBuffer bb = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            bb = ThreadLocalCoders.encoderFor("UTF-8")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                .encode(CharBuffer.wrap("" + c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        } catch (CharacterCodingException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        while (bb.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            int b = bb.get() & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            if (b >= 0x80)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                appendEscape(sb, (byte)b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                sb.append((char)b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31471
diff changeset
   513
    private static final char[] hexDigits = {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        '0', '1', '2', '3', '4', '5', '6', '7',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    private static void appendEscape(StringBuffer sb, byte b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        sb.append('%');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        sb.append(hexDigits[(b >> 4) & 0x0f]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        sb.append(hexDigits[(b >> 0) & 0x0f]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    // Tell whether the given character is permitted by the given mask pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    private static boolean match(char c, long lowMask, long highMask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        if (c < 64)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            return ((1L << c) & lowMask) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        if (c < 128)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            return ((1L << (c - 64)) & highMask) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    // If a scheme is given then the path, if given, must be absolute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    private static void checkPath(String s, String scheme, String path)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        if (scheme != null) {
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 48897
diff changeset
   539
            if (path != null && !path.isEmpty() && path.charAt(0) != '/')
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                throw new URISyntaxException(s,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                                             "Relative path in absolute URI");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    // -- Character classes for parsing --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   548
    // To save startup time, we manually calculate the low-/highMask constants.
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   549
    // For reference, the following methods were used to calculate the values:
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   550
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    // Compute a low-order mask for the characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    // between first and last, inclusive
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   553
    //    private static long lowMask(char first, char last) {
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   554
    //        long m = 0;
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   555
    //        int f = Math.max(Math.min(first, 63), 0);
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   556
    //        int l = Math.max(Math.min(last, 63), 0);
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   557
    //        for (int i = f; i <= l; i++)
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   558
    //            m |= 1L << i;
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   559
    //        return m;
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   560
    //    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    // Compute the low-order mask for the characters in the given string
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   563
    //    private static long lowMask(String chars) {
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   564
    //        int n = chars.length();
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   565
    //        long m = 0;
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   566
    //        for (int i = 0; i < n; i++) {
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   567
    //            char c = chars.charAt(i);
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   568
    //            if (c < 64)
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   569
    //                m |= (1L << c);
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   570
    //        }
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   571
    //        return m;
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   572
    //    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    // Compute a high-order mask for the characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    // between first and last, inclusive
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   576
    //    private static long highMask(char first, char last) {
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   577
    //        long m = 0;
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   578
    //        int f = Math.max(Math.min(first, 127), 64) - 64;
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   579
    //        int l = Math.max(Math.min(last, 127), 64) - 64;
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   580
    //        for (int i = f; i <= l; i++)
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   581
    //            m |= 1L << i;
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   582
    //        return m;
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   583
    //    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    // Compute the high-order mask for the characters in the given string
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   586
    //    private static long highMask(String chars) {
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   587
    //        int n = chars.length();
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   588
    //        long m = 0;
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   589
    //        for (int i = 0; i < n; i++) {
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   590
    //            char c = chars.charAt(i);
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   591
    //            if ((c >= 64) && (c < 128))
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   592
    //                m |= (1L << (c - 64));
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   593
    //        }
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   594
    //        return m;
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   595
    //     }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    // Character-class masks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    // digit    = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    //            "8" | "9"
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   602
    private static final long L_DIGIT = 0x3FF000000000000L; // lowMask('0', '9');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    private static final long H_DIGIT = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    // hex           =  digit | "A" | "B" | "C" | "D" | "E" | "F" |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    //                          "a" | "b" | "c" | "d" | "e" | "f"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    private static final long L_HEX = L_DIGIT;
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   608
    private static final long H_HEX = 0x7E0000007EL; // highMask('A', 'F') | highMask('a', 'f');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    // upalpha  = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    //            "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    //            "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    private static final long L_UPALPHA = 0L;
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   614
    private static final long H_UPALPHA = 0x7FFFFFEL; // highMask('A', 'Z');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    // lowalpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    //            "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    //            "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    private static final long L_LOWALPHA = 0L;
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   620
    private static final long H_LOWALPHA = 0x7FFFFFE00000000L; // highMask('a', 'z');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    // alpha         = lowalpha | upalpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    private static final long L_ALPHA = L_LOWALPHA | L_UPALPHA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    private static final long H_ALPHA = H_LOWALPHA | H_UPALPHA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    // alphanum      = alpha | digit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    private static final long L_ALPHANUM = L_DIGIT | L_ALPHA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    private static final long H_ALPHANUM = H_DIGIT | H_ALPHA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    // mark          = "-" | "_" | "." | "!" | "~" | "*" | "'" |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    //                 "(" | ")"
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   632
    private static final long L_MARK = 0x678200000000L; // lowMask("-_.!~*'()");
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   633
    private static final long H_MARK = 0x4000000080000000L; // highMask("-_.!~*'()");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    // unreserved    = alphanum | mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    private static final long L_UNRESERVED = L_ALPHANUM | L_MARK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    private static final long H_UNRESERVED = H_ALPHANUM | H_MARK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    // reserved      = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    //                 "$" | "," | "[" | "]"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    // Added per RFC2732: "[", "]"
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   642
    private static final long L_RESERVED = 0xAC00985000000000L; // lowMask(";/?:@&=+$,[]");
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   643
    private static final long H_RESERVED = 0x28000001L; // highMask(";/?:@&=+$,[]");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    // The zero'th bit is used to indicate that escape pairs and non-US-ASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    // characters are allowed; this is handled by the scanEscape method below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    private static final long L_ESCAPED = 1L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    private static final long H_ESCAPED = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    // uric          = reserved | unreserved | escaped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    private static final long L_URIC = L_RESERVED | L_UNRESERVED | L_ESCAPED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    private static final long H_URIC = H_RESERVED | H_UNRESERVED | H_ESCAPED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    // pchar         = unreserved | escaped |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    //                 ":" | "@" | "&" | "=" | "+" | "$" | ","
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    private static final long L_PCHAR
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   657
            = L_UNRESERVED | L_ESCAPED | 0x2400185000000000L; // lowMask(":@&=+$,");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    private static final long H_PCHAR
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   659
            = H_UNRESERVED | H_ESCAPED | 0x1L; // highMask(":@&=+$,");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    // All valid path characters
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   662
    private static final long L_PATH = L_PCHAR | 0x800800000000000L; // lowMask(";/");
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   663
    private static final long H_PATH = H_PCHAR; // highMask(";/") == 0x0L;
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   664
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   665
    // Dash, for use in domainlabel and toplabel
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   666
    private static final long L_DASH = 0x200000000000L; // lowMask("-");
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   667
    private static final long H_DASH = 0x0L; // highMask("-");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    // userinfo      = *( unreserved | escaped |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    //                    ";" | ":" | "&" | "=" | "+" | "$" | "," )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    private static final long L_USERINFO
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   672
            = L_UNRESERVED | L_ESCAPED | 0x2C00185000000000L; // lowMask(";:&=+$,");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    private static final long H_USERINFO
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   674
            = H_UNRESERVED | H_ESCAPED; // | highMask(";:&=+$,") == 0L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    // reg_name      = 1*( unreserved | escaped | "$" | "," |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    //                     ";" | ":" | "@" | "&" | "=" | "+" )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    private static final long L_REG_NAME
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   679
            = L_UNRESERVED | L_ESCAPED | 0x2C00185000000000L; // lowMask("$,;:@&=+");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    private static final long H_REG_NAME
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   681
            = H_UNRESERVED | H_ESCAPED | 0x1L; // highMask("$,;:@&=+");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    // All valid characters for server-based authorities
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    private static final long L_SERVER
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   685
            = L_USERINFO | L_ALPHANUM | L_DASH | 0x400400000000000L; // lowMask(".:@[]");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    private static final long H_SERVER
47024
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   687
            = H_USERINFO | H_ALPHANUM | H_DASH | 0x28000001L; // highMask(".:@[]");
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   688
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   689
    // Characters that are encoded in the path component of a URI.
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   690
    //
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   691
    // These characters are reserved in the path segment as described in
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   692
    // RFC2396 section 3.3:
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   693
    //     "=" | ";" | "?" | "/"
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   694
    //
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   695
    // These characters are defined as excluded in RFC2396 section 2.4.3
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   696
    // and must be escaped if they occur in the data part of a URI:
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   697
    //     "#" | " " | "<" | ">" | "%" | "\"" | "{" | "}" | "|" | "\\" | "^" |
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   698
    //     "[" | "]" | "`"
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   699
    //
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   700
    // Also US ASCII control characters 00-1F and 7F.
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   701
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   702
    // lowMask((char)0, (char)31) | lowMask("=;?/# <>%\"{}|\\^[]`");
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   703
    private static final long L_ENCODED = 0xF800802DFFFFFFFFL;
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   704
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   705
    // highMask((char)0x7F, (char)0x7F) | highMask("=;?/# <>%\"{}|\\^[]`");
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   706
    private static final long H_ENCODED = 0xB800000178000000L;
5bfe7700a8f7 8186930: Constant fold URI constants
redestad
parents: 42992
diff changeset
   707
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
}