A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pmd/pmd/issues/3190 below:

[java] Use StandardCharsets instead of Charset.forName · Issue #3190 · pmd/pmd · GitHub

Use StandardCharsets instead

Proposed Category: Best Practices | Performance (both could apply)

Description:

Starting with Java 7, StandardCharsets provides constants for common Charset objects, such as UTF-8.
Using the constants is less error prone, and can provide a small performance advantage compared to Charset.forName(...)
since no scan across the internal Charset caches is needed.

Code Sample:

This example:

     try (OutputStreamWriter osw = new OutputStreamWriter(out, Charset.forName("UTF-8"))) {

can be replaced with:

       try (OutputStreamWriter osw = new OutputStreamWriter(out, StandardCharsets.UTF_8)) {

However, there is a number of Java API where an overloaded method takes the Charset defition as a string, e.g. the following should also be flagged and reported:

       try (OutputStreamWriter osw = new OutputStreamWriter(out, "UTF-8")) {

Possible Properties:

Maybe a list of common constructors/methods that have an overload with possibility of using both a string/Charset?

Trivial implementation

I have written this trivial implementation looking just for Charset.forName cases:

<rule name="StandardCharsets"
      language="java"
      minimumLanguageVersion="1.8"
      message="Please use StandardCharsets constants"
      class="net.sourceforge.pmd.lang.rule.XPathRule">
   <description>
      StandardCharsets has constants for various common character
      sets.
   </description>
   <priority>3</priority>
   <properties>
      <property name="version" value="2.0"/>
      <property name="xpath">
         <value><![CDATA[
           //PrimaryExpression[PrimaryPrefix/Name/@Image = 'Charset.forName']/PrimarySuffix/Arguments/ArgumentList/Expression/PrimaryExpression/PrimaryPrefix/Literal[@Image = '"US-ASCII"' or @Image = '"ISO-8859-1"'  or @Image = '"UTF-8"'  or @Image = '"UTF-16BE"'  or @Image = '"UTF-16LE"'  or @Image = '"UTF-16"']
         ]]></value>
      </property>
   </properties>
  </rule>

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4