Library enhancement proposal for copying data from/to Reader/Writer InputStream/OutputStream
Patrick Reinhart
patrick at reini.net
Thu Nov 13 19:31:47 UTC 2014
Hi there,
In the followup of a BOF with Stephen Colebourne with his ideas of small library changes that may could get in JDK9. As of the fact that in the codebase of my company there are several locations where we copy from/to IO stream over and over again using either external libraries or do it by ourselves, I suggested to have some support for easy coping data between Input-/OutputStream or Reader/Writer without having to use to external libraries.
My suggestion would look something like this:
try (InputStream in = … ; OutputStream out = …) {
in.copyTo(out);
}
Or from the other end:
try (InputStream in = … ; OutputStream out = …) {
out.copyFrom(in);
}
The same for character based streams:
try (Reader in = …; Writer out = …) {
in.copyTo(out);
}
or
try (Reader in = …; Writer out = …) {
out.copyFrom(in);
}
What do you think about this idea? I could imagine, that we could also pass a IntConsumer / LongConsumer in, that will be called with each amount of successfully copied amount of bytes/chars in case we need a total amount of data or displaying the progress of copy externally.
I’m looking forward to your opinions upon this proposal.
Cheers
Patrick
More information about the core-libs-dev
mailing list