RFR: 8343110: Add getChars(int, int, char[], int) to CharSequence and CharBuffer

Chen Liang liach at openjdk.org
Sun Mar 23 08:45:15 UTC 2025


On Sat, 26 Oct 2024 15:48:11 GMT, Markus KARG <duke at openjdk.org> wrote:

> This Pull Request proposes an implementation for [JDK-8343110](https://bugs.openjdk.org/browse/JDK-8343110): Adding the new method `public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)` to the `CharSequence` interface, providing a **bulk-read** facility including a default implementation iterating over `charAt(int)`.
> 
> In addition, this Pull Request proposes to replace the implementation of `Reader.of(CharSequence).read(char[] cbuf, int off, int len)` to invoke `CharSequence.getChars(next, next + n, cbuf, off)` instead of utilizing pattern matching for switch. Also, this PR proposes to implement `CharBuffer.getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)` as an alias for `CharBuffer.get(srcBegin, dst, dstBegin, srcEnd - srcBegin)`.
> 
> To ensure quality...
> * ...the method signature and JavaDocs are adapted from `AbstractStringBuilder.getChars(...)`.
> * ...this PR relies upon the existing tests for `Reader.of(CharSequence)`, as these provide sufficient coverage of all changes introduced by this PR.

Oops sorry, did a round of review but forgot to finish it :(

src/java.base/share/classes/java/lang/CharSequence.java line 340:

> 338:     public default void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) {
> 339:         Objects.checkFromToIndex(srcBegin, srcEnd, length());
> 340:         Objects.checkFromIndexSize(dstBegin, srcEnd - srcBegin, dst.length);

We can just do a `checkIndex(dstBegin, dst.length - (srcEnd - srcBegin) + 1)` as we know `srcEnd - srcBegin` is valid.

Also we should specify an NPE is thrown if `dst` is `null`. (Unforunately `compare` forgot this specification when it was added)

src/java.base/share/classes/java/nio/X-Buffer.java.template line 2356:

> 2354: #end[streamableType]
> 2355: 
> 2356: #if[char]

Can we merge this with `// -- Other char stuff --` on line 1895?

On a side note, we can optimize a lot of Appendable operations that transfer from CharSequence on CharBuffer; don't know if you wish to have it in this RFE or another.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/21730#issuecomment-2746093864
PR Review Comment: https://git.openjdk.org/jdk/pull/21730#discussion_r2009009532
PR Review Comment: https://git.openjdk.org/jdk/pull/21730#discussion_r2009009662


More information about the nio-dev mailing list