hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r327848 - in /jakarta/httpclient/trunk/http-core/src: java/org/apache/http/util/HeadersParser.java test/org/apache/http/util/TestHeadersParser.java
Date Sun, 23 Oct 2005 19:54:37 GMT
Author: olegk
Date: Sun Oct 23 12:54:27 2005
New Revision: 327848

URL: http://svn.apache.org/viewcvs?rev=327848&view=rev
Log:
GC optimization. Use CharArrayBuffer-s internally to reduce the amount of garbage generated
when parsing HTTP headers

Modified:
    jakarta/httpclient/trunk/http-core/src/java/org/apache/http/util/HeadersParser.java
    jakarta/httpclient/trunk/http-core/src/test/org/apache/http/util/TestHeadersParser.java

Modified: jakarta/httpclient/trunk/http-core/src/java/org/apache/http/util/HeadersParser.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-core/src/java/org/apache/http/util/HeadersParser.java?rev=327848&r1=327847&r2=327848&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-core/src/java/org/apache/http/util/HeadersParser.java (original)
+++ jakarta/httpclient/trunk/http-core/src/java/org/apache/http/util/HeadersParser.java Sun
Oct 23 12:54:27 2005
@@ -55,31 +55,46 @@
     public static Header[] processHeaders(final HttpDataReceiver datareceiver) 
             throws HttpException, IOException {
         ArrayList headerLines = new ArrayList();
+        
+        CharArrayBuffer current = null;
+        CharArrayBuffer previous = null;
         for (;;) {
-            String line = datareceiver.readLine();
-            if ((line == null) || (line.length() < 1)) {
+            if (current == null) {
+                current = new CharArrayBuffer(64);
+            } else {
+                current.clear();
+            }
+            int l = datareceiver.readLine(current);
+            if (l == -1 || current.length() < 1) {
                 break;
             }
             // Parse the header name and value
             // Check for folded headers first
             // Detect LWS-char see HTTP/1.0 or HTTP/1.1 Section 2.2
             // discussion on folded headers
-            if ((line.charAt(0) == ' ' || line.charAt(0) == '\t') && !headerLines.isEmpty())
{
+            if ((current.charAt(0) == ' ' || current.charAt(0) == '\t') && previous
!= null) {
                 // we have continuation folded header
                 // so append value
-                String previousLine = (String) headerLines.remove(headerLines.size() - 1);
-                CharArrayBuffer buffer = new CharArrayBuffer(128);
-                buffer.append(previousLine);
-                buffer.append(' ');
-                buffer.append(line.trim());
-                headerLines.add(buffer.toString());
+                int i = 0;
+                while (i < current.length()) {
+                    char ch = current.charAt(i);
+                    if (ch != ' ' && ch != '\t') {
+                        break;
+                    }
+                    i++;
+                }
+                previous.append(' ');
+                previous.append(current.getBuffer(), i, current.length() - i);
             } else {
-                headerLines.add(line.trim());
+                headerLines.add(current);
+                previous = current;
+                current = null;
             }
         }
         Header[] headers = new Header[headerLines.size()];
         for (int i = 0; i < headerLines.size(); i++) {
-            headers[i] = Header.parse((String) headerLines.get(i));
+            CharArrayBuffer buffer = (CharArrayBuffer) headerLines.get(i);
+            headers[i] = Header.parse(buffer.toString());
         }
         return headers;
     }

Modified: jakarta/httpclient/trunk/http-core/src/test/org/apache/http/util/TestHeadersParser.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-core/src/test/org/apache/http/util/TestHeadersParser.java?rev=327848&r1=327847&r2=327848&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-core/src/test/org/apache/http/util/TestHeadersParser.java
(original)
+++ jakarta/httpclient/trunk/http-core/src/test/org/apache/http/util/TestHeadersParser.java
Sun Oct 23 12:54:27 2005
@@ -64,6 +64,7 @@
             "header3: stuff\r\n" + 
             "     and more stuff\r\n" + 
             "\t and even more stuff\r\n" +  
+            "     \r\n" +  
             "\r\n"; 
         HttpDataReceiver receiver = new HttpDataReceiverMockup(s, "US-ASCII"); 
         Header[] headers = HeadersParser.processHeaders(receiver);



Mime
View raw message