You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avalon.apache.org by Jeff Turner <je...@socialchange.net.au> on 2001/05/29 08:41:59 UTC

[PATCH] IOUtil.copy() shouldn't buffer/close streams

Hi,

A bit of background.. org.apache.avalon.excalibur.io.IOUtil has a method which
copies from an InputStream to an OutputStream:

public static void copy( final InputStream source, final OutputStream destination ) throws IOException;

I've been using this to write a stream to System.out for debugging purposes:

InputStream in = new FileInputStream("foo.xml");
org.apache.avalon.excalibur.io.IOUtil.copy(in, System.out);


After much painful debugging, I've discovered that copy() flushes and closes
both streams before exiting. This is certainly not the desired effect for
System.out!

I'd suggest that, as a rule, it is the caller's responsibility to flush and
close streams, not the called method. System.out is a good example of why
methods shouldn't close streams they get passed. Same for flushing; it should
be the caller's responsibility. What happens if my OutputStream was obtained
from response.getOutputStream(), and I only want to flush right at the end (or
my servlet breaks)? Likewise, copy() should not try to wrap the streams in a
Buffered*Stream, as that necessitates a flush, which may not be what the caller
wants/expects.

In short, methods, especially in utility code like excalibur, should be
side-effect-free, doing *only* what they advertise.

</rant> ;)

Anyway, patch is attached. I've also added a bufferedCopy() method for those
who do want buffered/flushed io.


--Jeff

Re: [PATCH] IOUtil.copy() shouldn't buffer/close streams

Posted by Peter Donald <do...@apache.org>.
Hi,

Sounds good except for one point - I will have to make some mods to
FileUtil to keep everything else backwards compatible ;) Will try to do
that and check compatibility tonight ;)

Cheers,

Pete

*-----------------------------------------------------*
| "Faced with the choice between changing one's mind, |
| and proving that there is no need to do so - almost |
| everyone gets busy on the proof."                   |
|              - John Kenneth Galbraith               |
*-----------------------------------------------------*


---------------------------------------------------------------------
To unsubscribe, e-mail: avalon-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: avalon-dev-help@jakarta.apache.org