Florent Guillaume wrote:
On 9 Dec 2006, at 16:58, Bogdan Stefanescu wrote:
Yes you are right. But more, it is not guaranteed that the input stream will be closed by the garbage collector!
This depends on the stream implementation.
For FileInputStream it is true (i.e. GC will close the stream).
If you look inside class source you have a finalize() method that is doing this:

protected void finalize() throws IOException {
   if (fd != null) {
       if (fd != fd.in) {
       close();
       }
   }
}

But the finalize() method is not mandatory and some implementors may not implement it.

Well, I hope any reasonable implementation would do that! Gratuitously leaking fds is not a reasonable implementation :)

Yes of course ;)
But what I want to point is a general rule: you must not make assumption on how interfaces are implemented if the interface is not specifying anything about how implementor must write the implementation.

Indeed, FileInputStream is using finalize() to close the stream. But you must not rely on that because this is not specified by the InputStream interface. More, in our case url.openStream() returns an InputStream. It may not be a FileInputStream. It may be anything - may be an implementation written by a lazy programmer. So in general it is good to follow the contract of the interface and to not rely on specific implementations.

Bogdan

Florent

--Florent Guillaume, Director of R&D, Nuxeo
Open Source Enterprise Content Management (ECM)
http://www.nuxeo.com   http://www.nuxeo.org   +33 1 40 33 79 87


_______________________________________________
ECM mailing list
[email protected]
http://lists.nuxeo.com/mailman/listinfo/ecm


_______________________________________________
ECM mailing list
[email protected]
http://lists.nuxeo.com/mailman/listinfo/ecm

Reply via email to