On Fri, 18 Apr 2025 02:49:43 GMT, Chen Liang <li...@openjdk.org> wrote:

>> `Reader` by specification works on `char`, so I think this is fine. Even if 
>> a StringBuilder is UTF16, it still has to check for all-Latin1 characters at 
>> the time of string creation, so not really an overhead elimination.
>
> 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.

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

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

Reply via email to