< prev index next >

src/java.base/share/classes/java/net/URLEncoder.java

Print this page
rev 14210 : 8154231: Simplify access to System properties from JDK code
Reviewed-by: rriggs


   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.ByteArrayOutputStream;
  29 import java.io.BufferedWriter;
  30 import java.io.OutputStreamWriter;
  31 import java.io.IOException;
  32 import java.io.UnsupportedEncodingException;
  33 import java.io.CharArrayWriter;
  34 import java.nio.charset.Charset;
  35 import java.nio.charset.IllegalCharsetNameException;
  36 import java.nio.charset.UnsupportedCharsetException ;
  37 import java.util.BitSet;
  38 import java.security.AccessController;
  39 import java.security.PrivilegedAction;
  40 import sun.security.action.GetBooleanAction;
  41 import sun.security.action.GetPropertyAction;
  42 
  43 /**
  44  * Utility class for HTML form encoding. This class contains static methods
  45  * for converting a String to the <CODE>application/x-www-form-urlencoded</CODE> MIME
  46  * format. For more information about HTML form encoding, consult the HTML
  47  * <A HREF="http://www.w3.org/TR/html4/">specification</A>.
  48  *
  49  * <p>
  50  * When encoding a String, the following rules apply:
  51  *
  52  * <ul>
  53  * <li>The alphanumeric characters &quot;{@code a}&quot; through
  54  *     &quot;{@code z}&quot;, &quot;{@code A}&quot; through
  55  *     &quot;{@code Z}&quot; and &quot;{@code 0}&quot;
  56  *     through &quot;{@code 9}&quot; remain the same.
  57  * <li>The special characters &quot;{@code .}&quot;,
  58  *     &quot;{@code -}&quot;, &quot;{@code *}&quot;, and
  59  *     &quot;{@code _}&quot; remain the same.
  60  * <li>The space character &quot; &nbsp; &quot; is


 123          */
 124 
 125         dontNeedEncoding = new BitSet(256);
 126         int i;
 127         for (i = 'a'; i <= 'z'; i++) {
 128             dontNeedEncoding.set(i);
 129         }
 130         for (i = 'A'; i <= 'Z'; i++) {
 131             dontNeedEncoding.set(i);
 132         }
 133         for (i = '0'; i <= '9'; i++) {
 134             dontNeedEncoding.set(i);
 135         }
 136         dontNeedEncoding.set(' '); /* encoding a space to a + is done
 137                                     * in the encode() method */
 138         dontNeedEncoding.set('-');
 139         dontNeedEncoding.set('_');
 140         dontNeedEncoding.set('.');
 141         dontNeedEncoding.set('*');
 142 
 143         dfltEncName = AccessController.doPrivileged(
 144             new GetPropertyAction("file.encoding")
 145         );
 146     }
 147 
 148     /**
 149      * You can't call the constructor.
 150      */
 151     private URLEncoder() { }
 152 
 153     /**
 154      * Translates a string into {@code x-www-form-urlencoded}
 155      * format. This method uses the platform's default encoding
 156      * as the encoding scheme to obtain the bytes for unsafe characters.
 157      *
 158      * @param   s   {@code String} to be translated.
 159      * @deprecated The resulting string may vary depending on the platform's
 160      *             default encoding. Instead, use the encode(String,String)
 161      *             method to specify the encoding.
 162      * @return  the translated {@code String}.
 163      */
 164     @Deprecated
 165     public static String encode(String s) {




   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 &quot;{@code a}&quot; through
  47  *     &quot;{@code z}&quot;, &quot;{@code A}&quot; through
  48  *     &quot;{@code Z}&quot; and &quot;{@code 0}&quot;
  49  *     through &quot;{@code 9}&quot; remain the same.
  50  * <li>The special characters &quot;{@code .}&quot;,
  51  *     &quot;{@code -}&quot;, &quot;{@code *}&quot;, and
  52  *     &quot;{@code _}&quot; remain the same.
  53  * <li>The space character &quot; &nbsp; &quot; is


 116          */
 117 
 118         dontNeedEncoding = new BitSet(256);
 119         int i;
 120         for (i = 'a'; i <= 'z'; i++) {
 121             dontNeedEncoding.set(i);
 122         }
 123         for (i = 'A'; i <= 'Z'; i++) {
 124             dontNeedEncoding.set(i);
 125         }
 126         for (i = '0'; i <= '9'; i++) {
 127             dontNeedEncoding.set(i);
 128         }
 129         dontNeedEncoding.set(' '); /* encoding a space to a + is done
 130                                     * in the encode() method */
 131         dontNeedEncoding.set('-');
 132         dontNeedEncoding.set('_');
 133         dontNeedEncoding.set('.');
 134         dontNeedEncoding.set('*');
 135 
 136         dfltEncName = GetPropertyAction.getProperty("file.encoding");


 137     }
 138 
 139     /**
 140      * You can't call the constructor.
 141      */
 142     private URLEncoder() { }
 143 
 144     /**
 145      * Translates a string into {@code x-www-form-urlencoded}
 146      * format. This method uses the platform's default encoding
 147      * as the encoding scheme to obtain the bytes for unsafe characters.
 148      *
 149      * @param   s   {@code String} to be translated.
 150      * @deprecated The resulting string may vary depending on the platform's
 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) {


< prev index next >