Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why no URLEncoder.encode(String, Charset), URLDecoder.decode(String, Charset) [closed]

I'm not sure if SOF is the best place to ask this, but something about the java URLEncoder and URLDecoder.

For URLEncoder, it has the method encode(String, String) where the second parameter is the name of the encoding to use. If the encoding is not valid, then an UnsupportedEncodingException is thrown. It is a checked exception, so a try-catch statement must be used when calling encode(). This makes sense in terms of using a String encoding...

But Java has the Charset class built in and you can easily access the Charset object of your favourite encoding using Java's StandardCharsets or Guava's Charsets. This would prevent needing to catch an exception that you know will never be thrown if you fed encode() a correctly-spelt encoding name. Without the ability to use a method like URLEncoder.encode(String, Charset), the code I write becomes really ugly because I need to store an extra String variable to store the encoding name and I have a pretty redundant try-catch statement, like so:

private static final String utf8 = "UTF-8";
...
String msg = ...;
try {
    String encodedMsg = URLEncoder.encode(msg, utf8);
    ...
} catch (UnsupportedEncodingException e) {
    // This exception should never happen
    System.err.println("Uh oh...");
}

The same logic applied to URLDecoder.decode(String, String).

So therefore, I'm just wondering, why does Java not have URLEncoder.encode(String, Charset) nor URLDecoder.decode(String, Charset)? Do the developers of the Java language have any plans on supporting this? It would turn the multi-line monstrosity I wrote above into a more pleasant one-liner that doesn't require me catching an exception I know will never occur unless the government made UTF-8 a crime. Are there any existing implementations or libraries that improve on this missing feature from URLEncoder and URLDecoder? I tried searching up for something in Guava, but found nothing.

like image 960
ecbrodie Avatar asked Jul 30 '13 17:07

ecbrodie


People also ask

What does Urldecoder decode do?

decode. Decodes a application/x-www-form-urlencoded string using a specific encoding scheme. The supplied encoding is used to determine what characters are represented by any consecutive sequences of the form " %xy ".

What is URLEncoder encode in Java?

public class URLEncoder extends Object. Utility class for HTML form encoding. This class contains static methods for converting a String to the application/x-www-form-urlencoded MIME format. For more information about HTML form encoding, consult the HTML specification.


1 Answers

I can't say for sure, but my guess is just timing -- URLDecoder came out with JDK 1.0 and Charset came later with JDK 1.4.

YourCharset.name() returns a String containing the canonical name, which you should be able to pass to decode() without fear of an exception.

like image 60
jedwards Avatar answered Sep 30 '22 15:09

jedwards