Class EofSensorInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable,ConnectionReleaseTrigger
public class EofSensorInputStream extends InputStream implements ConnectionReleaseTrigger
close() and EOF.
Primarily used to auto-release an underlying
connection
when the response body is consumed or no longer needed.
This class is based on AutoCloseInputStream in HttpClient 3.1,
but has notable differences. It does not allow mark/reset, distinguishes
different kinds of event, and does not always close the underlying stream
on EOF. That decision is left to the watcher.
- Since:
- 4.0
- Version:
- $Revision: 672367 $
- Author:
- Roland Weber, Ortwin Glueck, Eric Johnson, Mike Bowler
- See Also:
EofSensorWatcher
-
Field Summary
Fields Modifier and Type Field Description protected InputStreamwrappedStreamThe wrapped input stream, while accessible. -
Constructor Summary
Constructors Constructor Description EofSensorInputStream(InputStream in, EofSensorWatcher watcher)Creates a new EOF sensor. -
Method Summary
Modifier and Type Method Description voidabortConnection()Aborts this stream.intavailable()Returns an estimated number of bytes that can be read or skipped without blocking for more input.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()Closes this stream.protected booleanisReadAllowed()Checks whether the underlying stream can be read from.intread()Reads a single byte from this stream and returns it as an integer in the range from 0 to 255.intread(byte[] b)Equivalent toread(buffer, 0, buffer.length).intread(byte[] b, int off, int len)Reads up tobyteCountbytes from this stream and stores them in the byte arraybufferstarting atbyteOffset.voidreleaseConnection()Same 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.
-
-
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
-
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
Description copied from class:InputStreamReads a single byte from this stream and returns it as an integer in the range from 0 to 255. Returns -1 if the end of the stream has been reached. Blocks until one byte has been read, the end of the source stream is detected or an exception is thrown.- Specified by:
readin classInputStream- Throws:
IOException- if the stream is closed or another IOException occurs.
-
read
Description copied from class:InputStreamReads up tobyteCountbytes from this stream and stores them in the byte arraybufferstarting atbyteOffset. Returns the number of bytes actually read or -1 if the end of the stream has been reached.- Overrides:
readin classInputStream- Throws:
IOException- if the stream is closed or another IOException occurs.
-
read
Description copied from class:InputStreamEquivalent toread(buffer, 0, buffer.length).- Overrides:
readin classInputStream- Throws:
IOException
-
available
Description copied from class:InputStreamReturns an estimated number of bytes that can be read or skipped without blocking for more input.Note that this method provides such a weak guarantee that it is not very useful in practice.
Firstly, the guarantee is "without blocking for more input" rather than "without blocking": a read may still block waiting for I/O to complete — the guarantee is merely that it won't have to wait indefinitely for data to be written. The result of this method should not be used as a license to do I/O on a thread that shouldn't be blocked.
Secondly, the result is a conservative estimate and may be significantly smaller than the actual number of bytes available. In particular, an implementation that always returns 0 would be correct. In general, callers should only use this method if they'd be satisfied with treating the result as a boolean yes or no answer to the question "is there definitely data ready?".
Thirdly, the fact that a given number of bytes is "available" does not guarantee that a read or skip will actually read or skip that many bytes: they may read or skip fewer.
It is particularly important to realize that you must not use this method to size a container and assume that you can read the entirety of the stream without needing to resize the container. Such callers should probably write everything they read to a
ByteArrayOutputStreamand convert that to a byte array. Alternatively, if you're reading from a file,File.length()returns the current length of the file (though assuming the file's length can't change may be incorrect, reading a file is inherently racy).The default implementation of this method in
InputStreamalways returns 0. Subclasses should override this method if they are able to indicate the number of bytes available.- Overrides:
availablein classInputStream- Returns:
- the estimated number of bytes available
- Throws:
IOException- if this stream is closed or an error occurs
-
close
Description copied from class:InputStreamCloses this stream. Concrete implementations of this class should free any resources during close. This implementation does nothing.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException- if an error occurs while closing this stream.
-
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.
-