commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sebastian.Petzelber...@aformatik.com
Subject Antwort: Re: [IO] encapsulating open/close
Date Fri, 06 Aug 2010 13:53:16 GMT
Thnaks Niall and Jukka for your awnsers.

-----Niall Pemberton <niall.pemberton@gmail.com> schrieb: -----
>Do you mean this:
>http://developers.sun.com/mobility/apis/articles/fileconnection/

My term "file connection" was maybe misleading.
>public void showFile(String fileName)
Where is the connection closed?

With my suggestion the method would look like:

public static void showFile(final String fileName) throws IOException
{
  InputStreamHandler ish =
    new InputStreamHandler()
    {
      @Override
      public void read(InputStream is) throws IOException
      {
        byte b[] = new byte[1024];
        int length = is.read(b, 0, 1024);
        System.out.println
          ("Content of "+fileName + ": "+ new String(b, 0, length));
      }
    };
    IOUtils.read(fileName, ish);
}

==> No opening, no closing


-----Jukka Zitting <jukka.zitting@gmail.com> schrieb: -----
>OK, I see what you're after. Not sure though if there can be any
>clean API (i.e. one that's less verbose than a "open(); try { ... }
>finally { close(); }" block) for that without language extensions.

My suggestion does not need any language extensions.

>The best solutions I've seen are the automatic resource management
>blocks [1] proposed for Java 7 and the @Cleanup annotation [2]
>from Project Lombok.

Thanks for the link to the Project Lombok, there are some nice ideas there
I had myself. With the @Cleanup annotation opening is done and the closing
is moved somewhere else. I wouldn't prefer this, even when the closing is
just one annotation.

Sebastian Petzelberger


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


Mime
View raw message