S
Scott Harper
I've read in a couple places (the Essential Java Classes trail of the Sun Java
Tutorial being one), that "A program should close a stream as soon as it is
done with it, in order to free up system resources." Makes sense on face
value, but as I studied this more, I have a few questions.
First, if I have a locally declared stream (any stream) inside a method, when
the method returns and the stream object goes out of scope, wouldn't it
automatically be cleaned up, closed, and returned to the system? I think of
this the way other objects behave, and when their reference counts go to zero,
they are garbage collected. However, one colleague is of the opinion that
this is not the case, and that I have to close the stream before it can be
finalized and handed over to the garbage collector.
In this case, I am dealing primarily with ByteArrayOutputStreams, and it seems
counter-intuitive that they are consuming a lot of system resources anyway. A
little more digging into the API spec shows that
"Closing a ByteArrayOutputStream has no effect. The methods in this class can
be called after the stream has been closed without generating an IOException."
and further more, for OutputStreams in general:
"The close method of OutputStream does nothing."
So that really confuses me. I suspect that an ancestor of OutputStream
implements the Closeable interface, and therefore it gets inherited down to
OutputStream and ByteArrayOutputStream. It also makes sense that experienced
Java programmers just get into the habit of closing streams when they are
done, so by *not* implementing closeable on OutputStreams it could just be
confusing as well.
The only "problem" I have now is that (in theory anyway) ByteArrayOutputStream
can throw an IOException. And now I have to handle that and pass it all the
way up the call stack until some method can handle it intelligently. I have
my doubts that closing a ByteArrayOutputStream would ever generate an
IOException.
So what's the consensus? Don't close ByteArrayOutputStreams? Close them, but
ignore the possible IOException? Or go the full mile and be prepared to
handle the exception?
Thanks,
scott
Tutorial being one), that "A program should close a stream as soon as it is
done with it, in order to free up system resources." Makes sense on face
value, but as I studied this more, I have a few questions.
First, if I have a locally declared stream (any stream) inside a method, when
the method returns and the stream object goes out of scope, wouldn't it
automatically be cleaned up, closed, and returned to the system? I think of
this the way other objects behave, and when their reference counts go to zero,
they are garbage collected. However, one colleague is of the opinion that
this is not the case, and that I have to close the stream before it can be
finalized and handed over to the garbage collector.
In this case, I am dealing primarily with ByteArrayOutputStreams, and it seems
counter-intuitive that they are consuming a lot of system resources anyway. A
little more digging into the API spec shows that
"Closing a ByteArrayOutputStream has no effect. The methods in this class can
be called after the stream has been closed without generating an IOException."
and further more, for OutputStreams in general:
"The close method of OutputStream does nothing."
So that really confuses me. I suspect that an ancestor of OutputStream
implements the Closeable interface, and therefore it gets inherited down to
OutputStream and ByteArrayOutputStream. It also makes sense that experienced
Java programmers just get into the habit of closing streams when they are
done, so by *not* implementing closeable on OutputStreams it could just be
confusing as well.
The only "problem" I have now is that (in theory anyway) ByteArrayOutputStream
can throw an IOException. And now I have to handle that and pass it all the
way up the call stack until some method can handle it intelligently. I have
my doubts that closing a ByteArrayOutputStream would ever generate an
IOException.
So what's the consensus? Don't close ByteArrayOutputStreams? Close them, but
ignore the possible IOException? Or go the full mile and be prepared to
handle the exception?
Thanks,
scott