Library enhancement proposal for copying data from/to Reader/Writer InputStream/OutputStream

Patrick Reinhart patrick at reini.net
Fri Nov 14 06:14:47 UTC 2014


Hi Alan,

Where would you suggest to put such methods though? Should we aim for a „IOUitls" class holding such methods in the first place and eventually place some simple convenience methods on Input-/OutputStream and Reader/Writer?

I personally would do both, because I think it would make the usage more fluid from the usage perspective coming from a InputStream/Reader. 

Having said that, this leads me to the question: 

Are some other use cases where it would be good to have some sort of a callback in form of a function reference being called to notify about the amount of bytes/chars read and written to the target. This to have a hook for cases where we want to show either some sort of a progress or secondly sum of the total amount of transferred data. 

Also I think there should be may some possibility to stop the copy job on a „feedback“ event as follows:

while ((read=input.read(buffer)>-1 && proceedCheck.read(read)) {
output.write(buffer, 0, read);
}

Where proceedCheck would be a functional interface similar to the IntConsumer or LongConsumer but with a boolean return type signaling to keep on copying or terminate. (or there is a better approach using other functional interfaces, I missed so far)

Other opinions / input? I would be glad to help improve the library in this direction to finally get rid of lots of duplicated code copying data… 

Cheers Patrick


> Am 13.11.2014 um 20:59 schrieb Alan Bateman <Alan.Bateman at oracle.com>:
> 
> On 13/11/2014 19:31, Patrick Reinhart wrote:
>> 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.
>> 
> Long overdue. I remember we prototyped methods like this (and much more) during JDK 7 but didn't do enough at the time to actually get them in. We did include a bunch of easy to use methods in the Files class at the time, including copy between input/output streams and files, but we didn't introduce an IOUtils or such class in java.io.
> 
> -Alan
> 



More information about the adoption-discuss mailing list