org.apache.http.conn
Class EofSensorInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by org.apache.http.conn.EofSensorInputStream
All Implemented Interfaces:
Closeable, ConnectionReleaseTrigger

public class EofSensorInputStream
extends InputStream
implements 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:
EofSensorWatcher

Field Summary
protected  InputStream wrappedStream
          The wrapped input stream, while accessible.
 
Constructor Summary
EofSensorInputStream(InputStream in, EofSensorWatcher watcher)
          Creates a new EOF sensor.
 
Method Summary
 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()
           
protected  boolean isReadAllowed()
          Checks whether the underlying stream can be read from.
 int read()
           
 int read(byte[] b)
           
 int read(byte[] b, int off, int len)
           
 void releaseConnection()
          Same as close().
 
Methods inherited from class java.io.InputStream
mark, markSupported, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

wrappedStream

protected InputStream wrappedStream
The wrapped input stream, while accessible. The value changes to null when the wrapped stream becomes inaccessible.

Constructor Detail

EofSensorInputStream

public EofSensorInputStream(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 or close is called. Otherwise, the watcher decides whether the underlying stream should be closed before detaching from it.

Parameters:
in - the wrapped stream
watcher - the watcher for events, or null for auto-close behavior without notification
Method Detail

isReadAllowed

protected boolean isReadAllowed()
                         throws IOException
Checks whether the underlying stream can be read from.

Returns:
true if the underlying stream is accessible, false if this stream is in EOF mode and detached from the underlying stream
Throws:
IOException - if this stream is already closed

read

public int read()
         throws IOException
Specified by:
read in class InputStream
Throws:
IOException

read

public int read(byte[] b,
                int off,
                int len)
         throws IOException
Overrides:
read in class InputStream
Throws:
IOException

read

public int read(byte[] b)
         throws IOException
Overrides:
read in class InputStream
Throws:
IOException

available

public int available()
              throws IOException
Overrides:
available in class InputStream
Throws:
IOException

close

public void close()
           throws IOException
Specified by:
close in interface Closeable
Overrides:
close in class InputStream
Throws:
IOException

checkEOF

protected void checkEOF(int eof)
                 throws IOException
Detects EOF and notifies the watcher. This method should only be called while the underlying stream is still accessible. Use 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.

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

protected void checkClose()
                   throws IOException
Detects stream close and notifies the watcher. There's not much to detect since this is called by 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.

Throws:
IOException - in case of an IO problem on closing the underlying stream

checkAbort

protected void checkAbort()
                   throws IOException
Detects stream abort and notifies the watcher. There's not much to detect since this is called by 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.

Throws:
IOException - in case of an IO problem on closing the underlying stream

releaseConnection

public void releaseConnection()
                       throws IOException
Same as close().

Specified by:
releaseConnection in interface ConnectionReleaseTrigger
Throws:
IOException - in case of an IO problem. The connection will be released anyway.

abortConnection

public void abortConnection()
                     throws IOException
Aborts this stream. This is a special version of 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.

Specified by:
abortConnection in interface ConnectionReleaseTrigger
Throws:
IOException - in case of an IO problem. The connection will be released anyway.


Copyright © 1999–2019 The Apache Software Foundation. All rights reserved.