0N/A/*
2362N/A * Copyright (c) 1995, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.net;
0N/A
0N/Aimport java.io.ByteArrayOutputStream;
0N/Aimport java.io.BufferedWriter;
0N/Aimport java.io.OutputStreamWriter;
0N/Aimport java.io.IOException;
0N/Aimport java.io.UnsupportedEncodingException;
0N/Aimport java.io.CharArrayWriter;
0N/Aimport java.nio.charset.Charset;
0N/Aimport java.nio.charset.IllegalCharsetNameException;
0N/Aimport java.nio.charset.UnsupportedCharsetException ;
0N/Aimport java.util.BitSet;
0N/Aimport java.security.AccessController;
0N/Aimport java.security.PrivilegedAction;
0N/Aimport sun.security.action.GetBooleanAction;
0N/Aimport sun.security.action.GetPropertyAction;
0N/A
0N/A/**
0N/A * Utility class for HTML form encoding. This class contains static methods
0N/A * for converting a String to the <CODE>application/x-www-form-urlencoded</CODE> MIME
0N/A * format. For more information about HTML form encoding, consult the HTML
0N/A * <A HREF="http://www.w3.org/TR/html4/">specification</A>.
0N/A *
0N/A * <p>
0N/A * When encoding a String, the following rules apply:
0N/A *
0N/A * <p>
0N/A * <ul>
0N/A * <li>The alphanumeric characters &quot;<code>a</code>&quot; through
0N/A * &quot;<code>z</code>&quot;, &quot;<code>A</code>&quot; through
0N/A * &quot;<code>Z</code>&quot; and &quot;<code>0</code>&quot;
0N/A * through &quot;<code>9</code>&quot; remain the same.
0N/A * <li>The special characters &quot;<code>.</code>&quot;,
0N/A * &quot;<code>-</code>&quot;, &quot;<code>*</code>&quot;, and
0N/A * &quot;<code>_</code>&quot; remain the same.
0N/A * <li>The space character &quot;<code>&nbsp;</code>&quot; is
0N/A * converted into a plus sign &quot;<code>+</code>&quot;.
0N/A * <li>All other characters are unsafe and are first converted into
0N/A * one or more bytes using some encoding scheme. Then each byte is
0N/A * represented by the 3-character string
0N/A * &quot;<code>%<i>xy</i></code>&quot;, where <i>xy</i> is the
0N/A * two-digit hexadecimal representation of the byte.
0N/A * The recommended encoding scheme to use is UTF-8. However,
0N/A * for compatibility reasons, if an encoding is not specified,
0N/A * then the default encoding of the platform is used.
0N/A * </ul>
0N/A *
0N/A * <p>
0N/A * For example using UTF-8 as the encoding scheme the string &quot;The
0N/A * string &#252;@foo-bar&quot; would get converted to
0N/A * &quot;The+string+%C3%BC%40foo-bar&quot; because in UTF-8 the character
0N/A * &#252; is encoded as two bytes C3 (hex) and BC (hex), and the
0N/A * character @ is encoded as one byte 40 (hex).
0N/A *
0N/A * @author Herb Jellinek
0N/A * @since JDK1.0
0N/A */
0N/Apublic class URLEncoder {
0N/A static BitSet dontNeedEncoding;
0N/A static final int caseDiff = ('a' - 'A');
0N/A static String dfltEncName = null;
0N/A
0N/A static {
0N/A
0N/A /* The list of characters that are not encoded has been
0N/A * determined as follows:
0N/A *
0N/A * RFC 2396 states:
0N/A * -----
0N/A * Data characters that are allowed in a URI but do not have a
0N/A * reserved purpose are called unreserved. These include upper
0N/A * and lower case letters, decimal digits, and a limited set of
0N/A * punctuation marks and symbols.
0N/A *
0N/A * unreserved = alphanum | mark
0N/A *
0N/A * mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
0N/A *
0N/A * Unreserved characters can be escaped without changing the
0N/A * semantics of the URI, but this should not be done unless the
0N/A * URI is being used in a context that does not allow the
0N/A * unescaped character to appear.
0N/A * -----
0N/A *
0N/A * It appears that both Netscape and Internet Explorer escape
0N/A * all special characters from this list with the exception
0N/A * of "-", "_", ".", "*". While it is not clear why they are
0N/A * escaping the other characters, perhaps it is safest to
0N/A * assume that there might be contexts in which the others
0N/A * are unsafe if not escaped. Therefore, we will use the same
0N/A * list. It is also noteworthy that this is consistent with
0N/A * O'Reilly's "HTML: The Definitive Guide" (page 164).
0N/A *
0N/A * As a last note, Intenet Explorer does not encode the "@"
0N/A * character which is clearly not unreserved according to the
0N/A * RFC. We are being consistent with the RFC in this matter,
0N/A * as is Netscape.
0N/A *
0N/A */
0N/A
0N/A dontNeedEncoding = new BitSet(256);
0N/A int i;
0N/A for (i = 'a'; i <= 'z'; i++) {
0N/A dontNeedEncoding.set(i);
0N/A }
0N/A for (i = 'A'; i <= 'Z'; i++) {
0N/A dontNeedEncoding.set(i);
0N/A }
0N/A for (i = '0'; i <= '9'; i++) {
0N/A dontNeedEncoding.set(i);
0N/A }
0N/A dontNeedEncoding.set(' '); /* encoding a space to a + is done
0N/A * in the encode() method */
0N/A dontNeedEncoding.set('-');
0N/A dontNeedEncoding.set('_');
0N/A dontNeedEncoding.set('.');
0N/A dontNeedEncoding.set('*');
0N/A
0N/A dfltEncName = AccessController.doPrivileged(
0N/A new GetPropertyAction("file.encoding")
0N/A );
0N/A }
0N/A
0N/A /**
0N/A * You can't call the constructor.
0N/A */
0N/A private URLEncoder() { }
0N/A
0N/A /**
0N/A * Translates a string into <code>x-www-form-urlencoded</code>
0N/A * format. This method uses the platform's default encoding
0N/A * as the encoding scheme to obtain the bytes for unsafe characters.
0N/A *
0N/A * @param s <code>String</code> to be translated.
0N/A * @deprecated The resulting string may vary depending on the platform's
0N/A * default encoding. Instead, use the encode(String,String)
0N/A * method to specify the encoding.
0N/A * @return the translated <code>String</code>.
0N/A */
0N/A @Deprecated
0N/A public static String encode(String s) {
0N/A
0N/A String str = null;
0N/A
0N/A try {
0N/A str = encode(s, dfltEncName);
0N/A } catch (UnsupportedEncodingException e) {
0N/A // The system should always have the platform default
0N/A }
0N/A
0N/A return str;
0N/A }
0N/A
0N/A /**
0N/A * Translates a string into <code>application/x-www-form-urlencoded</code>
0N/A * format using a specific encoding scheme. This method uses the
0N/A * supplied encoding scheme to obtain the bytes for unsafe
0N/A * characters.
0N/A * <p>
0N/A * <em><strong>Note:</strong> The <a href=
0N/A * "http://www.w3.org/TR/html40/appendix/notes.html#non-ascii-chars">
0N/A * World Wide Web Consortium Recommendation</a> states that
0N/A * UTF-8 should be used. Not doing so may introduce
0N/A * incompatibilites.</em>
0N/A *
0N/A * @param s <code>String</code> to be translated.
0N/A * @param enc The name of a supported
0N/A * <a href="../lang/package-summary.html#charenc">character
0N/A * encoding</a>.
0N/A * @return the translated <code>String</code>.
0N/A * @exception UnsupportedEncodingException
0N/A * If the named encoding is not supported
0N/A * @see URLDecoder#decode(java.lang.String, java.lang.String)
0N/A * @since 1.4
0N/A */
0N/A public static String encode(String s, String enc)
0N/A throws UnsupportedEncodingException {
0N/A
0N/A boolean needToChange = false;
0N/A StringBuffer out = new StringBuffer(s.length());
0N/A Charset charset;
0N/A CharArrayWriter charArrayWriter = new CharArrayWriter();
0N/A
0N/A if (enc == null)
0N/A throw new NullPointerException("charsetName");
0N/A
0N/A try {
0N/A charset = Charset.forName(enc);
0N/A } catch (IllegalCharsetNameException e) {
0N/A throw new UnsupportedEncodingException(enc);
0N/A } catch (UnsupportedCharsetException e) {
0N/A throw new UnsupportedEncodingException(enc);
0N/A }
0N/A
0N/A for (int i = 0; i < s.length();) {
0N/A int c = (int) s.charAt(i);
0N/A //System.out.println("Examining character: " + c);
0N/A if (dontNeedEncoding.get(c)) {
0N/A if (c == ' ') {
0N/A c = '+';
0N/A needToChange = true;
0N/A }
0N/A //System.out.println("Storing: " + c);
0N/A out.append((char)c);
0N/A i++;
0N/A } else {
0N/A // convert to external encoding before hex conversion
0N/A do {
0N/A charArrayWriter.write(c);
0N/A /*
0N/A * If this character represents the start of a Unicode
0N/A * surrogate pair, then pass in two characters. It's not
0N/A * clear what should be done if a bytes reserved in the
0N/A * surrogate pairs range occurs outside of a legal
0N/A * surrogate pair. For now, just treat it as if it were
0N/A * any other character.
0N/A */
0N/A if (c >= 0xD800 && c <= 0xDBFF) {
0N/A /*
0N/A System.out.println(Integer.toHexString(c)
0N/A + " is high surrogate");
0N/A */
0N/A if ( (i+1) < s.length()) {
0N/A int d = (int) s.charAt(i+1);
0N/A /*
0N/A System.out.println("\tExamining "
0N/A + Integer.toHexString(d));
0N/A */
0N/A if (d >= 0xDC00 && d <= 0xDFFF) {
0N/A /*
0N/A System.out.println("\t"
0N/A + Integer.toHexString(d)
0N/A + " is low surrogate");
0N/A */
0N/A charArrayWriter.write(d);
0N/A i++;
0N/A }
0N/A }
0N/A }
0N/A i++;
0N/A } while (i < s.length() && !dontNeedEncoding.get((c = (int) s.charAt(i))));
0N/A
0N/A charArrayWriter.flush();
0N/A String str = new String(charArrayWriter.toCharArray());
0N/A byte[] ba = str.getBytes(charset);
0N/A for (int j = 0; j < ba.length; j++) {
0N/A out.append('%');
0N/A char ch = Character.forDigit((ba[j] >> 4) & 0xF, 16);
0N/A // converting to use uppercase letter as part of
0N/A // the hex value if ch is a letter.
0N/A if (Character.isLetter(ch)) {
0N/A ch -= caseDiff;
0N/A }
0N/A out.append(ch);
0N/A ch = Character.forDigit(ba[j] & 0xF, 16);
0N/A if (Character.isLetter(ch)) {
0N/A ch -= caseDiff;
0N/A }
0N/A out.append(ch);
0N/A }
0N/A charArrayWriter.reset();
0N/A needToChange = true;
0N/A }
0N/A }
0N/A
0N/A return (needToChange? out.toString() : s);
0N/A }
0N/A}