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

Optimize handling of builtin charsets

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P3
    • Resolution: Fixed
    • Affects Version/s: 7
    • Fix Version/s: 7
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • Resolved In Build:
      b53
    • CPU:
      generic, x86
    • OS:
      generic, linux

      Backports

        Description

        Builtin charsets can be trusted more than user-provided charsets,
        and this provides optimization opportunities.


        /**
         * A {@link Charset} that is provided as part of the JDK.
         * Such a charset guarantees that the following pathological
         * conditions never occur:
         *
         * <ul>
         * <li>no collisions among builtin charset names or aliases
         * <li>the charset encoders and decoders never retain references to
         * the source or destination arrays after a coding operation
         * <li>the {@code Charset} constructors are always called with valid
         * arguments
         * </ul>
         *
         * <p>The default security manager enforces that user-provided
         * charsets do not subclass this class.
         */
        public abstract class BuiltinCharset extends Charset {
            protected BuiltinCharset(String canonicalName, String[] aliases) {
                super(canonicalName, aliases);
            }
        }

        In particular, do the CharsetEncoder and CharsetDecoder constructors really need to
        validate their input every single time, given that their parameters are build-time
        constants in practice?

            {#if[encoder]?protected:private}
            Charset$Coder$(Charset cs,
        float average$ItypesPerOtype$,
        float max$ItypesPerOtype$,
        $replType$ replacement)
            {
        this.charset = cs;
        this.average$ItypesPerOtype$ = average$ItypesPerOtype$;
        this.max$ItypesPerOtype$ = max$ItypesPerOtype$;
        this.replacement = replacement;

                if (cs instanceof sun.nio.cs.BuiltinCharset)
                    assertConstructorPreconditions(cs,
                                                   average$ItypesPerOtype$,
                                                   max$ItypesPerOtype$,
                                                   replacement);
                else
                    checkConstructorPreconditions(cs,
                                                  average$ItypesPerOtype$,
                                                  max$ItypesPerOtype$,
                                                  replacement);
            }

        Also, the StringCoding class could do this to check for whether defensive copies
        are necessary:

            private static boolean charsetIsTrusted(Charset cs) {
                return System.getSecurityManager() == null ||
                    cs instanceof sun.nio.cs.BuiltinCharset ||
                    cs.getClass().getClassLoader0() == null;
            }

          Attachments

            Issue Links

              Activity

                People

                • Assignee:
                  sherman Xueming Shen
                  Reporter:
                  martin Martin Buchholz
                • Votes:
                  0 Vote for this issue
                  Watchers:
                  0 Start watching this issue

                  Dates

                  • Created:
                    Updated:
                    Resolved:
                    Imported:
                    Indexed: