Skip to content

refactor: use Files.readString() in PemManagersProvider#3143

Merged
He-Pin merged 2 commits into
mainfrom
refactor/files-readstring
Jun 23, 2026
Merged

refactor: use Files.readString() in PemManagersProvider#3143
He-Pin merged 2 commits into
mainfrom
refactor/files-readstring

Conversation

@He-Pin

@He-Pin He-Pin commented Jun 23, 2026

Copy link
Copy Markdown
Member

Motivation

PemManagersProvider.loadPrivateKey reads a PEM file with Files.readAllBytes() and immediately converts to String via new String(bytes, StandardCharsets.UTF_8).

Modification

Replace with Files.readString() (JDK 11) which reads a file directly into a String using UTF-8 by default. Remove the unused StandardCharsets import.

Result

Eliminates intermediate byte[] allocation and explicit charset conversion. One line instead of two.

Tests

  • sbt "remote/compile" — passed

References

Refs #3136

private[ssl] def loadPrivateKey(filename: String): PrivateKey = blocking {
val bytes = Files.readAllBytes(new File(filename).toPath)
val pemData = new String(bytes, StandardCharsets.UTF_8)
val pemData = Files.readString(new File(filename).toPath)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you pass StandardCharsets.UTF_8 param here just to make the code explicit? I agree this is the default for this API but it is easier to understand with the explicit param

He-Pin added 2 commits June 23, 2026 18:55
Motivation:
PemManagersProvider.loadPrivateKey reads a PEM file with
Files.readAllBytes() and immediately converts to String via
new String(bytes, StandardCharsets.UTF_8).

Modification:
Replace with Files.readString() (JDK 11) which reads a file directly
into a String using UTF-8 by default. Remove the unused
StandardCharsets import.

Result:
Eliminates intermediate byte[] allocation and explicit charset
conversion. One line instead of two.

Tests:
sbt "remote/compile" — passed

References:
Refs #3136
Motivation:
While UTF-8 is the default charset for Files.readString, making it
explicit improves code readability and intent clarity.

Modification:
Pass StandardCharsets.UTF_8 as the second argument to Files.readString.

Result:
Code is more explicit about the expected encoding.

References:
Refs #3143
@He-Pin He-Pin force-pushed the refactor/files-readstring branch from 3782db5 to 8dabf23 Compare June 23, 2026 10:58

@pjfanning pjfanning left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@He-Pin He-Pin merged commit 22d44aa into main Jun 23, 2026
9 checks passed
@He-Pin He-Pin deleted the refactor/files-readstring branch June 23, 2026 11:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants