jdk/src/java.base/share/classes/java/net/URLEncoder.java
author redestad
Thu, 21 Apr 2016 13:39:53 +0200
changeset 37593 824750ada3d6
parent 25859 3317bb8137f4
child 37781 71ed5645f17c
permissions -rw-r--r--
8154231: Simplify access to System properties from JDK code Reviewed-by: rriggs, chegar, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
     2
 * Copyright (c) 1995, 2013, 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 java.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.UnsupportedEncodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.CharArrayWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.charset.Charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.charset.IllegalCharsetNameException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.charset.UnsupportedCharsetException ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.BitSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.action.GetPropertyAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Utility class for HTML form encoding. This class contains static methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * for converting a String to the <CODE>application/x-www-form-urlencoded</CODE> MIME
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * format. For more information about HTML form encoding, consult the HTML
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <A HREF="http://www.w3.org/TR/html4/">specification</A>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * When encoding a String, the following rules apply:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <ul>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    46
 * <li>The alphanumeric characters &quot;{@code a}&quot; through
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    47
 *     &quot;{@code z}&quot;, &quot;{@code A}&quot; through
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    48
 *     &quot;{@code Z}&quot; and &quot;{@code 0}&quot;
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    49
 *     through &quot;{@code 9}&quot; remain the same.
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    50
 * <li>The special characters &quot;{@code .}&quot;,
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    51
 *     &quot;{@code -}&quot;, &quot;{@code *}&quot;, and
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    52
 *     &quot;{@code _}&quot; remain the same.
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    53
 * <li>The space character &quot; &nbsp; &quot; is
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    54
 *     converted into a plus sign &quot;{@code +}&quot;.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <li>All other characters are unsafe and are first converted into
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *     one or more bytes using some encoding scheme. Then each byte is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *     represented by the 3-character string
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    58
 *     &quot;<i>{@code %xy}</i>&quot;, where <i>xy</i> is the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *     two-digit hexadecimal representation of the byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *     The recommended encoding scheme to use is UTF-8. However,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *     for compatibility reasons, if an encoding is not specified,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *     then the default encoding of the platform is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * For example using UTF-8 as the encoding scheme the string &quot;The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * string &#252;@foo-bar&quot; would get converted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * &quot;The+string+%C3%BC%40foo-bar&quot; because in UTF-8 the character
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * &#252; is encoded as two bytes C3 (hex) and BC (hex), and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * character @ is encoded as one byte 40 (hex).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * @author  Herb Jellinek
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 21428
diff changeset
    73
 * @since   1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
public class URLEncoder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    static BitSet dontNeedEncoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    static final int caseDiff = ('a' - 'A');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    static String dfltEncName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        /* The list of characters that are not encoded has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
         * determined as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
         * RFC 2396 states:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
         * -----
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
         * Data characters that are allowed in a URI but do not have a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
         * reserved purpose are called unreserved.  These include upper
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
         * and lower case letters, decimal digits, and a limited set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
         * punctuation marks and symbols.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
         * unreserved  = alphanum | mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
         * mark        = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
         * Unreserved characters can be escaped without changing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
         * semantics of the URI, but this should not be done unless the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
         * URI is being used in a context that does not allow the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
         * unescaped character to appear.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
         * -----
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
         * It appears that both Netscape and Internet Explorer escape
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
         * all special characters from this list with the exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
         * of "-", "_", ".", "*". While it is not clear why they are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
         * escaping the other characters, perhaps it is safest to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
         * assume that there might be contexts in which the others
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
         * are unsafe if not escaped. Therefore, we will use the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
         * list. It is also noteworthy that this is consistent with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
         * O'Reilly's "HTML: The Definitive Guide" (page 164).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
         * As a last note, Intenet Explorer does not encode the "@"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
         * character which is clearly not unreserved according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
         * RFC. We are being consistent with the RFC in this matter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
         * as is Netscape.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        dontNeedEncoding = new BitSet(256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        for (i = 'a'; i <= 'z'; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            dontNeedEncoding.set(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        for (i = 'A'; i <= 'Z'; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            dontNeedEncoding.set(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        for (i = '0'; i <= '9'; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            dontNeedEncoding.set(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        dontNeedEncoding.set(' '); /* encoding a space to a + is done
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                    * in the encode() method */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        dontNeedEncoding.set('-');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        dontNeedEncoding.set('_');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        dontNeedEncoding.set('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        dontNeedEncoding.set('*');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 25859
diff changeset
   136
        dfltEncName = GetPropertyAction.getProperty("file.encoding");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * You can't call the constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    private URLEncoder() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
   145
     * Translates a string into {@code x-www-form-urlencoded}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * format. This method uses the platform's default encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * as the encoding scheme to obtain the bytes for unsafe characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
   149
     * @param   s   {@code String} to be translated.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @deprecated The resulting string may vary depending on the platform's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *             default encoding. Instead, use the encode(String,String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *             method to specify the encoding.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
   153
     * @return  the translated {@code String}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public static String encode(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        String str = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            str = encode(s, dfltEncName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        } catch (UnsupportedEncodingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            // The system should always have the platform default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
   170
     * Translates a string into {@code application/x-www-form-urlencoded}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * format using a specific encoding scheme. This method uses the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * supplied encoding scheme to obtain the bytes for unsafe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * <em><strong>Note:</strong> The <a href=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * "http://www.w3.org/TR/html40/appendix/notes.html#non-ascii-chars">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * World Wide Web Consortium Recommendation</a> states that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * UTF-8 should be used. Not doing so may introduce
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 19069
diff changeset
   179
     * incompatibilities.</em>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
   181
     * @param   s   {@code String} to be translated.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @param   enc   The name of a supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *    <a href="../lang/package-summary.html#charenc">character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *    encoding</a>.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
   185
     * @return  the translated {@code String}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @exception  UnsupportedEncodingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *             If the named encoding is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @see URLDecoder#decode(java.lang.String, java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public static String encode(String s, String enc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        throws UnsupportedEncodingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        boolean needToChange = false;
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 24865
diff changeset
   195
        StringBuilder out = new StringBuilder(s.length());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        Charset charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        CharArrayWriter charArrayWriter = new CharArrayWriter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (enc == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            throw new NullPointerException("charsetName");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            charset = Charset.forName(enc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        } catch (IllegalCharsetNameException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            throw new UnsupportedEncodingException(enc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        } catch (UnsupportedCharsetException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            throw new UnsupportedEncodingException(enc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        for (int i = 0; i < s.length();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            int c = (int) s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            //System.out.println("Examining character: " + c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            if (dontNeedEncoding.get(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                if (c == ' ') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    c = '+';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    needToChange = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                //System.out.println("Storing: " + c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                out.append((char)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                // convert to external encoding before hex conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    charArrayWriter.write(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                     * If this character represents the start of a Unicode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                     * surrogate pair, then pass in two characters. It's not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                     * clear what should be done if a bytes reserved in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                     * surrogate pairs range occurs outside of a legal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                     * surrogate pair. For now, just treat it as if it were
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                     * any other character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                    if (c >= 0xD800 && c <= 0xDBFF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                          System.out.println(Integer.toHexString(c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                          + " is high surrogate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                        if ( (i+1) < s.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                            int d = (int) s.charAt(i+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                              System.out.println("\tExamining "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                              + Integer.toHexString(d));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                            */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                            if (d >= 0xDC00 && d <= 0xDFFF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                                  System.out.println("\t"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                                  + Integer.toHexString(d)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                                  + " is low surrogate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                                */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                                charArrayWriter.write(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                } while (i < s.length() && !dontNeedEncoding.get((c = (int) s.charAt(i))));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                charArrayWriter.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                String str = new String(charArrayWriter.toCharArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                byte[] ba = str.getBytes(charset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                for (int j = 0; j < ba.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    out.append('%');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    char ch = Character.forDigit((ba[j] >> 4) & 0xF, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    // converting to use uppercase letter as part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    // the hex value if ch is a letter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    if (Character.isLetter(ch)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                        ch -= caseDiff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    out.append(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    ch = Character.forDigit(ba[j] & 0xF, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    if (Character.isLetter(ch)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                        ch -= caseDiff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    out.append(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                charArrayWriter.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                needToChange = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        return (needToChange? out.toString() : s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
}