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) ...
Modifier and Type | Class and Description |
---|---|
static class |
NonClosingInputStream.CloseBehavior |
Constructor and Description |
---|
NonClosingInputStream(java.io.InputStream in)
Wraps an existing stream into this filtering stream.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Performs the requested
close() operation, depending on the current
NonClosingInputStream.CloseBehavior . |
NonClosingInputStream.CloseBehavior |
getCloseBehavior()
Returns the current
NonClosingInputStream.CloseBehavior . |
NonClosingInputStream |
setCloseBehavior(NonClosingInputStream.CloseBehavior closeBehavior)
Changes the current
NonClosingInputStream.CloseBehavior . |
public NonClosingInputStream(@NonNull java.io.InputStream in)
in
- A non-null input stream.@NonNull public NonClosingInputStream.CloseBehavior getCloseBehavior()
NonClosingInputStream.CloseBehavior
.NonClosingInputStream.CloseBehavior
. Never null.public NonClosingInputStream setCloseBehavior(@NonNull NonClosingInputStream.CloseBehavior closeBehavior)
NonClosingInputStream.CloseBehavior
.closeBehavior
- A new non-null NonClosingInputStream.CloseBehavior
.public void close() throws java.io.IOException
close()
operation, depending on the current
NonClosingInputStream.CloseBehavior
.close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.FilterInputStream
java.io.IOException