Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL decoding: UnsupportedEncodingException in Java

Tags:

java

encoding

What I understand from the documentation is that UnsupportedEncodingException can only be thrown if I specify a wrong encoding as the second parameter to URLDecoder.decode(String, String) method. Is it so? I need to know cases where this exception can be thrown.

Basically, I have this code segment in one of my functions:

if (keyVal.length == 2) {     try {         value = URLDecoder.decode(             keyVal[1],             "UTF-8");     } catch (UnsupportedEncodingException e) {           // Will it ever be thrown?     } } 

Since I am explicitly mentioning "UTF-8", is there any way this exception can be thrown? Do I need to do anything in the catch block? Or, if my understanding is completely wrong, please let me know.

like image 432
Hari Menon Avatar asked May 17 '11 11:05

Hari Menon


People also ask

What is URL decoder in Java?

public class URLDecoder extends Object. Utility class for HTML form decoding. This class contains static methods for decoding a String from the application/x-www-form-urlencoded MIME format. The conversion process is the reverse of that used by the URLEncoder class.

How do you encode and decode URL parameters in Java?

Encode the URLprivate String encodeValue(String value) { return URLEncoder. encode(value, StandardCharsets. UTF_8. toString()); } @Test public void givenRequestParam_whenUTF8Scheme_thenEncode() throws Exception { Map<String, String> requestParams = new HashMap<>(); requestParams.

How do I generate UnsupportedEncodingException?

UnsupportedEncodingException occurs when an unsupported character encoding scheme is used in java strings or bytes. The java String getBytes method converts the requested string to bytes in the specified encoding format. If java does not support the encoding format, the method String getBytes throws java.

How can I tell if a URL is encoded?

So you can test if the string contains a colon, if not, urldecode it, and if that string contains a colon, the original string was url encoded, if not, check if the strings are different and if so, urldecode again and if not, it is not a valid URI.


1 Answers

It cannot happen, unless there is something fundamentally broken in your JVM. But I think you should write this as:

try {     value = URLDecoder.decode(keyVal[1], "UTF-8"); } catch (UnsupportedEncodingException e) {     throw new AssertionError("UTF-8 is unknown");     // or 'throw new AssertionError("Impossible things are happening today. " +     //                              "Consider buying a lottery ticket!!");' } 

The cost of doing this is a few bytes of code that will "never" be executed, and one String literal that will never be used. That a small price for the protecting against the possibility that you may have misread / misunderstood the javadocs (you haven't in this case ...) or that the specs might change (they won't in this case ...)

like image 158
Stephen C Avatar answered Sep 22 '22 05:09

Stephen C