[prev in list] [next in list] [prev in thread] [next in thread] 

List:       httpcomponents-commits
Subject:    svn commit: r634796 - in /httpcomponents/httpcore/trunk: RELEASE_NOTES.txt
From:       olegk () apache ! org
Date:       2008-03-07 20:07:55
Message-ID: 20080307200806.74A371A9832 () eris ! apache ! org
[Download RAW message or body]

Author: olegk
Date: Fri Mar  7 12:07:53 2008
New Revision: 634796

URL: http://svn.apache.org/viewvc?rev=634796&view=rev
Log:
HTTPCORE-92: ChunkEncoder splits input data larger than available space in the \
session output buffer into smaller chunks instead of expanding the buffer.  \
Contributed by Andrea Selva <selva.andrea at gmail.com> and Oleg Kalnichevski <olegk \
at apache.org> 


Modified:
    httpcomponents/httpcore/trunk/RELEASE_NOTES.txt
    httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/ChunkEncoder.java
  httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/codecs/TestChunkEncoder.java


Modified: httpcomponents/httpcore/trunk/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/RELEASE_NOTES.txt?rev=634796&r1=634795&r2=634796&view=diff
 ==============================================================================
--- httpcomponents/httpcore/trunk/RELEASE_NOTES.txt (original)
+++ httpcomponents/httpcore/trunk/RELEASE_NOTES.txt Fri Mar  7 12:07:53 2008
@@ -1,6 +1,12 @@
 Changes since 4.0 Beta 1
 -------------------
 
+* [HTTPCORE-92] ChunkEncoder splits input data larger than available space 
+  in the session output buffer into smaller chunks instead of expanding 
+  the buffer. 
+  Contributed by Andrea Selva <selva.andrea at gmail.com> and 
+  Oleg Kalnichevski <olegk at apache.org> 
+
 * [HTTPCORE-149] I/O reactors now count period of inactivity since the
   time of the last read or write operation. Previously only read 
   operations resulted in timeout counter reset.
@@ -89,7 +95,7 @@
   Contributed by Oleg Kalnichevski <olegk at apache.org>
 
 * [HTTPCORE-121] new interface HeaderElementIterator
-  Contributed by Andrea Selva <selva.andre at gmail.com>
+  Contributed by Andrea Selva <selva.andrea at gmail.com>
 
 * [HTTPCORE-123] Fixed problem with SSLSession losing buffered data, if the 
   connection has been closed by the peer.

Modified: httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/ChunkEncoder.java
                
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/module-nio/src/main/ja \
va/org/apache/http/impl/nio/codecs/ChunkEncoder.java?rev=634796&r1=634795&r2=634796&view=diff
 ==============================================================================
--- httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/ChunkEncoder.java \
                (original)
+++ httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/ChunkEncoder.java \
Fri Mar  7 12:07:53 2008 @@ -37,18 +37,22 @@
 
 import org.apache.http.impl.io.HttpTransportMetricsImpl;
 import org.apache.http.nio.reactor.SessionOutputBuffer;
+import org.apache.http.nio.util.BufferInfo;
 import org.apache.http.util.CharArrayBuffer;
 
 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 {
@@ -60,10 +64,36 @@
         if (chunk == 0) {
             return 0;
         }
-        this.lineBuffer.clear();
-        this.lineBuffer.append(Integer.toHexString(chunk));
-        this.buffer.writeLine(this.lineBuffer);
-        this.buffer.write(src);
+        
+        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 the longest chunk header
+        // 12345678\r\n
+        avail -= 10;
+
+        if (avail < chunk) {
+            // write no more than 'avail' bytes
+            this.lineBuffer.clear();
+            this.lineBuffer.append(Integer.toHexString(avail));
+            this.buffer.writeLine(this.lineBuffer);
+            int oldlimit = src.limit();
+            src.limit(src.position() + avail);
+            this.buffer.write(src);
+            src.limit(oldlimit);
+        } else {
+            // write all
+            this.lineBuffer.clear();
+            this.lineBuffer.append(Integer.toHexString(chunk));
+            this.buffer.writeLine(this.lineBuffer);
+            this.buffer.write(src);
+        }
         this.lineBuffer.clear();
         this.buffer.writeLine(this.lineBuffer);
         return chunk;

Modified: httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/codecs/TestChunkEncoder.java
                
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/module-nio/src/test/ja \
va/org/apache/http/impl/nio/codecs/TestChunkEncoder.java?rev=634796&r1=634795&r2=634796&view=diff
 ==============================================================================
--- httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/codecs/TestChunkEncoder.java \
                (original)
+++ httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/codecs/TestChunkEncoder.java \
Fri Mar  7 12:07:53 2008 @@ -100,6 +100,52 @@
         assertTrue(encoder.isCompleted());
         assertEquals("5\r\n12345\r\n3\r\n678\r\n2\r\n90\r\n0\r\n\r\n", s);
     }
+    
+    public void testChunkNoExceed() throws Exception {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
+        WritableByteChannel channel = newChannel(baos);
+        HttpParams params = new BasicHttpParams();
+        SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 16, params);
+        HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
+        ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics);
+        encoder.write(wrap("1234"));
+        encoder.complete();
+        
+        outbuf.flush(channel);
+        
+        String s = baos.toString("US-ASCII");
+        
+        assertTrue(encoder.isCompleted());
+        assertEquals("4\r\n1234\r\n0\r\n\r\n", s);    
+    }
+    
+    
+    public void testChunkExceed() throws Exception {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
+        WritableByteChannel channel = newChannel(baos);
+        HttpParams params = new BasicHttpParams();
+        SessionOutputBuffer outbuf = new SessionOutputBufferImpl(16, 16, params);
+        HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
+        ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics);
+        
+        ByteBuffer src = wrap("0123456789ABCDEF");
+        
+        encoder.write(src);
+        assertTrue(src.hasRemaining());
+        assertEquals(10, src.remaining());
+
+        encoder.write(src);
+        assertTrue(src.hasRemaining());
+        assertEquals(4, src.remaining());
+
+        encoder.write(src);
+        assertFalse(src.hasRemaining());
+        
+        outbuf.flush(channel);
+        String s = baos.toString("US-ASCII");
+        assertEquals("6\r\n012345\r\n6\r\n6789AB\r\n4\r\nCDEF\r\n", s);    
+        
+    }
 
     public void testCodingEmptyBuffer() throws Exception {
         ByteArrayOutputStream baos = new ByteArrayOutputStream(); 


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic