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