Class NonClosingInputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class NonClosingInputStream
    extends java.io.FilterInputStream

    Wraps an InputStream to change its closing behavior: this makes it possible to ignore close operations or have them perform a InputStream.reset() instead (if supported by the underlying stream) or plain ignored.

    This is useful to pass a stream to an XML parser or validator that automatically closes its input stream -- which makes it impossible to validate a stream and then parse it without reacquiring the stream.

    Example of usage:

     // Get a stream that supports mark/reset in case the original does not
     InputStream buffered = new BufferedInputStream(source);
     // Mark the beginning of the stream and indicate how much buffering is acceptable
     buffered.mark(500000);
     // Wrap the stream so that closing it actually just resets it to the mark
     NonClosingInputStream stream = new NonClosingInputStream(buffered);
     stream.setCloseBehavior(CloseBehavior.RESET)
     // pass it to a schema validator (pseudo code, real code is way more verbose.)
     SAXParserFactory...newSAXParser().parse(stream) ...
     // the validator parser closes the stream, which resets it to the beginning
     // so passing it another parser will actually parse the whole stream
     SAXParser...parse(stream) ...