Return-Path: Delivered-To: apmail-jakarta-avalon-dev-archive@jakarta.apache.org Received: (qmail 82630 invoked by uid 500); 5 Jul 2001 05:11:11 -0000 Mailing-List: contact avalon-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Reply-To: "Avalon Development" Delivered-To: mailing list avalon-dev@jakarta.apache.org Received: (qmail 82618 invoked from network); 5 Jul 2001 05:11:10 -0000 Content-Type: text/plain; charset="iso-8859-1" From: Peter Donald To: "Avalon Development" Subject: Re: [PATCH] IOUtil.java additions Date: Thu, 5 Jul 2001 15:04:59 +1000 X-Mailer: KMail [version 1.2] References: <20010704204119.C1181@socialchange.net.au> In-Reply-To: <20010704204119.C1181@socialchange.net.au> MIME-Version: 1.0 Message-Id: <01070515035606.00812@helm.realityforge.org> Content-Transfer-Encoding: 8bit X-Spam-Rating: h31.sny.collab.net 1.6.2 0/1000/N On Thu, 5 Jul 2001 10:41, jeff wrote: > In a dark corner of Excalibur lies a rather unexciting class called > IOUtils.java. It lets you copy between InputStreams and OutputStreams, > cleanly shut down streams, and that's it. > > Over the last week I have ruthlessly expanded it's functionality, such > that there are now methods to copy from (InputStream|Reader|String) to > (OutputStream|Writer|String), with variants to select the buffer size > and (where appropriate) the byte->char encoding. kewl ;) > Example uses: > > // Read text from a file to a String > String s = IOUtil.toString( new FileReader("foo.txt") ); > > // Copy the jakarta home page to a File: > IOUtil.copy( > new URL("http://jakarta.apache.org").openStream(), > new FileOutputStream("index.html") > ).close(); excellent. The only one thing I don't like is return the OutputStream/Writer at the end. I like the following approach better final InputStream input = new URL("http://jakarta.apache.org").openStream(); final FileOutputStream output = new FileOutputStream("index.html"); try { IOUtil.copy( input, output ); } finally { IOUtil.shutdownStream( input ); IOUtil.shutdownStream( output ); } because it forces users to deal with exceptions. Otherwise you get cases like IOUtil.copy( new URL("http://jakarta.apache.org").openStream(), new FileOutputStream("index.html") ).close() When this raises an exception the output stream is never shutdown and you can quickly foobar the system. I actually had a security leak caused by this once (my authenticator would stop authenticating after ~256 requests as it would hit FD limit for process). SO it is possible I am just particularly sensitive on this issue ;) Thoughts? BTW instead of diffing against /dev/null it would be better to just send file so CVS weenies like myself don't end up scratching their heads ;) 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