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

Alan Bateman alanb at openjdk.org
Tue Mar 25 13:37:18 UTC 2025


On Mon, 24 Mar 2025 11:55:27 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.
>
> Markus KARG has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Removed apiNote and implNote from CharBuffer, as suggested by Chen

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

> 305:     /**
> 306:      * Characters are copied from this sequence into the
> 307:      * destination character array {@code dst}. The first character to

I think the "Characters are copied ..." sentence goes back to StringBuffer in JDK 1.0. CS doesn't need to copy this exactly and I think better to say that it copies chars from this sequence into the given destination array.

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

> 333:      *
> 334:      * @implSpec
> 335:      * The default implementation iterates over {@link #charAt(int)}.

This sentence doesn't make sense, did something get deleted?

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

> 1898: 
> 1899:     /**
> 1900:      * {@inheritDoc}

The method description here will need to start with  "Absolute bulk get method". This is important because CB defines both "absolute bulk get" and "relative bulk get methods", it has to be very clear in the API docs.

If the proposal goes again then I think the method description won't be inherited into CB, instead it will say that it transfers chars from this buffer into the given destination array.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/21730#discussion_r2012114639
PR Review Comment: https://git.openjdk.org/jdk/pull/21730#discussion_r2012116378
PR Review Comment: https://git.openjdk.org/jdk/pull/21730#discussion_r2012054339


More information about the core-libs-dev mailing list