src/java.base/share/classes/java/net/URLEncoder.java
changeset 48252 77b88d8f8380
parent 47216 71c04702a3d5
child 52499 768b1c612100
equal deleted inserted replaced
48251:57148c79bd75 48252:77b88d8f8380
     1 /*
     1 /*
     2  * Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    29 import java.io.CharArrayWriter;
    29 import java.io.CharArrayWriter;
    30 import java.nio.charset.Charset;
    30 import java.nio.charset.Charset;
    31 import java.nio.charset.IllegalCharsetNameException;
    31 import java.nio.charset.IllegalCharsetNameException;
    32 import java.nio.charset.UnsupportedCharsetException ;
    32 import java.nio.charset.UnsupportedCharsetException ;
    33 import java.util.BitSet;
    33 import java.util.BitSet;
       
    34 import java.util.Objects;
    34 import sun.security.action.GetPropertyAction;
    35 import sun.security.action.GetPropertyAction;
    35 
    36 
    36 /**
    37 /**
    37  * Utility class for HTML form encoding. This class contains static methods
    38  * 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  * for converting a String to the <CODE>application/x-www-form-urlencoded</CODE> MIME
   166         return str;
   167         return str;
   167     }
   168     }
   168 
   169 
   169     /**
   170     /**
   170      * Translates a string into {@code application/x-www-form-urlencoded}
   171      * Translates a string into {@code application/x-www-form-urlencoded}
   171      * format using a specific encoding scheme. This method uses the
   172      * format using a specific encoding scheme.
   172      * supplied encoding scheme to obtain the bytes for unsafe
       
   173      * characters.
       
   174      * <p>
   173      * <p>
   175      * <em><strong>Note:</strong> The <a href=
   174      * This method behaves the same as {@linkplain encode(String s, Charset charset)}
   176      * "http://www.w3.org/TR/html40/appendix/notes.html#non-ascii-chars">
   175      * except that it will {@linkplain java.nio.charset.Charset#forName look up the charset}
   177      * World Wide Web Consortium Recommendation</a> states that
   176      * using the given encoding name.
   178      * UTF-8 should be used. Not doing so may introduce
       
   179      * incompatibilities.</em>
       
   180      *
   177      *
   181      * @param   s   {@code String} to be translated.
   178      * @param   s   {@code String} to be translated.
   182      * @param   enc   The name of a supported
   179      * @param   enc   The name of a supported
   183      *    <a href="../lang/package-summary.html#charenc">character
   180      *    <a href="../lang/package-summary.html#charenc">character
   184      *    encoding</a>.
   181      *    encoding</a>.
   185      * @return  the translated {@code String}.
   182      * @return  the translated {@code String}.
   186      * @exception  UnsupportedEncodingException
   183      * @throws  UnsupportedEncodingException
   187      *             If the named encoding is not supported
   184      *             If the named encoding is not supported
   188      * @see URLDecoder#decode(java.lang.String, java.lang.String)
   185      * @see URLDecoder#decode(java.lang.String, java.lang.String)
   189      * @since 1.4
   186      * @since 1.4
   190      */
   187      */
   191     public static String encode(String s, String enc)
   188     public static String encode(String s, String enc)
   192         throws UnsupportedEncodingException {
   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");
   193 
   222 
   194         boolean needToChange = false;
   223         boolean needToChange = false;
   195         StringBuilder out = new StringBuilder(s.length());
   224         StringBuilder out = new StringBuilder(s.length());
   196         Charset charset;
       
   197         CharArrayWriter charArrayWriter = new CharArrayWriter();
   225         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 
   226 
   210         for (int i = 0; i < s.length();) {
   227         for (int i = 0; i < s.length();) {
   211             int c = (int) s.charAt(i);
   228             int c = (int) s.charAt(i);
   212             //System.out.println("Examining character: " + c);
   229             //System.out.println("Examining character: " + c);
   213             if (dontNeedEncoding.get(c)) {
   230             if (dontNeedEncoding.get(c)) {