You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Sebastiaan van Erk <se...@sebster.com> on 2008/04/02 21:55:04 UTC

ChunkedOutputStream and flushing

Hi,

I have a question on the chunked output stream: one of the nice things 
about chunked encoding is that you can send your data in little parts. 
However, flush() on ChunkedOutputStream does not flush the buffer:

     /**
      * Flushes the underlying stream, but leaves the internal buffer alone.
      * @throws IOException
      */
     public void flush() throws IOException {
         this.out.flush();
     }

This is kind of against the spec of flush on OutputStream:

     /**
      * Flushes this output stream and forces any buffered output bytes
      * to be written out. The general contract of <code>flush</code> is
      * that calling it is an indication that, if any bytes previously
      * written have been buffered by the implementation of the output
      * stream, such bytes should immediately be written to their
      * intended destination.
      * ...

I was wondering what the motivation was behind disabling the flush() 
option? Generally if a client does not want to flush the data they won't 
call flush, which will cause all the chunks to attain the full chunk 
size anyway...

Regards,
Sebastiaan


Re: ChunkedOutputStream and flushing

Posted by Sebastiaan van Erk <se...@sebster.com>.
Ok, thanks for the reply.

https://issues.apache.org/jira/browse/HTTPCORE-157

I have a workaround (simply subclass ChunkedOutputStream and overwrite 
flush to call flushCache() followed by super.flush()), and subclassing 
the EntitySerializer to use it for chunked encoding. Then I create 
content manager like so:

final ClientConnectionManager connectionManager = new 
ThreadSafeClientConnManager(httpParams, schemeRegistry) {
	@Override
	protected ClientConnectionOperator createConnectionOperator(final 
SchemeRegistry schemeRegistry) {
		return new DefaultClientConnectionOperator(schemeRegistry) {
			@Override
			public OperatedClientConnection createConnection() {
				return new DefaultClientConnection() {
					@Override
					protected EntitySerializer createEntitySerializer() {
						return new MyEntitySerializer(new StrictContentLengthStrategy());
					}
				};
			}
		};
	}
};

I guess this is the right way to install a different entity serializer?

Anyway, it works for now. :)

Regards,
Sebastiaan

Oleg Kalnichevski wrote:
> On Wed, 2008-04-02 at 21:55 +0200, Sebastiaan van Erk wrote:
>> Hi,
>>
>> I have a question on the chunked output stream: one of the nice things 
>> about chunked encoding is that you can send your data in little parts. 
>> However, flush() on ChunkedOutputStream does not flush the buffer:
>>
>>      /**
>>       * Flushes the underlying stream, but leaves the internal buffer alone.
>>       * @throws IOException
>>       */
>>      public void flush() throws IOException {
>>          this.out.flush();
>>      }
>>
>> This is kind of against the spec of flush on OutputStream:
>>
>>      /**
>>       * Flushes this output stream and forces any buffered output bytes
>>       * to be written out. The general contract of <code>flush</code> is
>>       * that calling it is an indication that, if any bytes previously
>>       * written have been buffered by the implementation of the output
>>       * stream, such bytes should immediately be written to their
>>       * intended destination.
>>       * ...
>>
>> I was wondering what the motivation was behind disabling the flush() 
>> option?
> 
> To avoid tiny chunks if for some reason the output stream gets flushed
> too often. 
> 
> Feel free to open a bug in JIRA for this issue.
> 
> Oleg
> 
>>  Generally if a client does not want to flush the data they won't 
>> call flush, which will cause all the chunks to attain the full chunk 
>> size anyway...
>>
>> Regards,
>> Sebastiaan
>>
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
> 

Re: ChunkedOutputStream and flushing

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Wed, 2008-04-02 at 21:55 +0200, Sebastiaan van Erk wrote:
> Hi,
> 
> I have a question on the chunked output stream: one of the nice things 
> about chunked encoding is that you can send your data in little parts. 
> However, flush() on ChunkedOutputStream does not flush the buffer:
> 
>      /**
>       * Flushes the underlying stream, but leaves the internal buffer alone.
>       * @throws IOException
>       */
>      public void flush() throws IOException {
>          this.out.flush();
>      }
> 
> This is kind of against the spec of flush on OutputStream:
> 
>      /**
>       * Flushes this output stream and forces any buffered output bytes
>       * to be written out. The general contract of <code>flush</code> is
>       * that calling it is an indication that, if any bytes previously
>       * written have been buffered by the implementation of the output
>       * stream, such bytes should immediately be written to their
>       * intended destination.
>       * ...
> 
> I was wondering what the motivation was behind disabling the flush() 
> option?

To avoid tiny chunks if for some reason the output stream gets flushed
too often. 

Feel free to open a bug in JIRA for this issue.

Oleg

>  Generally if a client does not want to flush the data they won't 
> call flush, which will cause all the chunks to attain the full chunk 
> size anyway...
> 
> Regards,
> Sebastiaan
> 


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