JDK-8054317 : java.net.URLDecoder / URLEncoder should accept java.nio.charset.Charset
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.net
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux_ubuntu
  • CPU: x86
  • Submitted: 2014-08-04
  • Updated: 2017-08-25
  • Resolved: 2017-08-25
Related Reports
Duplicate :  
Description
A DESCRIPTION OF THE REQUEST :
Currently URLDecoder has this API:

static String decode(String s, String enc) throws UnsupportedEncodingException

Which is unsafe and annoying because of checked Exception which is in most cases useless when used with a fixed String in code.

Instead, URLDecoder should (additionally) have a method
static String decode(String s, Charset charset)
which does not raise a checked Exception. 



JUSTIFICATION :
This is in line with

public byte[] getBytes(Charset charset) 
public byte[] getBytes(String charsetName)
            throws UnsupportedEncodingException

of java.lang.String

The equivalent reasoning holds for URLEncoder.encode()

As a result code written using these classes can be safer against typos ("UFT-8"), and unnecessary try/catch blocks can be eliminated.



Comments
This is a duplicate of JDK-8178704.
25-08-2017