hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oleg Kalnichevski <ol...@apache.org>
Subject Re: Guidance on the use of EntityUtils.consume
Date Tue, 06 Dec 2011 23:09:21 GMT
On Tue, 2011-12-06 at 17:32 -0500, Todd W Lainhart wrote:
> Thanks.  I'm reacting to an argument that the "isStreaming()" 
> interface/implementation is superfluous.  That if "getContent()" returns 
> non-null, that you should should be able to safely call close() on it. The 
> same argument then  takes the position that there shouldn't be the need to 
> implement "isStreaming()", which presents the opportunity to get it wrong.
> 
> I'm looking for the counter argument where the following would be wrong:
> 
> if (isStreaming() == false && getContent() !== null)
>      getContent().close();
> 
> 

Todd

There are also non-streaming entities such as those backed by an object
rather than a stream (StringEntity, ByteArrayEntity, FileEntity).
Non-streaming entities are self-contained and do not need to be consumed
in order to ensure release of resources (unlike those backed by a stream
from a socket of an open connection).

As long as you obtain an InputStream from an entity by calling
#getContent() you _must_ close that stream. The #isStreaming() check is
a safe-guard to prevent unnecessary creation of an InputStream instance
in case the entity is not backed by a data stream from a socket.

Hope this helps

Oleg


> 
> 
> Todd Lainhart
> Rational software
> IBM Corporation
> 550 King Street, Littleton, MA 01460-1250
> 1-978-899-4705
> 2-276-4705 (T/L)
> lainhart@us.ibm.com
> 
> 
> 
> 
> From:   William Speirs <wspeirs@apache.org>
> To:     HttpClient User Discussion <httpclient-users@hc.apache.org>
> Date:   12/06/2011 05:11 PM
> Subject:        Re: Guidance on the use of EntityUtils.consume
> 
> 
> 
> That was the reason I posted the code... it is the same thing.
> 
> Bill-
> On Dec 6, 2011 4:54 PM, "Todd W Lainhart" <lainhart@us.ibm.com> wrote:
> 
> > Understood re the deprecation, but note the javadoc that I quoted which
> > describes two different approaches when reacting to the deprecation. 
> That
> > was the heart of my question.
> >
> >
> >
> >
> > Todd Lainhart
> > Rational software
> > IBM Corporation
> > 550 King Street, Littleton, MA 01460-1250
> > 1-978-899-4705
> > 2-276-4705 (T/L)
> > lainhart@us.ibm.com
> >
> >
> >
> >
> > From:   William Speirs <wspeirs@apache.org>
> > To:     HttpClient User Discussion <httpclient-users@hc.apache.org>
> > Date:   12/06/2011 04:46 PM
> > Subject:        Re: Guidance on the use of EntityUtils.consume
> >
> >
> >
> > As per the documentation, HttpEntity.consumeContent() is deprecated.
> > You should use EntityUtils.consume() instead. The code for that is
> > straightforward:
> >
> >    public static void consume(final HttpEntity entity) throws 
> IOException
> > {
> >        if (entity == null) {
> >            return;
> >        }
> >        if (entity.isStreaming()) {
> >            InputStream instream = entity.getContent();
> >            if (instream != null) {
> >                instream.close();
> >            }
> >        }
> >    }
> >
> > Bill-
> > On Tue, Dec 6, 2011 at 2:29 PM, Todd W Lainhart <lainhart@us.ibm.com>
> > wrote:
> > > The javadoc for HttpEntity.consumeContent() is copied below.  I can't
> > tell
> > > if best practice is to use EntityUtils.consume, or just close the 
> input
> > > stream associated to the entity.  I'm guessing the latter, but there 
> may
> > > be an idiom that I'm missing.
> > >
> > >    /**
> > >     * This method is deprecated since version 4.1. Please use standard
> > >     * java convention to ensure resource deallocation by calling
> > >     * {@link InputStream#close()} on the input stream returned by
> > >     * {@link #getContent()}
> > >     * <p>
> > >     * This method is called to indicate that the content of this 
> entity
> > >     * is no longer required. All entity implementations are expected 
> to
> > >     * release all allocated resources as a result of this method
> > >     * invocation. Content streaming entities are also expected to
> > >     * dispose of the remaining content, if any. Wrapping entities 
> should
> > >     * delegate this call to the wrapped entity.
> > >     * <p>
> > >     * This method is of particular importance for entities being
> > >     * received from a {@link HttpConnection connection}. The entity
> > >     * needs to be consumed completely in order to re-use the 
> connection
> > >     * with keep-alive.
> > >     *
> > >     * @throws IOException if an I/O error occurs.
> > >     *
> > >     * @deprecated Use {@link
> > > org.apache.http.util.EntityUtils#consume(HttpEntity)}
> > >     *
> > >     * @see #getContent() and #writeTo(OutputStream)
> > >     */
> > >
> > >
> > >
> > >  -- Todd
> > >
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> > > For additional commands, e-mail: httpclient-users-help@hc.apache.org
> > >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> > For additional commands, e-mail: httpclient-users-help@hc.apache.org
> >
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> > For additional commands, e-mail: httpclient-users-help@hc.apache.org
> >
> >
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Mime
View raw message