RFR: 8378698: Optimize Base64.Encoder#encodeToString
Chen Liang
liach at openjdk.org
Thu Feb 26 15:52:14 UTC 2026
On Thu, 26 Feb 2026 10:17:54 GMT, Eirik Bjørsnøs <eirbjo at openjdk.org> wrote:
>> Avoid a byte array copy in encodeToString by using JavaLangAccess#uncheckedNewStringWithLatin1Bytes
>
> src/java.base/share/classes/java/util/Base64.java line 351:
>
>> 349: */
>> 350: public String encodeToString(byte[] src) {
>> 351: byte[] encoded = encode(src);
>
> Consider adding a comment here to make it clear that `encoded` is guaranteed to be ASCII-only.
>
> Otherwise, using `uncheckedNewStringWithLatin1Bytes` would not be safe. Better to make this contract explicit with a comment.
`encoded` is already a well-behaved platform API, and this method already states:
> In other words, an invocation of this method has exactly the same effect as invoking `new String(encode(src), StandardCharsets.ISO_8859_1)`.
And this is exactly compatible with `uncheckedNewStringWithLatin1Bytes`.
Let's not add redundant comments.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/29920#discussion_r2859802886
More information about the core-libs-dev
mailing list