hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r517372 - in /jakarta/httpcomponents/httpcore/trunk/module-nio/src: main/java/org/apache/http/impl/nio/codecs/ChunkDecoder.java test/java/org/apache/http/impl/nio/codecs/TestChunkDecoder.java
Date Mon, 12 Mar 2007 20:23:25 GMT
Author: olegk
Date: Mon Mar 12 13:23:24 2007
New Revision: 517372

URL: http://svn.apache.org/viewvc?view=rev&rev=517372
Log:
Code optimization in the ChunkDecoder class: attempt to read data from the channel only if
the session buffer is empty or next chunk is expected

Modified:
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/ChunkDecoder.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/codecs/TestChunkDecoder.java

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/ChunkDecoder.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/ChunkDecoder.java?view=diff&rev=517372&r1=517371&r2=517372
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/ChunkDecoder.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/ChunkDecoder.java
Mon Mar 12 13:23:24 2007
@@ -150,14 +150,16 @@
             return -1;
         }
         
-        int bytesRead = this.buffer.fill(this.channel);
-        if (bytesRead == -1) {
-            this.endOfStream = true;
-        }
-        
         int totalRead = 0;
         while (this.state != COMPLETED) {
 
+            if (!this.buffer.hasData() || this.chunkSize == -1) {
+                int bytesRead = this.buffer.fill(this.channel);
+                if (bytesRead == -1) {
+                    this.endOfStream = true;
+                }
+            }
+            
             switch (this.state) {
             case READ_CONTENT:
                 
@@ -173,6 +175,7 @@
                     }
                     if (this.chunkSize == 0) {
                         // Last chunk. Read footers
+                        this.chunkSize = -1;
                         this.state = READ_FOOTERS;
                         break;
                     }

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/codecs/TestChunkDecoder.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/codecs/TestChunkDecoder.java?view=diff&rev=517372&r1=517371&r2=517372
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/codecs/TestChunkDecoder.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/codecs/TestChunkDecoder.java
Mon Mar 12 13:23:24 2007
@@ -110,8 +110,15 @@
         ChunkDecoder decoder = new ChunkDecoder(channel, inbuf);
         
         ByteBuffer dst = ByteBuffer.allocate(1024); 
+
+        int bytesRead = 0;
+        while (dst.hasRemaining() && !decoder.isCompleted()) {
+            int i = decoder.read(dst);
+            if (i > 0) {
+                bytesRead += i;
+            }
+        }
         
-        int bytesRead = decoder.read(dst);
         assertEquals(26, bytesRead);
         assertEquals("12345678901234561234512345", convert(dst));
         
@@ -137,31 +144,24 @@
         SessionInputBuffer inbuf = new SessionInputBuffer(1024, 256); 
         ChunkDecoder decoder = new ChunkDecoder(channel, inbuf);
         
-        ByteBuffer dst = ByteBuffer.allocate(4); 
-        
-        int bytesRead = decoder.read(dst);
-        assertEquals(4, bytesRead);
-        assertEquals("0123", convert(dst));
-        assertFalse(decoder.isCompleted());
-
-        dst.clear();
-        bytesRead = decoder.read(dst);
-        assertEquals(4, bytesRead);
-        assertEquals("4567", convert(dst));
-        assertFalse(decoder.isCompleted());
-
-        dst.clear();
-        bytesRead = decoder.read(dst);
-        assertEquals(4, bytesRead);
-        assertEquals("89ab", convert(dst));
-        assertFalse(decoder.isCompleted());
+        ByteBuffer dst = ByteBuffer.allocate(1024); 
+        ByteBuffer tmp = ByteBuffer.allocate(4); 
 
-        dst.clear();
-        bytesRead = decoder.read(dst);
-        assertEquals(4, bytesRead);
-        assertEquals("cdef", convert(dst));
-        assertTrue(decoder.isCompleted());
+        int bytesRead = 0;
+        while (dst.hasRemaining() && !decoder.isCompleted()) {
+            int i = decoder.read(tmp);
+            if (i > 0) {
+                bytesRead += i;
+            }
+            tmp.flip();
+            dst.put(tmp);
+            tmp.compact();
+        }
         
+        assertEquals(16, bytesRead);
+        assertEquals("0123456789abcdef", convert(dst));
+        assertTrue(decoder.isCompleted());
+
         dst.clear();
         bytesRead = decoder.read(dst);
         assertEquals(-1, bytesRead);
@@ -187,52 +187,19 @@
         ByteBuffer dst = ByteBuffer.allocate(1024); 
         
         ChunkDecoder decoder = new ChunkDecoder(channel, inbuf);
-        int bytesRead = decoder.read(dst);
-        assertEquals(0, bytesRead);
-        assertFalse(decoder.isCompleted());
         
-        bytesRead = decoder.read(dst);
-        assertEquals(0, bytesRead);
-        assertFalse(decoder.isCompleted());
-
-        bytesRead = decoder.read(dst);
-        assertEquals(15, bytesRead);
-        assertEquals("123456789012345", convert(dst));
-        assertFalse(decoder.isCompleted());
-
-        dst.clear();
-        bytesRead = decoder.read(dst);
-        assertEquals(3, bytesRead);
-        assertEquals("612", convert(dst));
-        assertFalse(decoder.isCompleted());
-
-        dst.clear();
-        bytesRead = decoder.read(dst);
-        assertEquals(3, bytesRead);
-        assertEquals("345", convert(dst));
-        assertFalse(decoder.isCompleted());
-
-        dst.clear();
-        bytesRead = decoder.read(dst);
-        assertEquals(6, bytesRead);
-        assertEquals("abcdef", convert(dst));
-        assertFalse(decoder.isCompleted());
-
-        dst.clear();
-        bytesRead = decoder.read(dst);
-        assertEquals(0, bytesRead);
-        assertFalse(decoder.isCompleted());
-
-        dst.clear();
-        bytesRead = decoder.read(dst);
-        assertEquals(0, bytesRead);
-        assertFalse(decoder.isCompleted());
-
-        dst.clear();
-        bytesRead = decoder.read(dst);
-        assertEquals(0, bytesRead);
+        int bytesRead = 0;
+        while (dst.hasRemaining() && !decoder.isCompleted()) {
+            int i = decoder.read(dst);
+            if (i > 0) {
+                bytesRead += i;
+            }
+        }
+        
+        assertEquals(27, bytesRead);
+        assertEquals("123456789012345612345abcdef", convert(dst));
         assertTrue(decoder.isCompleted());
-
+        
         Header[] footers = decoder.getFooters();
         assertEquals(2, footers.length);
         assertEquals("Footer1", footers[0].getName());
@@ -332,14 +299,17 @@
         ChunkDecoder decoder = new ChunkDecoder(channel, inbuf);
         
         ByteBuffer dst = ByteBuffer.allocate(1024); 
+
+        int bytesRead = 0;
+        while (dst.hasRemaining() && !decoder.isCompleted()) {
+            int i = decoder.read(dst);
+            if (i > 0) {
+                bytesRead += i;
+            }
+        }
         
-        int bytesRead = decoder.read(dst);
         assertEquals(26, bytesRead);
         assertEquals("12345678901234561234512345", convert(dst));
-        assertFalse(decoder.isCompleted());
-        
-        bytesRead = decoder.read(dst);
-        assertEquals(0, bytesRead);
         assertTrue(decoder.isCompleted());
     }
 
@@ -353,14 +323,17 @@
         ChunkDecoder decoder = new ChunkDecoder(channel, inbuf);
         
         ByteBuffer dst = ByteBuffer.allocate(1024); 
+
+        int bytesRead = 0;
+        while (dst.hasRemaining() && !decoder.isCompleted()) {
+            int i = decoder.read(dst);
+            if (i > 0) {
+                bytesRead += i;
+            }
+        }
         
-        int bytesRead = decoder.read(dst);
         assertEquals(26, bytesRead);
         assertEquals("12345678901234561234512345", convert(dst));
-        assertFalse(decoder.isCompleted());
-        
-        bytesRead = decoder.read(dst);
-        assertEquals(0, bytesRead);
         assertTrue(decoder.isCompleted());
     }
 



Mime
View raw message