Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8164636

Charset validation method for input string

XMLWordPrintable

      A DESCRIPTION OF THE REQUEST :
      Recently I developed a case when user can type the desired charset name in a box for further use as a nio.Charset. So, validation for the input had to be provided as well.
      The problem is that in java.nio.charset.Charset class there is no method for determination if an input String can be parsed to a charset or not.

      There is isSupported method but it fails when the input string contains illegal characters (such as "-*/").
      So, the only possible way to deal with that is to use forName method and then catch exception but it's a kind of overhead for such a simple operation.


      JUSTIFICATION :
      This enhancement would provide nice and elegant way to determine validity of an input string for getting a Charset.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I would like to see a method

      public static boolean isValid(String charsetName) which validate input string charsetName
      and return either true or false (and doesn't throw exceptions)

      ---------- BEGIN SOURCE ----------
      the same as in workaround section.
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
             try {
                 Charset.forName(encoding);
             } catch (RuntimeException e) {
                 throw new RuntimeException("Encoding is not valid");
             }
      or
           try {
              if (!Charset.isSupported(encoding)) {
                  throw new RuntimeException("Encoding is not valid");
               } catch (IllegalCharsetNameException e) {
                 throw new RuntimeException("Encoding is not valid");
             }


            naoto Naoto Sato
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: