com.amazonaws.http
Class HttpMethodReleaseInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by com.amazonaws.internal.SdkInputStream
          extended by com.amazonaws.http.HttpMethodReleaseInputStream
All Implemented Interfaces:
com.amazonaws.internal.MetricAware, Closeable

public class HttpMethodReleaseInputStream
extends com.amazonaws.internal.SdkInputStream

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

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 the input stream. Additionally, this class attempts to release the connection and close the stream in this object's finalizer method, as a last resort to avoid leaking resources.

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


Constructor Summary
HttpMethodReleaseInputStream(org.apache.http.HttpEntityEnclosingRequest httpMethod)
          Constructs an input stream based on an HttpMethod object representing an HTTP connection.
 
Method Summary
 int available()
          Standard input stream available method, except it ensures that releaseConnection() is called if any errors are encountered from the wrapped stream.
 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.http.HttpEntityEnclosingRequest getHttpRequest()
          Returns the underlying HttpMethod object that contains/manages the actual HTTP connection.
protected  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 com.amazonaws.internal.SdkInputStream
abort, abortIfNeeded, isMetricActivated
 
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.http.HttpEntityEnclosingRequest 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 and makes that stream available. If no underlying connection is available, an empty ByteArrayInputStream is made available.

Parameters:
httpMethod - The HTTP method being executed, whose response content is to be wrapped.
Method Detail

getHttpRequest

public org.apache.http.HttpEntityEnclosingRequest getHttpRequest()
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 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:
IOException

read

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

Specified by:
read in class InputStream
Throws:
IOException
See Also:
InputStream.read()

read

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

Overrides:
read in class InputStream
Throws:
IOException
See Also:
InputStream.read(byte[], int, int)

available

public int available()
              throws IOException
Standard input stream available method, except it ensures that releaseConnection() is called if any errors are encountered from the wrapped stream.

Overrides:
available in class InputStream
Throws:
IOException
See Also:
InputStream.available()

close

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

Specified by:
close in interface Closeable
Overrides:
close in class InputStream
Throws:
IOException
See Also:
InputStream.close()

finalize

protected void finalize()
                 throws 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 Object
Throws:
Throwable

getWrappedInputStream

protected InputStream getWrappedInputStream()
Specified by:
getWrappedInputStream in class com.amazonaws.internal.SdkInputStream


Copyright © 2016. All rights reserved.