[ 
https://issues.apache.org/jira/browse/HTTPCORE-92?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12572068#action_12572068
 ] 

Oleg Kalnichevski commented on HTTPCORE-92:
-------------------------------------------

Hi Andrea

No, we cannot add methods to interfaces at this point. We'll have to work this 
problem around by using an additional interface. 

All buffer primitives in HttpCore NIO now additionally implement this interface:
http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/util/BufferInfo.java

The patch still does not do what I think it should. Moreover, ChunkEncoder can 
now enter a busy loop when trying to write a large chunk of data (I guess this 
is the cause of the test failures you have been seeing). The main point: do not 
try to write _all_ data at once. Write only as much as it can fit into the 
session output buffer. That's it.

===
public class ChunkEncoder extends AbstractContentEncoder {
    
    private final CharArrayBuffer lineBuffer;
    private final BufferInfo bufferinfo; 
    
    public ChunkEncoder(
            final WritableByteChannel channel, 
            final SessionOutputBuffer buffer,
            final HttpTransportMetricsImpl metrics) {
        super(channel, buffer, metrics);
        this.lineBuffer = new CharArrayBuffer(16);
        this.bufferinfo = (BufferInfo) buffer;
    }

    public int write(final ByteBuffer src) throws IOException {
        if (src == null) {
            return 0;
        }
        assertNotCompleted();
        int chunk = src.remaining();
        if (chunk == 0) {
            return 0;
        }
        long bytesWritten = this.buffer.flush(this.channel);
        if (bytesWritten > 0) {
            this.metrics.incrementBytesTransferred(bytesWritten);
        }
        int avail = this.bufferinfo.available();
        if (avail == 0) {
            return 0;
        }
        
        // subtract the length of an average chunk header
        // 0x12345678\r\n
        avail -= 12;
        
        boolean partial = avail < chunk; 
        
        if (partial) {
            // write no more than 'avail' bytes
        } else {
            // write all 
        }
===

Oleg

> NIO ChunkEncoder does not flush the session output buffer
> ---------------------------------------------------------
>
>                 Key: HTTPCORE-92
>                 URL: https://issues.apache.org/jira/browse/HTTPCORE-92
>             Project: HttpComponents HttpCore
>          Issue Type: Improvement
>          Components: HttpCore NIO
>            Reporter: Oleg Kalnichevski
>             Fix For: 4.0-beta2
>
>         Attachments: possiblefix_92.patch, write_reworked.txt
>
>
> Present implementation of the NIO ChunkEncoder is over-simplistic. It always 
> expands the session output buffer to store more content instead of trying to 
> free up space by flushing the buffered content.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to