Package org.apache.http.conn
Class EofSensorInputStream
- java.lang.Object
-
- java.io.InputStream
-
- org.apache.http.conn.EofSensorInputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,ConnectionReleaseTrigger
public class EofSensorInputStream extends java.io.InputStream implements ConnectionReleaseTrigger
A stream wrapper that triggers actions onclose()and EOF. Primarily used to auto-release an underlying managed connection when the response body is consumed or no longer needed.- Since:
- 4.0
- See Also:
EofSensorWatcher
-
-
Field Summary
Fields Modifier and Type Field Description private EofSensorWatchereofWatcherThe watcher to be notified, if any.private booleanselfClosedIndicates whether this stream itself is closed.protected java.io.InputStreamwrappedStreamThe wrapped input stream, while accessible.
-
Constructor Summary
Constructors Constructor Description EofSensorInputStream(java.io.InputStream in, EofSensorWatcher watcher)Creates a new EOF sensor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidabortConnection()Aborts this stream.intavailable()protected voidcheckAbort()Detects stream abort and notifies the watcher.protected voidcheckClose()Detects stream close and notifies the watcher.protected voidcheckEOF(int eof)Detects EOF and notifies the watcher.voidclose()(package private) java.io.InputStreamgetWrappedStream()protected booleanisReadAllowed()Checks whether the underlying stream can be read from.(package private) booleanisSelfClosed()intread()intread(byte[] b)intread(byte[] b, int off, int len)voidreleaseConnection()Same asclose().
-
-
-
Field Detail
-
wrappedStream
protected java.io.InputStream wrappedStream
The wrapped input stream, while accessible. The value changes tonullwhen the wrapped stream becomes inaccessible.
-
selfClosed
private boolean selfClosed
Indicates whether this stream itself is closed. If it isn't, butwrappedStreamisnull, we're running in EOF mode. All read operations will indicate EOF without accessing the underlying stream. After closing this stream, read operations will trigger anIOException.- See Also:
isReadAllowed
-
eofWatcher
private final EofSensorWatcher eofWatcher
The watcher to be notified, if any.
-
-
Constructor Detail
-
EofSensorInputStream
public EofSensorInputStream(java.io.InputStream in, EofSensorWatcher watcher)Creates a new EOF sensor. If no watcher is passed, the underlying stream will simply be closed when EOF is detected orcloseis called. Otherwise, the watcher decides whether the underlying stream should be closed before detaching from it.- Parameters:
in- the wrapped streamwatcher- the watcher for events, ornullfor auto-close behavior without notification
-
-
Method Detail
-
isSelfClosed
boolean isSelfClosed()
-
getWrappedStream
java.io.InputStream getWrappedStream()
-
isReadAllowed
protected boolean isReadAllowed() throws java.io.IOExceptionChecks whether the underlying stream can be read from.- Returns:
trueif the underlying stream is accessible,falseif this stream is in EOF mode and detached from the underlying stream- Throws:
java.io.IOException- if this stream is already closed
-
read
public int read() throws java.io.IOException- Specified by:
readin classjava.io.InputStream- Throws:
java.io.IOException
-
read
public int read(byte[] b, int off, int len) throws java.io.IOException- Overrides:
readin classjava.io.InputStream- Throws:
java.io.IOException
-
read
public int read(byte[] b) throws java.io.IOException- Overrides:
readin classjava.io.InputStream- Throws:
java.io.IOException
-
available
public int available() throws java.io.IOException- Overrides:
availablein classjava.io.InputStream- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.InputStream- Throws:
java.io.IOException
-
checkEOF
protected void checkEOF(int eof) throws java.io.IOExceptionDetects EOF and notifies the watcher. This method should only be called while the underlying stream is still accessible. UseisReadAllowedto check that condition.If EOF is detected, the watcher will be notified and this stream is detached from the underlying stream. This prevents multiple notifications from this stream.
- Parameters:
eof- the result of the calling read operation. A negative value indicates that EOF is reached.- Throws:
java.io.IOException- in case of an IO problem on closing the underlying stream
-
checkClose
protected void checkClose() throws java.io.IOExceptionDetects stream close and notifies the watcher. There's not much to detect since this is called byclose. The watcher will only be notified if this stream is closed for the first time and before EOF has been detected. This stream will be detached from the underlying stream to prevent multiple notifications to the watcher.- Throws:
java.io.IOException- in case of an IO problem on closing the underlying stream
-
checkAbort
protected void checkAbort() throws java.io.IOExceptionDetects stream abort and notifies the watcher. There's not much to detect since this is called byabortConnection. The watcher will only be notified if this stream is aborted for the first time and before EOF has been detected or the stream has beenclosedgracefully. This stream will be detached from the underlying stream to prevent multiple notifications to the watcher.- Throws:
java.io.IOException- in case of an IO problem on closing the underlying stream
-
releaseConnection
public void releaseConnection() throws java.io.IOExceptionSame asclose().- Specified by:
releaseConnectionin interfaceConnectionReleaseTrigger- Throws:
java.io.IOException- in case of an IO problem. The connection will be released anyway.
-
abortConnection
public void abortConnection() throws java.io.IOExceptionAborts this stream. This is a special version ofclose()which prevents re-use of the underlying connection, if any. Calling this method indicates that there should be no attempt to read until the end of the stream.- Specified by:
abortConnectionin interfaceConnectionReleaseTrigger- Throws:
java.io.IOException- in case of an IO problem. The connection will be released anyway.
-
-