Package org.apache.http.conn
Class EofSensorInputStream
java.lang.Object
java.io.InputStream
org.apache.http.conn.EofSensorInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable,ConnectionReleaseTrigger
A stream wrapper that triggers actions on
close() 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:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final EofSensorWatcherThe watcher to be notified, if any.private booleanIndicates whether this stream itself is closed.protected InputStreamThe wrapped input stream, while accessible. -
Constructor Summary
ConstructorsConstructorDescriptionEofSensorInputStream(InputStream in, EofSensorWatcher watcher) Creates a new EOF sensor. -
Method Summary
Modifier and TypeMethodDescriptionvoidAborts this stream.intprotected voidDetects stream abort and notifies the watcher.protected voidDetects stream close and notifies the watcher.protected voidcheckEOF(int eof) Detects EOF and notifies the watcher.voidclose()(package private) InputStreamprotected booleanChecks whether the underlying stream can be read from.(package private) booleanintread()intread(byte[] b) intread(byte[] b, int off, int len) voidSame asclose().Methods inherited from class java.io.InputStream
mark, markSupported, reset, skip
-
Field Details
-
wrappedStream
The wrapped input stream, while accessible. The value changes tonullwhen the wrapped stream becomes inaccessible. -
selfClosed
private boolean selfClosedIndicates 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:
-
eofWatcher
The watcher to be notified, if any.
-
-
Constructor Details
-
EofSensorInputStream
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 Details
-
isSelfClosed
boolean isSelfClosed() -
getWrappedStream
InputStream getWrappedStream() -
isReadAllowed
Checks 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:
IOException- if this stream is already closed
-
read
- Specified by:
readin classInputStream- Throws:
IOException
-
read
- Overrides:
readin classInputStream- Throws:
IOException
-
read
- Overrides:
readin classInputStream- Throws:
IOException
-
available
- Overrides:
availablein classInputStream- Throws:
IOException
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException
-
checkEOF
Detects 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:
IOException- in case of an IO problem on closing the underlying stream
-
checkClose
Detects 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:
IOException- in case of an IO problem on closing the underlying stream
-
checkAbort
Detects 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:
IOException- in case of an IO problem on closing the underlying stream
-
releaseConnection
Same asclose().- Specified by:
releaseConnectionin interfaceConnectionReleaseTrigger- Throws:
IOException- in case of an IO problem. The connection will be released anyway.
-
abortConnection
Aborts 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:
IOException- in case of an IO problem. The connection will be released anyway.
-