org.jets3t.service.impl.rest.httpclient
Class HttpMethodReleaseInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by org.jets3t.service.impl.rest.httpclient.HttpMethodReleaseInputStream
All Implemented Interfaces:
java.io.Closeable, InputStreamWrapper

public class HttpMethodReleaseInputStream
extends java.io.InputStream
implements InputStreamWrapper

Utility class to wrap InputStreams obtained from an HttpClient library's HttpMethod object, and ensure the stream and HTTP connection is cleaned up properly.

This input stream wrapper is used to ensure that input streams obtained through HttpClient connections are cleaned up correctly once the caller has read all the contents of the connection's input stream, or closed that input stream.

Important! This input stream must be completely consumed or closed to ensure the necessary cleanup operations can be performed.


Constructor Summary
HttpMethodReleaseInputStream(org.apache.commons.httpclient.HttpMethod httpMethod)
          Constructs an input stream based on an HttpMethod object representing an HTTP connection.
 
Method Summary
 int available()
           
 void close()
          Standard input stream close method, except it ensures that releaseConnection() is called before the input stream is closed.
protected  void finalize()
          Tries to ensure a connection is always cleaned-up correctly by calling releaseConnection() on class destruction if the cleanup hasn't already been done.
 org.apache.commons.httpclient.HttpMethod getHttpMethod()
          Returns the underlying HttpMethod object that contains/manages the actual HTTP connection.
 java.io.InputStream getWrappedInputStream()
           
 int read()
          Standard input stream read method, except it calls releaseConnection() when the underlying input stream is consumed.
 int read(byte[] b, int off, int len)
          Standard input stream read method, except it calls releaseConnection() when the underlying input stream is consumed.
protected  void releaseConnection()
          Forces the release of an HttpMethod's connection in a way that will perform all the necessary cleanup through the correct use of HttpClient methods.
 
Methods inherited from class java.io.InputStream
mark, markSupported, read, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HttpMethodReleaseInputStream

public HttpMethodReleaseInputStream(org.apache.commons.httpclient.HttpMethod httpMethod)
Constructs an input stream based on an HttpMethod object representing an HTTP connection. If a connection input stream is available, this constructor wraps the underlying input stream in an InterruptableInputStream and makes that stream available. If no underlying connection is available, an empty ByteArrayInputStream is made available.

Parameters:
httpMethod -
Method Detail

getHttpMethod

public org.apache.commons.httpclient.HttpMethod getHttpMethod()
Returns the underlying HttpMethod object that contains/manages the actual HTTP connection.

Returns:
the HTTPMethod object that provides the data input stream.

releaseConnection

protected void releaseConnection()
                          throws java.io.IOException
Forces the release of an HttpMethod's connection in a way that will perform all the necessary cleanup through the correct use of HttpClient methods.

Throws:
java.io.IOException

read

public int read()
         throws java.io.IOException
Standard input stream read method, except it calls releaseConnection() when the underlying input stream is consumed.

Specified by:
read in class java.io.InputStream
Throws:
java.io.IOException

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
Standard input stream read method, except it calls releaseConnection() when the underlying input stream is consumed.

Overrides:
read in class java.io.InputStream
Throws:
java.io.IOException

available

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

close

public void close()
           throws java.io.IOException
Standard input stream close method, except it ensures that releaseConnection() is called before the input stream is closed.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.InputStream
Throws:
java.io.IOException

finalize

protected void finalize()
                 throws java.lang.Throwable
Tries to ensure a connection is always cleaned-up correctly by calling releaseConnection() on class destruction if the cleanup hasn't already been done.

This desperate cleanup act will only be necessary if the user of this class does not completely consume or close this input stream prior to object destruction. This method will log Warning messages if a forced cleanup is required, hopefully reminding the user to close their streams properly.

Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable

getWrappedInputStream

public java.io.InputStream getWrappedInputStream()
Specified by:
getWrappedInputStream in interface InputStreamWrapper
Returns:
the underlying input stream wrapped by this class.