commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
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 <> schrieb: -----
>Do you mean this:

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()
      public void read(InputStream is) throws IOException
        byte b[] = new byte[1024];
        int length =, 0, 1024);
          ("Content of "+fileName + ": "+ new String(b, 0, length));
    };, ish);

==> No opening, no closing

-----Jukka Zitting <> 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:
For additional commands, e-mail:

View raw message