Skip to content

Remove use of File{Input,Ouput}Stream#2649

Open
rovarga wants to merge 1 commit into
apache:mainfrom
rovarga:no-fis
Open

Remove use of File{Input,Ouput}Stream#2649
rovarga wants to merge 1 commit into
apache:mainfrom
rovarga:no-fis

Conversation

@rovarga

@rovarga rovarga commented May 7, 2026

Copy link
Copy Markdown
Contributor

These two classes have a finalizer in Java 12, hence they are an
unnecessary burden for GC.

Use:

  • Files.newInputStream() instead of FileInputStream
  • Files.newBufferedReader() when we end up converting to a Writer
  • Files.readAllBytes() if we end up reading the stream
  • Files.copy() if we are copying the stream to another stream or file
  • Files.writeString() when we are emitting just a plain string
  • Files.newOutputStream() instead of FileOutputStream
  • Files.newBufferedReader() when we end up conerting to a Writer

Signed-off-by: Robert Varga robert.varga@pantheon.tech

These two classes have a finalizer in Java 12, hence they are an
unnecessary burden for GC.

Use:
- Files.newInputStream() instead of FileInputStream
- Files.newBufferedReader() when we end up converting to a Writer
- Files.readAllBytes() if we end up reading the stream
- Files.copy() if we are copying the stream to another stream or file
- Files.writeString() when we are emitting just a plain string
- Files.newOutputStream() instead of FileOutputStream
- Files.newBufferedReader() when we end up conerting to a Writer

Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
OutputStream os = new FileOutputStream(tmpFile)) {
StreamUtils.copy(is, os);
try (var is = urlObj.openStream()) {
Files.copy(is, tmpFile.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.

I think this will always throws FileAlreadyExistsException because Files.creqteTempFile() creates the file before the copy is attempted. REPLACE_EXISTING is required here.

{
StreamUtils.copy(is, os);
try (var is = new URL(url).openStream()) {
Files.copy(is, tmpFile.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.

I think this will always throws FileAlreadyExistsException because Files.creqteTempFile() creates the file before the copy is attempted. REPLACE_EXISTING is required here.

) {
StreamUtils.copy(is, fop);
}
Files.copy(is, file.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.

Without REPLACE_EXISTING, it throws when override=true and the config file already exists. The old FileOutputStream silently overwrote. With Files.copy() will always fail the override path. The same happens in InstallCommand and ConfigMBeanImpl.

FileOutputStream out = new FileOutputStream(dest);
StreamUtils.copy(is, out);
out.close();
Files.copy(is, dest.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.

I think REPLACE_EXISTING is required here. On KAR re-install or upgrade, previously extracted files exist, causing FileAlreadyExistsException and leaving the KAR partially extracted.

return new String(outputStream.toByteArray(), StandardCharsets.UTF_8);
}
private static String loadConfiguration(final URL url) throws IOException {
return new String(url.openStream().readAllBytes(), StandardCharsets.UTF_8);

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.

Here, we potentially leaks the URL stream if readAllBytes() throws mid-read. Previously, we used a proper try-with-resources approach.

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