Package com.android.io
Class NonClosingInputStream
- java.lang.Object
-
- java.io.InputStream
-
- java.io.FilterInputStream
-
- com.android.io.NonClosingInputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class NonClosingInputStream extends java.io.FilterInputStreamWraps an
InputStreamto change its closing behavior: this makes it possible to ignore close operations or have them perform aInputStream.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) ...
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classNonClosingInputStream.CloseBehavior
-
Constructor Summary
Constructors Constructor Description NonClosingInputStream(java.io.InputStream in)Wraps an existing stream into this filtering stream.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Performs the requestedclose()operation, depending on the currentNonClosingInputStream.CloseBehavior.NonClosingInputStream.CloseBehaviorgetCloseBehavior()Returns the currentNonClosingInputStream.CloseBehavior.NonClosingInputStreamsetCloseBehavior(NonClosingInputStream.CloseBehavior closeBehavior)Changes the currentNonClosingInputStream.CloseBehavior.-
Methods inherited from class java.io.FilterInputStream
available, mark, markSupported, read, read, read, reset, skip
-
-
-
-
Method Detail
-
getCloseBehavior
@NonNull public NonClosingInputStream.CloseBehavior getCloseBehavior()
Returns the currentNonClosingInputStream.CloseBehavior.- Returns:
- the current
NonClosingInputStream.CloseBehavior. Never null.
-
setCloseBehavior
public NonClosingInputStream setCloseBehavior(@NonNull NonClosingInputStream.CloseBehavior closeBehavior)
Changes the currentNonClosingInputStream.CloseBehavior.- Parameters:
closeBehavior- A new non-nullNonClosingInputStream.CloseBehavior.- Returns:
- Self for chaining.
-
close
public void close() throws java.io.IOExceptionPerforms the requestedclose()operation, depending on the currentNonClosingInputStream.CloseBehavior.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.FilterInputStream- Throws:
java.io.IOException
-
-