Search code examples
javainputstreamapache-commons-io

CloseShieldInputStream vs NoCloseInputStream


Is there a difference between Apache Commons' CloseShieldInputStream and Sun's NoCloseInputStream? (Actually in my JDK I have com.sun.xml.internal.ws.util.NoCloseInputStream but no com.sun.xml.ws.util.NoCloseInputStream)

From documentation:

CloseShieldInputStream - proxy stream that prevents the underlying input stream from being closed.


NoCloseInputStream - InputStream that cannot be closed.


Solution

  • There is a subtle difference:

    1. CloseShieldInputStream: When the close method is called, the reference to the originally proxied InputStream is removed and replaced by a no-op implementation. After closing a stream, it is therefore no longer accessible but only exposes the no-op stream. The original stream is however never closed.

    2. NoCloseInputStream: The close method is overridden to do nothing, i.e. not to call the proxied InputStream. After a stream is presumably "closed", it can therefore be used as if close was never called. The added method doClose does however still expose the original closing functionality.

    There is no difference between the two versions of the NoCloseInputStream other than that the internal one is not supposed to be used as it is considered to be internal to Sun's JAX-WS implementation. This internal implementation was later moved to make it public API which is why the package name changed.