Roedy said:
If you can remember to flush, surely you can remember to close.
Sometimes it isn't an issue of memory or care. For example, I have
several methods which output formatted text to a Writer. The Writer is
provided as a method argument, and the method implementations wrap it in
a Formatter, which is then used to produce the output. (Not exactly
stream wrapping, but Formatters have exactly the same flushing/closing
semantics as stream decorators.) These methods must *not* close their
Formatters because doing so would close the underlying stream, which is
not their responsibility and not the desired behavior. They must flush
their Formatters, however, lest data be lost.
I agree with Tom. Streams that own system resources must assuredly be
closed to prevent resource leaks. This can be accomplished by closing
some decorating stream, but the only advantage in that is ensuring that
all data written directly via that decorator are committed to the stream
destination. If that is done by some other means (or if it isn't
important) then it doesn't matter which stream up or down the decorator
chain is closed -- closing any one of them will free up the system
resources.