RFR: 8310901: Convert String::newStringNoRepl with Latin-1 to String::newStringLatin1NoRepl [v4]

Chen Liang liach at openjdk.org
Wed Jun 28 01:49:05 UTC 2023


On Tue, 27 Jun 2023 07:45:34 GMT, Glavo <duke at openjdk.org> wrote:

>> Added a new method `newStringLatin1NoRepl` to the `JavaLangAccess`.
>> 
>> Reasons:
>> 
>> * Most use cases of `newStringNoRepl` use `ISO_8859_1` as the charset, creating a new shortcut can make writing shorter;
>> * Since all possible values of `byte` are legal Latin-1 characters, `newStringLatin1NoRepl` **will not throw `CharacterCodingException`**, so users can make the compiler happy without using useless try-catch statements.
>
> Glavo has updated the pull request with a new target base due to a merge or a rebase. The incremental webrev excludes the unrelated changes brought in by the merge/rebase. The pull request contains seven additional commits since the last revision:
> 
>  - Merge branch 'openjdk:master' into latin1-no-repl
>  - Merge branch 'openjdk:master' into latin1-no-repl
>  - update javadoc
>  - clean newStringNoRepl1
>  - clean newStringNoRepl1
>  - Rename jla to JLA
>  - Create new method JavaLangAccess::newStringLatin1NoRepl

On a side note, `NoRepl` means "no replication", implying the passed array is already trusted. I think you should do something like this instead:

public static String readString(Path path, Charset cs) throws IOException { 
    Objects.requireNonNull(path); 
    Objects.requireNonNull(cs); 

    byte[] ba = readAllBytes(path); 
    if (path.getClass().getModule() != Object.class.getModule())
        return new String(ba, 0, ba.length, cs);
    return JLA.newStringNoRepl(ba, cs); 
}

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

PR Comment: https://git.openjdk.org/jdk/pull/14655#issuecomment-1610485537


More information about the core-libs-dev mailing list