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