public class EofSensorInputStream extends InputStream implements ConnectionReleaseTrigger
close()
and EOF.
Primarily used to auto-release an underlying managed connection when the response
body is consumed or no longer needed.EofSensorWatcher
Modifier and Type | Field and Description |
---|---|
private EofSensorWatcher |
eofWatcher
The watcher to be notified, if any.
|
private boolean |
selfClosed
Indicates whether this stream itself is closed.
|
protected InputStream |
wrappedStream
The wrapped input stream, while accessible.
|
Constructor and Description |
---|
EofSensorInputStream(InputStream in,
EofSensorWatcher watcher)
Creates a new EOF sensor.
|
Modifier and Type | Method and Description |
---|---|
void |
abortConnection()
Aborts this stream.
|
int |
available() |
protected void |
checkAbort()
Detects stream abort and notifies the watcher.
|
protected void |
checkClose()
Detects stream close and notifies the watcher.
|
protected void |
checkEOF(int eof)
Detects EOF and notifies the watcher.
|
void |
close() |
(package private) InputStream |
getWrappedStream() |
protected boolean |
isReadAllowed()
Checks whether the underlying stream can be read from.
|
(package private) boolean |
isSelfClosed() |
int |
read() |
int |
read(byte[] b) |
int |
read(byte[] b,
int off,
int len) |
void |
releaseConnection()
Same as
close() . |
mark, markSupported, reset, skip
protected InputStream wrappedStream
null
when the wrapped stream
becomes inaccessible.private boolean selfClosed
wrappedStream
is null
, 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 an IOException
.isReadAllowed
private final EofSensorWatcher eofWatcher
public EofSensorInputStream(InputStream in, EofSensorWatcher watcher)
close
is called.
Otherwise, the watcher decides whether the underlying stream
should be closed before detaching from it.in
- the wrapped streamwatcher
- the watcher for events, or null
for
auto-close behavior without notificationboolean isSelfClosed()
InputStream getWrappedStream()
protected boolean isReadAllowed() throws IOException
true
if the underlying stream is accessible,
false
if this stream is in EOF mode and
detached from the underlying streamIOException
- if this stream is already closedpublic int read() throws IOException
read
in class InputStream
IOException
public int read(byte[] b, int off, int len) throws IOException
read
in class InputStream
IOException
public int read(byte[] b) throws IOException
read
in class InputStream
IOException
public int available() throws IOException
available
in class InputStream
IOException
public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
close
in class InputStream
IOException
protected void checkEOF(int eof) throws IOException
isReadAllowed
to
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.
eof
- the result of the calling read operation.
A negative value indicates that EOF is reached.IOException
- in case of an IO problem on closing the underlying streamprotected void checkClose() throws IOException
close
.
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.IOException
- in case of an IO problem on closing the underlying streamprotected void checkAbort() throws IOException
abortConnection
.
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 been closed
gracefully.
This stream will be detached from the underlying stream to prevent
multiple notifications to the watcher.IOException
- in case of an IO problem on closing the underlying streampublic void releaseConnection() throws IOException
close()
.releaseConnection
in interface ConnectionReleaseTrigger
IOException
- in case of an IO problem. The connection will be released
anyway.public void abortConnection() throws IOException
close()
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.abortConnection
in interface ConnectionReleaseTrigger
IOException
- in case of an IO problem.
The connection will be released anyway.WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses