116 * <p>
117 * This method behaves the same as {@linkplain decode(String s, Charset charset)}
118 * except that it will {@linkplain java.nio.charset.Charset#forName look up the charset}
119 * using the given encoding name.
120 *
121 * @implNote This implementation will throw an {@link java.lang.IllegalArgumentException}
122 * when illegal strings are encountered.
123 *
124 * @param s the {@code String} to decode
125 * @param enc The name of a supported
126 * <a href="../lang/package-summary.html#charenc">character
127 * encoding</a>.
128 * @return the newly decoded {@code String}
129 * @throws UnsupportedEncodingException
130 * If character encoding needs to be consulted, but
131 * named character encoding is not supported
132 * @see URLEncoder#encode(java.lang.String, java.lang.String)
133 * @since 1.4
134 */
135 public static String decode(String s, String enc) throws UnsupportedEncodingException {
136 if (enc.length() == 0) {
137 throw new UnsupportedEncodingException ("URLDecoder: empty string enc parameter");
138 }
139
140 try {
141 Charset charset = Charset.forName(enc);
142 return decode(s, charset);
143 } catch (IllegalCharsetNameException | UnsupportedCharsetException e) {
144 throw new UnsupportedEncodingException(enc);
145 }
146 }
147
148 /**
149 * Decodes an {@code application/x-www-form-urlencoded} string using
150 * a specific {@linkplain java.nio.charset.Charset Charset}.
151 * The supplied charset is used to determine
152 * what characters are represented by any consecutive sequences of the
153 * form "<i>{@code %xy}</i>".
154 * <p>
155 * <em><strong>Note:</strong> The <a href=
156 * "http://www.w3.org/TR/html40/appendix/notes.html#non-ascii-chars">
|
116 * <p>
117 * This method behaves the same as {@linkplain decode(String s, Charset charset)}
118 * except that it will {@linkplain java.nio.charset.Charset#forName look up the charset}
119 * using the given encoding name.
120 *
121 * @implNote This implementation will throw an {@link java.lang.IllegalArgumentException}
122 * when illegal strings are encountered.
123 *
124 * @param s the {@code String} to decode
125 * @param enc The name of a supported
126 * <a href="../lang/package-summary.html#charenc">character
127 * encoding</a>.
128 * @return the newly decoded {@code String}
129 * @throws UnsupportedEncodingException
130 * If character encoding needs to be consulted, but
131 * named character encoding is not supported
132 * @see URLEncoder#encode(java.lang.String, java.lang.String)
133 * @since 1.4
134 */
135 public static String decode(String s, String enc) throws UnsupportedEncodingException {
136 if (enc.isEmpty()) {
137 throw new UnsupportedEncodingException ("URLDecoder: empty string enc parameter");
138 }
139
140 try {
141 Charset charset = Charset.forName(enc);
142 return decode(s, charset);
143 } catch (IllegalCharsetNameException | UnsupportedCharsetException e) {
144 throw new UnsupportedEncodingException(enc);
145 }
146 }
147
148 /**
149 * Decodes an {@code application/x-www-form-urlencoded} string using
150 * a specific {@linkplain java.nio.charset.Charset Charset}.
151 * The supplied charset is used to determine
152 * what characters are represented by any consecutive sequences of the
153 * form "<i>{@code %xy}</i>".
154 * <p>
155 * <em><strong>Note:</strong> The <a href=
156 * "http://www.w3.org/TR/html40/appendix/notes.html#non-ascii-chars">
|