com.android.io
Class NonClosingInputStream
java.lang.Object
java.io.InputStream
java.io.FilterInputStream
com.android.io.NonClosingInputStream
- All Implemented Interfaces:
- java.io.Closeable
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) ...
Fields inherited from class java.io.FilterInputStream |
in |
Constructor Summary |
NonClosingInputStream(java.io.InputStream in)
Wraps an existing stream into this filtering stream. |
Methods inherited from class java.io.FilterInputStream |
available, mark, markSupported, read, read, read, reset, skip |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
NonClosingInputStream
public NonClosingInputStream(@NonNull
java.io.InputStream in)
- Wraps an existing stream into this filtering stream.
- Parameters:
in
- A non-null input stream.
getCloseBehavior
@NonNull
public NonClosingInputStream.CloseBehavior getCloseBehavior()
- Returns the current
NonClosingInputStream.CloseBehavior
.
- Returns:
- the current
NonClosingInputStream.CloseBehavior
. Never null.
setCloseBehavior
public NonClosingInputStream setCloseBehavior(@NonNull
NonClosingInputStream.CloseBehavior closeBehavior)
- Changes the current
NonClosingInputStream.CloseBehavior
.
- Parameters:
closeBehavior
- A new non-null NonClosingInputStream.CloseBehavior
.
- Returns:
- Self for chaining.
close
public void close()
throws java.io.IOException
- Performs the requested
close()
operation, depending on the current
NonClosingInputStream.CloseBehavior
.
- Specified by:
close
in interface java.io.Closeable
- Overrides:
close
in class java.io.FilterInputStream
- Throws:
java.io.IOException