1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package java.net; 19 20 import java.io.UnsupportedEncodingException; 21 import java.nio.charset.Charset; 22 import libcore.net.UriCodec; 23 24 /** 25 * This class is used to decode a string which is encoded in the {@code 26 * application/x-www-form-urlencoded} MIME content type. 27 */ 28 public class URLDecoder { 29 /** 30 * Decodes the argument which is assumed to be encoded in the {@code 31 * x-www-form-urlencoded} MIME content type. 32 * <p> 33 *'+' will be converted to space, '%' and two following hex digit 34 * characters are converted to the equivalent byte value. All other 35 * characters are passed through unmodified. For example "A+B+C %24%25" -> 36 * "A B C $%". 37 * 38 * @param s 39 * the encoded string. 40 * @return the decoded clear-text representation of the given string. 41 * @deprecated use {@link #decode(String, String)} instead. 42 */ 43 @Deprecated decode(String s)44 public static String decode(String s) { 45 return UriCodec.decode(s, true, Charset.defaultCharset(), true); 46 } 47 48 /** 49 * Decodes the argument which is assumed to be encoded in the {@code 50 * x-www-form-urlencoded} MIME content type using the specified encoding 51 * scheme. 52 * <p> 53 *'+' will be converted to space, '%' and two following hex digit 54 * characters are converted to the equivalent byte value. All other 55 * characters are passed through unmodified. For example "A+B+C %24%25" -> 56 * "A B C $%". 57 * 58 * @param s 59 * the encoded string. 60 * @param encoding 61 * the encoding scheme to be used. 62 * @return the decoded clear-text representation of the given string. 63 * @throws UnsupportedEncodingException 64 * if the specified encoding scheme is invalid. 65 */ decode(String s, String encoding)66 public static String decode(String s, String encoding) throws UnsupportedEncodingException { 67 return UriCodec.decode(s, true, Charset.forName(encoding), true); 68 } 69 } 70