Package org.apache.commons.io.input
Class CloseShieldInputStream
- java.lang.Object
-
- java.io.InputStream
-
- java.io.FilterInputStream
-
- org.apache.commons.io.input.ProxyInputStream
-
- org.apache.commons.io.input.CloseShieldInputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class CloseShieldInputStream extends ProxyInputStream
Proxy stream that prevents the underlying input stream from being closed.This class is typically used in cases where an input stream needs to be passed to a component that wants to explicitly close the stream even if more input would still be available to other components.
- Since:
- 1.4
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCloseShieldInputStream.BuilderConstructs a new builder forCloseShieldInputStream.-
Nested classes/interfaces inherited from class org.apache.commons.io.input.ProxyInputStream
ProxyInputStream.AbstractBuilder<T,B extends AbstractStreamBuilder<T,B>>
-
-
Field Summary
Fields Modifier and Type Field Description private IOUnaryOperator<java.io.InputStream>onClose
-
Constructor Summary
Constructors Modifier Constructor Description CloseShieldInputStream(java.io.InputStream inputStream)Deprecated.Using this constructor prevents IDEs from warning if the underlying input stream is never closed.privateCloseShieldInputStream(CloseShieldInputStream.Builder builder)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static CloseShieldInputStream.Builderbuilder()Constructs a new builder forCloseShieldInputStream.voidclose()Applies theonClosefunction to the underlying input stream, replacing it with the result.static java.io.InputStreamsystemIn(java.io.InputStream inputStream)Constructs a proxy that only shieldsSystem.infrom closing.static CloseShieldInputStreamwrap(java.io.InputStream inputStream)Constructs a proxy that shields the given input stream from being closed.-
Methods inherited from class org.apache.commons.io.input.ProxyInputStream
afterRead, available, beforeRead, checkOpen, handleIOException, isClosed, mark, markSupported, read, read, read, reset, setReference, skip, unwrap
-
-
-
-
Field Detail
-
onClose
private final IOUnaryOperator<java.io.InputStream> onClose
-
-
Constructor Detail
-
CloseShieldInputStream
private CloseShieldInputStream(CloseShieldInputStream.Builder builder) throws java.io.IOException
- Throws:
java.io.IOException
-
CloseShieldInputStream
@Deprecated public CloseShieldInputStream(java.io.InputStream inputStream)
Deprecated.Using this constructor prevents IDEs from warning if the underlying input stream is never closed. Usewrap(InputStream)instead.Constructs a proxy that shields the given input stream from being closed.- Parameters:
inputStream- underlying input stream.
-
-
Method Detail
-
builder
public static CloseShieldInputStream.Builder builder()
Constructs a new builder forCloseShieldInputStream.- Returns:
- the new builder.
- Since:
- 2.22.0
-
systemIn
public static java.io.InputStream systemIn(java.io.InputStream inputStream)
Constructs a proxy that only shieldsSystem.infrom closing.- Parameters:
inputStream- the candidate input stream.- Returns:
- the given stream or a proxy on
System.in. - Since:
- 2.17.0
-
wrap
public static CloseShieldInputStream wrap(java.io.InputStream inputStream)
Constructs a proxy that shields the given input stream from being closed.- Parameters:
inputStream- the input stream to wrap.- Returns:
- the created proxy.
- Since:
- 2.9.0
-
close
public void close() throws java.io.IOExceptionApplies theonClosefunction to the underlying input stream, replacing it with the result.By default, replaces the underlying input stream with a
ClosedInputStreamsentinel. The original input stream will remain open, but this proxy will appear closed.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classProxyInputStream- Throws:
java.io.IOException- Thrown by theonClosefunction.
-
-