Skip to content

Replace new FileInputStream with Files.newInputStream()#183

Open
arturobernalg wants to merge 1 commit intoapache:masterfrom
arturobernalg:fileInputStream
Open

Replace new FileInputStream with Files.newInputStream()#183
arturobernalg wants to merge 1 commit intoapache:masterfrom
arturobernalg:fileInputStream

Conversation

@arturobernalg
Copy link
Member

Thanks for your contribution to Apache Commons! Your help is appreciated!

Before you push a pull request, review this list:

  • Read the contribution guidelines for this project.
  • Run a successful build using the default Maven goal with mvn; that's mvn on the command line by itself.
  • Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied. This may not always be possible but is a best-practice.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Each commit in the pull request should have a meaningful subject line and body. Note that commits might be squashed by a maintainer on merge.

@sebbASF
Copy link
Contributor

sebbASF commented Mar 6, 2025

What are the benefits of this change?
Any disadvantages?

@arturobernalg
Copy link
Member Author

What are the benefits of this change? Any disadvantages?

It integrates seamlessly with the Path class, offers more flexible open options, and works well with try-with-resources for automatic resource management. In short, it’s a more robust, future-proof approach compared to the legacy FileInputStream.

@sebbASF
Copy link
Contributor

sebbASF commented Mar 6, 2025

AFAICT, it returns an InputStream rather than a FileInputStream. This means that some methods are not directly available. For example getFD, getChannel.
It does not look like this matters here, but it means the change is not entirely positive

@garydgregory
Copy link
Member

This looks like an OK internal change from IO to NIO.

If you need a Channel from an input stream, just call java.nio.channels.Channels.newChannel(InputStream).

@jochenw
Copy link

jochenw commented Mar 14, 2025

I'm fine with changing, or leaving this, as it is.

A more deserving change would be, IMO, to introduce try-with-resources here.

@garydgregory
Copy link
Member

The changes in this PR are not fully baked IMO. I pushed changes that use better NIO APIs, for example to read and write a file in one shot without having to create streams.

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.

4 participants