You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2011/05/10 16:20:41 UTC

svn commit: r1101469 - /httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/AsyncCharConsumer.java

Author: olegk
Date: Tue May 10 14:20:41 2011
New Revision: 1101469

URL: http://svn.apache.org/viewvc?rev=1101469&view=rev
Log:
Flush charset decoder when the message is fully consumed

Modified:
    httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/AsyncCharConsumer.java

Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/AsyncCharConsumer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/AsyncCharConsumer.java?rev=1101469&r1=1101468&r2=1101469&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/AsyncCharConsumer.java (original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/AsyncCharConsumer.java Tue May 10 14:20:41 2011
@@ -48,7 +48,7 @@ public abstract class AsyncCharConsumer<
     private final int bufSize;
     private String charsetName;
     private Charset charset;
-    private CharsetDecoder decoder;
+    private CharsetDecoder chardecoder;
     private ByteBuffer bbuf;
     private CharBuffer cbuf;
 
@@ -86,7 +86,7 @@ public abstract class AsyncCharConsumer<
             } catch (UnsupportedCharsetException ex) {
                 throw new UnsupportedEncodingException(this.charsetName);
             }
-            this.decoder = this.charset.newDecoder();
+            this.chardecoder = this.charset.newDecoder();
             this.bbuf = ByteBuffer.allocate(this.bufSize);
             this.cbuf = CharBuffer.allocate(this.bufSize);
         }
@@ -96,21 +96,33 @@ public abstract class AsyncCharConsumer<
                 break;
             }
             this.bbuf.flip();
-            CoderResult result = this.decoder.decode(this.bbuf, this.cbuf, decoder.isCompleted());
-            if (result.isError()) {
-                result.throwException();
+            boolean completed = decoder.isCompleted();
+            CoderResult result = this.chardecoder.decode(this.bbuf, this.cbuf, completed);
+            handleDecodingResult(result, ioctrl);
+            this.bbuf.compact();
+            if (completed) {
+                result = this.chardecoder.flush(this.cbuf);
+                handleDecodingResult(result, ioctrl);
             }
-            this.cbuf.flip();
+        }
+    }
+
+    private void handleDecodingResult(
+            final CoderResult result, final IOControl ioctrl) throws IOException {
+        if (result.isError()) {
+            result.throwException();
+        }
+        this.cbuf.flip();
+        if (this.cbuf.hasRemaining()) {
             onCharReceived(this.cbuf, ioctrl);
-            this.cbuf.clear();
-            this.bbuf.clear();
         }
+        this.cbuf.clear();
     }
 
     @Override
     void releaseResources() {
         this.charset = null;
-        this.decoder = null;
+        this.chardecoder = null;
         this.bbuf = null;
         this.cbuf = null;
         super.releaseResources();