On Sat, 19 Aug 2023 09:05:47 GMT, Glavo <[email protected]> wrote:

> `DEFAULT_ENCODING_NAME ` in `URLEncoder` and `URLDecoder` can be replaced 
> with `Charset.defaultCharset()`, which removes unnecessary static fields and 
> avoid looking up charset when calling `URLDecoder.decode(String)` and 
> `URLEncoder.encode(String)`.
> 
> This PR is trivial, since `Charset.defaultCharset()` is also initialized with 
> `StaticProperty.fileEncoding()`, this causes no change in behavior. 
> 
> Moreover, the javadoc of `URLDecoder.decode(String)` and 
> `URLEncoder.encode(String)` say that they use the default charset, so this 
> change is semantically consistent with the documentation.

~I want to make changes to `Charset` like below in another PR:~


public abstract class Charset {
  // ...
  private static class DefaultCharsetHolder {
    static final Charset defaultCharset;
    static {
      Charset cs = lookup(StaticProperty.fileEncoding())
      if (cs != null)
        defaultCharset = cs;
      else
        defaultCharset = sun.nio.cs.UTF_8.INSTANCE;
    }
  }

  public static Charset defaultCharset() {
    return DefaultCharsetHolder.defaultCharset;
  }
  //...
}


~Perhaps I should discuss this change first before considering this PR?~

Update: I used to read the source code for Java 20, but it is outdated. Now 
`defaultCharset()` is already using the `StaticProperty.fileEncoding()`.

I'm just hoping to clean up the redundant code with this PR.

Additionally, by searching on GitHub, I found that there appear to be some use 
cases for this method (although most may be misused). This PR can also slightly 
optimize these use cases.

I think this PR is trivial. It does not change the semantics of the method 
(because the javadoc says it should use default charset), nor does it change 
the behavior of the method (`Charset.defaultCharset()` is also initialized with 
`StaticProperty.fileEncoding()`).

Can someone help create a issue on JBS?

-------------

PR Comment: https://git.openjdk.org/jdk/pull/15353#issuecomment-1725813688
PR Comment: https://git.openjdk.org/jdk/pull/15353#issuecomment-1729730297
PR Comment: https://git.openjdk.org/jdk/pull/15353#issuecomment-1737880783

Reply via email to