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.
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.
- duplicates
-
JDK-6792262 (str) Add a getChars method to java.lang.CharSequence
- Open
-
JDK-8077242 (str) Optimize AbstractStringBuilder.append(CharSequence, int, int) for String argument
- Resolved