Better handling of StreamInterface::detach()

Calling StreamInterface::detach() now returns the underlying resource
or null if no resource is wrapped by the abstraction. Other methods have
been updated to behave without errors if a stream is closed or detached.