hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r234333 - in /jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io: NIOHttpDataReceiver.java NIOHttpDataTransmitter.java
Date Sun, 21 Aug 2005 20:01:59 GMT
Author: olegk
Date: Sun Aug 21 13:01:54 2005
New Revision: 234333

URL: http://svn.apache.org/viewcvs?rev=234333&view=rev
Log:
Changed to reuse char buffer when reading / writing lines of text

Modified:
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/NIOHttpDataReceiver.java
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/NIOHttpDataTransmitter.java

Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/NIOHttpDataReceiver.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/NIOHttpDataReceiver.java?rev=234333&r1=234332&r2=234333&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/NIOHttpDataReceiver.java
(original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/NIOHttpDataReceiver.java
Sun Aug 21 13:01:54 2005
@@ -58,7 +58,8 @@
     private ByteBuffer buffer = null;
     
     private Charset charset = null;
-    private CharsetDecoder chardecoder;
+    private CharsetDecoder chardecoder = null;
+    private CharBuffer chbuffer = null;
     
     protected void initBuffer(int buffersize) {
         if (buffersize < 2048) {
@@ -68,6 +69,7 @@
         this.buffer.flip();
         this.charset = Charset.forName("US-ASCII");
         this.chardecoder = createCharDecoder();
+        this.chbuffer = CharBuffer.allocate(1024);
     }
 
     public void reset(final HttpParams params) {
@@ -148,9 +150,9 @@
     
     public String readLine() throws IOException {
         int noRead = 0;
+        this.chbuffer.clear();
         this.chardecoder.reset();
         StringBuffer line = new StringBuffer(); 
-        CharBuffer tmp = CharBuffer.allocate(1024);
         boolean retry = true;
         while (retry) {
             // attempt to find end of line (LF)
@@ -162,11 +164,13 @@
                 int origLimit = this.buffer.limit();
                 this.buffer.limit(i + 1);
                 for (;;) {
-                    CoderResult result = this.chardecoder.decode(this.buffer, tmp, true);
+                    CoderResult result = this.chardecoder.decode(
+                    		this.buffer, this.chbuffer, true);
                     if (result.isOverflow()) {
-                        tmp.flip();
-                        line.append(tmp.array(), tmp.position(), tmp.remaining());
-                        tmp.clear();
+                        this.chbuffer.flip();
+                        line.append(this.chbuffer.array(), 
+                        		this.chbuffer.position(), this.chbuffer.remaining());
+                        this.chbuffer.clear();
                     }
                     if (result.isUnderflow()) {
                         break;
@@ -177,29 +181,31 @@
                 // end of line not found
                 if (this.buffer.hasRemaining()) {
                     // decode the entire buffer content
-                    this.chardecoder.decode(this.buffer, tmp, false);
+                    this.chardecoder.decode(this.buffer, this.chbuffer, false);
                 }
                 // discard the decoded content
                 noRead = fillBuffer();
                 if (noRead == -1) {
                     retry = false;
                     // terminate the decoding process
-                    this.chardecoder.decode(this.buffer, tmp, true);
+                    this.chardecoder.decode(this.buffer, this.chbuffer, true);
                 }
             }
             // append the decoded content to the line buffer
-            tmp.flip();
-            if (tmp.hasRemaining()) {
-                line.append(tmp.array(), tmp.position(), tmp.remaining());
+            this.chbuffer.flip();
+            if (this.chbuffer.hasRemaining()) {
+                line.append(this.chbuffer.array(), 
+                		this.chbuffer.position(), this.chbuffer.remaining());
             }
-            tmp.clear();
+            this.chbuffer.clear();
         }
         // flush the decoder
-        this.chardecoder.flush(tmp);
-        tmp.flip();
+        this.chardecoder.flush(this.chbuffer);
+        this.chbuffer.flip();
         // append the decoded content to the line buffer
-        if (tmp.hasRemaining()) {
-            line.append(tmp.array(), tmp.position(), tmp.remaining());
+        if (this.chbuffer.hasRemaining()) {
+            line.append(this.chbuffer.array(), 
+            		this.chbuffer.position(), this.chbuffer.remaining());
         }
         if (noRead == -1 && line.length() == 0) {
             // indicate the end of stream

Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/NIOHttpDataTransmitter.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/NIOHttpDataTransmitter.java?rev=234333&r1=234332&r2=234333&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/NIOHttpDataTransmitter.java
(original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/io/NIOHttpDataTransmitter.java
Sun Aug 21 13:01:54 2005
@@ -60,6 +60,7 @@
 
     private Charset charset = null;
     private CharsetEncoder charencoder = null;
+    private CharBuffer chbuffer = null;
 
     protected void initBuffer(int buffersize) {
         if (buffersize < 2048) {
@@ -68,6 +69,7 @@
         this.buffer = ByteBuffer.allocateDirect(buffersize);
         this.charset = Charset.forName("US-ASCII");
         this.charencoder = createCharEncoder();
+        this.chbuffer = CharBuffer.allocate(1024);
     }
     
     public void reset(final HttpParams params) {
@@ -171,22 +173,21 @@
         // Do not bother if the string is empty
         if (s.length() > 0 ) {
         	this.charencoder.reset();
-            CharBuffer tmp = CharBuffer.allocate(1024);
             // transfer the string in small chunks
             int remaining = s.length();
             int offset = 0;
             while (remaining > 0) {
-                int l = tmp.remaining();
+                int l = this.chbuffer.remaining();
                 boolean eol = false;
                 if (remaining < l) {
                     l = remaining;
                     // terminate the encoding process
                     eol = true;
                 }
-                tmp.put(s, offset, offset + l);
-                tmp.flip();
-                write(this.charencoder, tmp, eol);
-                tmp.compact();
+                this.chbuffer.put(s, offset, offset + l);
+                this.chbuffer.flip();
+                write(this.charencoder, this.chbuffer, eol);
+                this.chbuffer.compact();
                 offset += l;
                 remaining -= l;
             }



Mime
View raw message