On Fri, 18 Apr 2025 06:59:18 GMT, Shaojin Wen <s...@openjdk.org> wrote:

>> Seems you want to make the `result` StringBuilder fully expanded 
>> (maybeLatin1 = true, coder = UTF16) initially, so we just compress the whole 
>> builder with a vectorized intrinsic instead of char-by-char loop in 
>> `append`. Makes sense.
>
> public static String readString() throws IOException {
>         char[] chars = new char[TRANSFER_BUFFER_SIZE];
>         int n;
>         int off = 0;
>         int rest = chars.length;
>         while ((n = read(chars, off, rest)) != -1) {
>             off += n;
>             if (n == rest) {
>                 chars = Arrays.copyOf(chars, chars.length * 2);
>             }
>             rest = chars.length - off;
>         }
>         return new String(chars, 0, off);
>     }
> 
> Maybe this version is better, it directly expands the capacity on char[] 
> without using StringBuilder, has good performance, and does not need to add 
> new methods.

Maybe it can be implemented by referring to `InputStream::readNBytes(int)` (The 
default implementation of `InputStream::readAllBytes()` is based on it):

https://github.com/openjdk/jdk/blob/22e8a97a1ce4e1c781fbc6f1e271c477fe95f069/src/java.base/share/classes/java/io/InputStream.java#L396-L458

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

PR Review Comment: https://git.openjdk.org/jdk/pull/24728#discussion_r2050746983

Reply via email to