From 42a675f74e4cabe4f44fc91ac200956d55538a20 Mon Sep 17 00:00:00 2001 From: Markus KARG Date: Sat, 5 Oct 2024 22:05:07 +0000 Subject: [PATCH] Draft: CharSequence.getChars --- .../share/classes/java/io/Reader.java | 11 +------ .../share/classes/java/lang/CharSequence.java | 33 +++++++++++++++++++ .../classes/java/nio/X-Buffer.java.template | 11 +++++++ 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/src/java.base/share/classes/java/io/Reader.java b/src/java.base/share/classes/java/io/Reader.java index 6cad0c6b57f08..eb61169606bb6 100644 --- a/src/java.base/share/classes/java/io/Reader.java +++ b/src/java.base/share/classes/java/io/Reader.java @@ -200,16 +200,7 @@ public int read(char[] cbuf, int off, int len) throws IOException { if (next >= cs.length()) return -1; int n = Math.min(cs.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..6a884acd3f2bf 100644 --- a/src/java.base/share/classes/java/lang/CharSequence.java +++ b/src/java.base/share/classes/java/lang/CharSequence.java @@ -302,4 +302,37 @@ 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}. + * + * @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: + * + */ + public default void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) { + Objects.checkFromToIndex(srcBegin, srcEnd, length()); + int n = srcEnd - srcBegin; + Objects.checkFromToIndex(dstBegin, 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..c297b43ac67f9 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] + @Override + public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) { + Objects.checkFromToIndex(srcBegin, srcEnd, limit()); + int size = srcEnd - srcBegin; + Objects.checkFromIndexSize(dstBegin, size, dst.length); + + getArray(srcBegin, dst, dstBegin, size); + } +#end[char] + }