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

(str) Add method CharSequence.getChars() as StringBuilder.getChars()

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P5 P5
    • None
    • 6u10
    • core-libs
    • x86
    • windows_xp

      A DESCRIPTION OF THE REQUEST :
      Add two method to CharSequence
      1. CharSequence.getChars() as StringBuilder.getChars()
      2. CharSequence.writeTo(Writer writer);

      I expect this feature is because I using StringBuilder a lot and then write
      the StringBuilder to Writer directly without call StringBuilder.toString().

      Let's see how many char[] we need to create in this process.
      1. StringBuilder.toString(). -> new String()
      2. Writer.write(String s) -> need create another temporary char[]

      Suggesting:
      1. Change Writer.write(String sr) to Writer.write(CharSequence cs),
      Writer.write(String sr, int off, int len) to Writer.write(CharSequence cs,
      int off, int len)
      2. About CharSequence.writeTo(Writer w), I can have implement in different
      subclass.

          String.writeTo(Writer w) {
              w.write(this); // shall not expose inner buffer to avoid modify the
      String, so have to use a temporary buffer here.
          }


          StringBuilder.writeTo(Writer w) {
              w.write(this.value, 0, count); // see
      java.io.CharArayWriter.writeTo()
          }

      Look, no need to create the two unnecessary char[], isn't it great?

      And another reason is since String, StringBuffer, StringBuilder all have
      this getChars() method, why dont push the method up to CharSequence. I've
      changed a lot of my StringUtil.xxxxx method to using input parameters as
      CharSequence which is very convenient. Which surely will helpful in some
      case, that why the method is here in String and StringBuilder ....

      JUSTIFICATION :
      With the two method, we do not need create two temporary char[] when write CharSequence into writer. And some other similar cases as well.

            martin Martin Buchholz
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: