1 /*
2 * Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.net;
27
28 import java.io.UnsupportedEncodingException;
29 import java.io.CharArrayWriter;
30 import java.nio.charset.Charset;
31 import java.nio.charset.IllegalCharsetNameException;
32 import java.nio.charset.UnsupportedCharsetException ;
33 import java.util.BitSet;
34 import sun.security.action.GetPropertyAction;
35
36 /**
37 * Utility class for HTML form encoding. This class contains static methods
38 * for converting a String to the <CODE>application/x-www-form-urlencoded</CODE> MIME
39 * format. For more information about HTML form encoding, consult the HTML
40 * <A HREF="http://www.w3.org/TR/html4/">specification</A>.
41 *
42 * <p>
43 * When encoding a String, the following rules apply:
44 *
45 * <ul>
46 * <li>The alphanumeric characters "{@code a}" through
47 * "{@code z}", "{@code A}" through
48 * "{@code Z}" and "{@code 0}"
49 * through "{@code 9}" remain the same.
50 * <li>The special characters "{@code .}",
51 * "{@code -}", "{@code *}", and
52 * "{@code _}" remain the same.
53 * <li>The space character " " is
151 * default encoding. Instead, use the encode(String,String)
152 * method to specify the encoding.
153 * @return the translated {@code String}.
154 */
155 @Deprecated
156 public static String encode(String s) {
157
158 String str = null;
159
160 try {
161 str = encode(s, dfltEncName);
162 } catch (UnsupportedEncodingException e) {
163 // The system should always have the platform default
164 }
165
166 return str;
167 }
168
169 /**
170 * Translates a string into {@code application/x-www-form-urlencoded}
171 * format using a specific encoding scheme. This method uses the
172 * supplied encoding scheme to obtain the bytes for unsafe
173 * characters.
174 * <p>
175 * <em><strong>Note:</strong> The <a href=
176 * "http://www.w3.org/TR/html40/appendix/notes.html#non-ascii-chars">
177 * World Wide Web Consortium Recommendation</a> states that
178 * UTF-8 should be used. Not doing so may introduce
179 * incompatibilities.</em>
180 *
181 * @param s {@code String} to be translated.
182 * @param enc The name of a supported
183 * <a href="../lang/package-summary.html#charenc">character
184 * encoding</a>.
185 * @return the translated {@code String}.
186 * @exception UnsupportedEncodingException
187 * If the named encoding is not supported
188 * @see URLDecoder#decode(java.lang.String, java.lang.String)
189 * @since 1.4
190 */
191 public static String encode(String s, String enc)
192 throws UnsupportedEncodingException {
193
194 boolean needToChange = false;
195 StringBuilder out = new StringBuilder(s.length());
196 Charset charset;
197 CharArrayWriter charArrayWriter = new CharArrayWriter();
198
199 if (enc == null)
200 throw new NullPointerException("charsetName");
201
202 try {
203 charset = Charset.forName(enc);
204 } catch (IllegalCharsetNameException e) {
205 throw new UnsupportedEncodingException(enc);
206 } catch (UnsupportedCharsetException e) {
207 throw new UnsupportedEncodingException(enc);
208 }
209
210 for (int i = 0; i < s.length();) {
211 int c = (int) s.charAt(i);
212 //System.out.println("Examining character: " + c);
213 if (dontNeedEncoding.get(c)) {
214 if (c == ' ') {
215 c = '+';
216 needToChange = true;
217 }
218 //System.out.println("Storing: " + c);
219 out.append((char)c);
220 i++;
221 } else {
222 // convert to external encoding before hex conversion
223 do {
224 charArrayWriter.write(c);
225 /*
226 * If this character represents the start of a Unicode
227 * surrogate pair, then pass in two characters. It's not
228 * clear what should be done if a byte reserved in the
|
1 /*
2 * Copyright (c) 1995, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.net;
27
28 import java.io.UnsupportedEncodingException;
29 import java.io.CharArrayWriter;
30 import java.nio.charset.Charset;
31 import java.nio.charset.IllegalCharsetNameException;
32 import java.nio.charset.UnsupportedCharsetException ;
33 import java.util.BitSet;
34 import java.util.Objects;
35 import sun.security.action.GetPropertyAction;
36
37 /**
38 * Utility class for HTML form encoding. This class contains static methods
39 * for converting a String to the <CODE>application/x-www-form-urlencoded</CODE> MIME
40 * format. For more information about HTML form encoding, consult the HTML
41 * <A HREF="http://www.w3.org/TR/html4/">specification</A>.
42 *
43 * <p>
44 * When encoding a String, the following rules apply:
45 *
46 * <ul>
47 * <li>The alphanumeric characters "{@code a}" through
48 * "{@code z}", "{@code A}" through
49 * "{@code Z}" and "{@code 0}"
50 * through "{@code 9}" remain the same.
51 * <li>The special characters "{@code .}",
52 * "{@code -}", "{@code *}", and
53 * "{@code _}" remain the same.
54 * <li>The space character " " is
152 * default encoding. Instead, use the encode(String,String)
153 * method to specify the encoding.
154 * @return the translated {@code String}.
155 */
156 @Deprecated
157 public static String encode(String s) {
158
159 String str = null;
160
161 try {
162 str = encode(s, dfltEncName);
163 } catch (UnsupportedEncodingException e) {
164 // The system should always have the platform default
165 }
166
167 return str;
168 }
169
170 /**
171 * Translates a string into {@code application/x-www-form-urlencoded}
172 * format using a specific encoding scheme.
173 * <p>
174 * This method behaves the same as {@linkplain encode(String s, Charset charset)}
175 * except that it will {@linkplain java.nio.charset.Charset#forName look up the charset}
176 * using the given encoding name.
177 *
178 * @param s {@code String} to be translated.
179 * @param enc The name of a supported
180 * <a href="../lang/package-summary.html#charenc">character
181 * encoding</a>.
182 * @return the translated {@code String}.
183 * @throws UnsupportedEncodingException
184 * If the named encoding is not supported
185 * @see URLDecoder#decode(java.lang.String, java.lang.String)
186 * @since 1.4
187 */
188 public static String encode(String s, String enc)
189 throws UnsupportedEncodingException {
190 if (enc == null) {
191 throw new NullPointerException("charsetName");
192 }
193
194 try {
195 Charset charset = Charset.forName(enc);
196 return encode(s, charset);
197 } catch (IllegalCharsetNameException | UnsupportedCharsetException e) {
198 throw new UnsupportedEncodingException(enc);
199 }
200 }
201
202 /**
203 * Translates a string into {@code application/x-www-form-urlencoded}
204 * format using a specific {@linkplain java.nio.charset.Charset Charset}.
205 * This method uses the supplied charset to obtain the bytes for unsafe
206 * characters.
207 * <p>
208 * <em><strong>Note:</strong> The <a href=
209 * "http://www.w3.org/TR/html40/appendix/notes.html#non-ascii-chars">
210 * World Wide Web Consortium Recommendation</a> states that
211 * UTF-8 should be used. Not doing so may introduce incompatibilities.</em>
212 *
213 * @param s {@code String} to be translated.
214 * @param charset the given charset
215 * @return the translated {@code String}.
216 * @throws NullPointerException if {@code s} or {@code charset} is {@code null}.
217 * @see URLDecoder#decode(java.lang.String, java.nio.charset.Charset)
218 * @since 10
219 */
220 public static String encode(String s, Charset charset) {
221 Objects.requireNonNull(charset, "charset");
222
223 boolean needToChange = false;
224 StringBuilder out = new StringBuilder(s.length());
225 CharArrayWriter charArrayWriter = new CharArrayWriter();
226
227 for (int i = 0; i < s.length();) {
228 int c = (int) s.charAt(i);
229 //System.out.println("Examining character: " + c);
230 if (dontNeedEncoding.get(c)) {
231 if (c == ' ') {
232 c = '+';
233 needToChange = true;
234 }
235 //System.out.println("Storing: " + c);
236 out.append((char)c);
237 i++;
238 } else {
239 // convert to external encoding before hex conversion
240 do {
241 charArrayWriter.write(c);
242 /*
243 * If this character represents the start of a Unicode
244 * surrogate pair, then pass in two characters. It's not
245 * clear what should be done if a byte reserved in the
|