diff --git a/src/java.base/share/classes/java/io/Reader.java b/src/java.base/share/classes/java/io/Reader.java index 0f3096b36374b..fedee25eda409 100644 --- a/src/java.base/share/classes/java/io/Reader.java +++ b/src/java.base/share/classes/java/io/Reader.java @@ -202,16 +202,7 @@ public int read(char[] cbuf, int off, int len) throws IOException { if (next >= length) return -1; int n = Math.min(length - next, len); - switch (cs) { - case String s -> s.getChars(next, next + n, cbuf, off); - case StringBuilder sb -> sb.getChars(next, next + n, cbuf, off); - case StringBuffer sb -> sb.getChars(next, next + n, cbuf, off); - case CharBuffer cb -> cb.get(next, cbuf, off, n); - default -> { - for (int i = 0; i < n; i++) - cbuf[off + i] = cs.charAt(next + i); - } - } + cs.getChars(next, next + n, cbuf, off); next += n; return n; } diff --git a/src/java.base/share/classes/java/lang/CharSequence.java b/src/java.base/share/classes/java/lang/CharSequence.java index 87bc4a5525a85..6cba3499a58dc 100644 --- a/src/java.base/share/classes/java/lang/CharSequence.java +++ b/src/java.base/share/classes/java/lang/CharSequence.java @@ -302,4 +302,42 @@ public static int compare(CharSequence cs1, CharSequence cs2) { return cs1.length() - cs2.length(); } + /** + * Characters are copied from this sequence into the destination character + * array {@code dst}. + * + * The first character to be copied is at index {@code srcBegin}; the last + * character to be copied is at index {@code srcEnd-1}. The total number of + * characters to be copied is {@code srcEnd-srcBegin}. The characters are + * copied into the subarray of {@code dst} starting at index {@code dstBegin} + * and ending at index {@code dstBegin + (srcEnd-srcBegin) - 1}. + * + * @implSpec + * The default implementation invokes {@link #charAt(int)} to sequentially + * copy one char after the other. + * + * @param srcBegin start copying at this offset. + * @param srcEnd stop copying at this offset. + * @param dst the array to copy the data into. + * @param dstBegin offset into {@code dst}. + * @throws IndexOutOfBoundsException if any of the following is true: + * + * + * @since 24 + */ + public default void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) { + Objects.checkFromToIndex(srcBegin, srcEnd, length()); + int n = srcEnd - srcBegin; + Objects.checkFromIndexSize(dstBegin, n, dst.length); + for (int i = 0; i < n; i++) + dst[dstBegin + i] = charAt(srcBegin + i); + } } diff --git a/src/java.base/share/classes/java/nio/X-Buffer.java.template b/src/java.base/share/classes/java/nio/X-Buffer.java.template index 1b378a2ef4a91..5d2ef6075b44b 100644 --- a/src/java.base/share/classes/java/nio/X-Buffer.java.template +++ b/src/java.base/share/classes/java/nio/X-Buffer.java.template @@ -2333,4 +2333,15 @@ public abstract sealed class $Type$Buffer #end[streamableType] +#if[char] + /** + * // TODO Inherit JavaDocs, but add apiNote that this is same as #get. + * @since 24 + */ + @Override + public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) { + get(srcBegin, dst, dstBegin, srcEnd - srcBegin); + } +#end[char] + }