This discussion is archived
6 Replies Latest reply: Aug 5, 2011 11:51 AM by maheshguruswamy RSS

How to write a generic close method?

880088 Newbie
Currently Being Moderated
Let's say I have some code which does file IO to some type of reader and writer.
Good practices say that I should close these in a finally:

<code>
...
try {
...
} finally {
reader.close();
writer.close();
}
</code>

However, reviewers object that
- the close is not protected from an exception so the second close might never happen.
- if the reader or writer is null, there is no point in trying to close it.
- I should log close failures

So now each close looks something like

<code>
if (reader != null) try { reader.close() } catch (Exception e) { log ... e  }
</code>

This type of thing is a very good idea, but now file closing involves a lot of repititious boilerplate code.
Also, if someone comes up with another great idea on what to do during close, I need to change a lot of code.

If all IO types implemented some interface with a close method I could write a static utility method to do this and then just say

<code>
FileUtil.close(reader);
FileUtil.close(writer);
</code>

But since they do not have any such interface defining close(), how do I code that utility method?
Can this be done with templates?

Thanks,
-- Frank

Edited by: Frank on Aug 5, 2011 8:13 AM
  • 1. Re: How to write a generic close method?
    baftos Expert
    Currently Being Moderated
    Frank wrote:
    If all IO types implemented some interface with a close method I could write a static utility method to do this and then just say

    <code>
    FileUtil.close(reader);
    FileUtil.close(writer);
    </code>

    But since they do not have any such interface defining close(), how do I code that utility method?
    http://download.oracle.com/javase/6/docs/api/java/io/Closeable.html
  • 2. Re: How to write a generic close method?
    880088 Newbie
    Currently Being Moderated
    Fantastic. I missed that.
  • 3. Re: How to write a generic close method?
    maheshguruswamy Journeyer
    Currently Being Moderated
    Frank wrote:
    Let's say I have some code which does file IO to some type of reader and writer.
    Good practices say that I should close these in a finally:

    <code>
    ...
    try {
    ...
    } finally {
    reader.close();
    writer.close();
    }
    </code>

    However, reviewers object that
    - the close is not protected from an exception so the second close might never happen.
    - if the reader or writer is null, there is no point in trying to close it.
    - I should log close failures

    So now each close looks something like

    <code>
    if (reader != null) try { reader.close() } catch (Exception e) { log ... e  }
    </code>

    This type of thing is a very good idea, but now file closing involves a lot of repititious boilerplate code.
    Also, if someone comes up with another great idea on what to do during close, I need to change a lot of code.

    If all IO types implemented some interface with a close method I could write a static utility method to do this and then just say

    <code>
    FileUtil.close(reader);
    FileUtil.close(writer);
    </code>

    But since they do not have any such interface defining close(), how do I code that utility method?
    Can this be done with templates?

    Thanks,
    -- Frank

    Edited by: Frank on Aug 5, 2011 8:13 AM
    If you can wait for JDK 7.0.2 ...you can look at http://download.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
  • 4. Re: How to write a generic close method?
    880088 Newbie
    Currently Being Moderated
    Neat. Does the autoclose handle the resource value being null?
  • 5. Re: How to write a generic close method?
    maheshguruswamy Journeyer
    Currently Being Moderated
    Frank wrote:
    Neat. Does the autoclose handle the resource value being null?
    Haven't tried it but i am guessing it will handle it gracefully. Try it out.
  • 6. Re: How to write a generic close method?
    maheshguruswamy Journeyer
    Currently Being Moderated
    maheshguruswamy wrote:
    Frank wrote:
    Neat. Does the autoclose handle the resource value being null?
    Haven't tried it but i am guessing it will handle it gracefully. Try it out.
    Just tried it myself, handles it well. Also..depending on your situation you might have to retrieve suppressed expressions (more information in the link i posted earlier).

    Also..just to add some context around why i said to wait till jdk 7.0.2. There is some hooey about jdk 7.0 GA having a 'major' bug in looping. Reality is 1) The bug exists in jdk 6.x as well and 2) It manifest itself only in certain types of loops. More details here http://www.infoq.com/news/2011/08/java7-hotspot. So...just watch out for that and use your best judgement.

    Edited by: maheshguruswamy on Aug 5, 2011 11:51 AM

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points