RFR: 8354724: BufferedReader readAllLines and readString methods [v14]
Markus KARG
duke at openjdk.org
Sat May 3 09:32:51 UTC 2025
On Sat, 3 May 2025 07:25:09 GMT, Markus KARG <duke at openjdk.org> wrote:
>> Brian Burkhalter has updated the pull request incrementally with one additional commit since the last revision:
>>
>> 8354724: Fix readAllChars gaffe in Reader returned by Readed.of and account for it in test
>
> src/java.base/share/classes/java/io/Reader.java line 213:
>
>> 211: public String readAllChars() throws IOException {
>> 212: ensureOpen();
>> 213: return cs.toString().substring(next);
>
> Your change implies creating a full-length string *first* (including cyoping and compression), just to strip it down to a smaller one *later*. It would be more efficient to *first* strip it down, and compression into a `String` *afterwards*. Imagine a huge `cs` with `next` being near to `length()`, and you see the difference in efficiency! 🙂
>
> `cs.subSequence(next, cs.length() - next).toString()`
Oh, and we should set `next` afterwards, so the `Reader` knows that the end of the sequence is reached:
public String readAllChars() throws IOException {
ensureOpen();
var remainder = cs.subSequence(next, cs.length() - next);
next = length();
return remainder.toString();
}
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/24728#discussion_r2072359744
More information about the core-libs-dev
mailing list