Android
org.apache.http.conn
public class

org.apache.http.conn.EofSensorInputStream

java.lang.Object
java.io.InputStream Closeable
org.apache.http.conn.EofSensorInputStream ConnectionReleaseTrigger

A stream wrapper that triggers actions on 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.

See Also

Summary

Fields

protected      InputStream  wrappedStream  The wrapped input stream, while accessible. 

Public Constructors

            EofSensorInputStream(InputStream in, EofSensorWatcher watcher)
Creates a new EOF sensor.

Public Methods

          void  abortConnection()
Aborts this stream.
          int  available()
Returns a int representing then number of bytes that are available before this InputStream will block.
          void  close()
Close the InputStream.
          int  read(byte[] b, int off, int len)
Reads at most length bytes from the Stream and stores them in byte array b starting at offset.
          int  read(byte[] b)
Reads bytes from the Stream and stores them in byte array b.
          int  read()
Reads a single byte from this InputStream and returns the result as an int.
          void  releaseConnection()
Same as close().

Protected Methods

          void  checkAbort()
Detects stream abort and notifies the watcher.
          void  checkClose()
Detects stream close and notifies the watcher.
          void  checkEOF(int eof)
Detects EOF and notifies the watcher.
          boolean  isReadAllowed()
Checks whether the underlying stream can be read from.
Methods inherited from class java.io.InputStream
Methods inherited from class java.lang.Object
Methods inherited from interface java.io.Closeable
Methods inherited from interface org.apache.http.conn.ConnectionReleaseTrigger

Details

Fields

protected InputStream wrappedStream

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

Public Constructors

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

Public Methods

public void abortConnection()

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.

Throws

IOException

public int available()

Returns a int representing then number of bytes that are available before this InputStream will block. This method always returns 0. Subclasses should override and indicate the correct number of bytes available.

Returns

  • the number of bytes available before blocking.

Throws

IOException

public void close()

Close the InputStream. Concrete implementations of this class should free any resources during close. This implementation does nothing.

Throws

IOException

public int read(byte[] b, int off, int len)

Reads at most length bytes from the Stream and stores them in byte array b starting at offset. Answer the number of bytes actually read or -1 if no bytes were read and end of stream was encountered.

Parameters

b the byte array in which to store the read bytes.
off the offset in b to store the read bytes.
len the maximum number of bytes to store in b.

Returns

  • the number of bytes actually read or -1 if end of stream.

Throws

IOException

public int read(byte[] b)

Reads bytes from the Stream and stores them in byte array b. Answer the number of bytes actually read or -1 if no bytes were read and end of stream was encountered.

Parameters

b the byte array in which to store the read bytes.

Returns

  • the number of bytes actually read or -1 if end of stream.

Throws

IOException

public int read()

Reads a single byte from this InputStream and returns the result as an int. The low-order byte is returned or -1 of the end of stream was encountered. This abstract implementation must be provided by concrete subclasses.

Returns

  • the byte read or -1 if end of stream.

Throws

IOException

public void releaseConnection()

Same as close().

Throws

IOException

Protected Methods

protected void checkAbort()

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

protected void checkClose()

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

protected void checkEOF(int eof)

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

protected boolean isReadAllowed()

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
Copyright 2007 Google Inc. Build 0.9_r1-98467 - 14 Aug 2008 18:56