[Advanced-java] (style question?) who closes the resource..
Nikolaos Giannopoulos
nikolaos at solmar.ca
Wed Aug 27 21:15:20 2003
> -----Original Message-----
> From: advanced-java-bounces@lists.xcf.berkeley.edu
> [mailto:advanced-java-bounces@lists.xcf.berkeley.edu]On Behalf Of Robert
> Helmer
>
>
> Is it generally expected by most Java programmers here that a
> method that accepts a resource (say an InputStream) will
> possibly close that InputStream, or would you expect that
> to be the job of the caller?
IMO if the caller created the stream then the caller should be responsible
for closing it - that offers the most flexibility and re-useability IMO.
Morevorer, the caller - having created the stream - is also most likely in
the best position to perform any necessarily error handling and direct flow
of the code appropriately.
> I'd assume the latter, but it's not always possible to know
> when the caller will be getting control back (if ever)...
> I suppose you could document that the resource will be closed
> in the JavaDoc, but I don't recall ever seeing that before.
If you go with the former then IMO you may (depends on the case) hamper the
re-useability of the called method as a caller may or may not want the
resource closed - imagine a resource that has markers wherein the caller may
want to step back to a previous mark (or something thereof).
If you leave it up to JavaDoc then you know how far that will get you and I
imagine it won't be long before ones application runs out of file handles
;-)
FWIW,
--Nikolaos