On Thu, 2010-04-08 at 14:18 -0400, James Leigh wrote:
> Hi all,
> 
> After looking through the javadocs and the source code it is not clear
> to me when writeTo is called if consumeContent also needs to be called.
> 

The choice of the method name is very unfortunate.
HttpEntity#consumeContent should have rather been called
HttpEntity#dispose or HttpEntity#finish.  

Actually the only requirement for proper release of resources held by an
entity is reading the content stream to the end (until a read operation
returns -1). So, any of these should be enough:

(1) HttpEntity#consumeContent()
(2) HttpEntity#writeTo()
(3) if (HttpEntity#getContent())
      HttpEntity#getContent().close()

I am really thinking about deprecating HttpEntity#consumeContent

> The javadoc for HttpEntity#isStreaming says this:
>         Tells whether this entity depends on an underlying stream.
>         Streamed entities should return true until the content has been
>         consumed, false afterwards. Self-contained entities should
>         return false. Wrapping entities should delegate this call to the
>         wrapped entity. 
>         
>         The content of a streamed entity is consumed when the stream
>         returned by getContent has been read to EOF, or after
>         consumeContent has been called. If a streamed entity can not
>         detect whether the stream has been read to EOF, it should return
>         true until consumeContent is called.
> 
> This would indicate that consumeContent should be called after writeTo,
> unless isStreaming returns false. The implementation for
> BasicHttpEntity#writeTo does not close the content stream, which is
> consistent with the above (it requires the caller to also call
> consumeContent).
> 
>         
>         // non-javadoc, see interface HttpEntity
>         public void writeTo(final OutputStream outstream) throws IOException {
>             if (outstream == null) {
>                 throw new IllegalArgumentException("Output stream may not be 
> null");
>             }
>             InputStream instream = getContent();
>             int l;
>             byte[] tmp = new byte[2048];
>             while ((l = instream.read(tmp)) != -1) {
>                 outstream.write(tmp, 0, l);
>             }
>         }
> 
> However, looking elsewhere in the source code, consumeContent is never
> called after writeTo. Here is a code sample from
> ThrottlingHttpClientHandler, but similar code can be found in
> EntitySerializer.
>         
>         HttpEntity entity = request.getEntity();
>         OutputStream outstream = new 
> ContentOutputStream(connState.getOutbuffer());
>         entity.writeTo(outstream);
>         outstream.flush();
>         outstream.close();
> 
> When should the content InputStream get closed? Is the bug in
> BasicHttpEntity#writeTo or ThrottlingHttpClientHandler and
> EntitySerializer?
> 

Ideally InputStream returned by HttpEntity#getContent() should always be
closed. This method will ensure the content is fully consumed and
resources held by the entity are properly released.

Oleg

> Thanks for your time,
> James
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to