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 2013/12/25 14:57:28 UTC

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

Author: olegk
Date: Wed Dec 25 13:57:28 2013
New Revision: 1553382

URL: http://svn.apache.org/r1553382
Log:
Re-implementation of the HTTPASYNC-40 fix involving an exteremely ugly work-around, a better solution requires API changes

Modified:
    httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/AsyncByteConsumer.java
    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/AsyncByteConsumer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/AsyncByteConsumer.java?rev=1553382&r1=1553381&r2=1553382&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/AsyncByteConsumer.java (original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/AsyncByteConsumer.java Wed Dec 25 13:57:28 2013
@@ -28,12 +28,15 @@ package org.apache.http.nio.client.metho
 
 import java.io.IOException;
 import java.nio.ByteBuffer;
+import java.nio.channels.SelectionKey;
 
 import org.apache.http.HttpEntity;
 import org.apache.http.entity.ContentType;
 import org.apache.http.nio.ContentDecoder;
 import org.apache.http.nio.IOControl;
+import org.apache.http.nio.conn.ManagedNHttpClientConnection;
 import org.apache.http.nio.protocol.AbstractAsyncResponseConsumer;
+import org.apache.http.nio.reactor.IOSession;
 import org.apache.http.util.Asserts;
 
 /**
@@ -79,11 +82,26 @@ public abstract class AsyncByteConsumer<
     protected final void onContentReceived(
             final ContentDecoder decoder, final IOControl ioctrl) throws IOException {
         Asserts.notNull(this.bbuf, "Byte buffer");
-        final int bytesRead = decoder.read(this.bbuf);
-        if (bytesRead > 0) {
+
+        //FIXME: IOControl needs to expose event mask in order to avoid this extreme ugliness
+        final IOSession iosession;
+        if (ioctrl instanceof ManagedNHttpClientConnection) {
+            final ManagedNHttpClientConnection conn = (ManagedNHttpClientConnection) ioctrl;
+            iosession = conn != null ? conn.getIOSession() : null;
+        } else {
+            iosession = null;
+        }
+        for (;;) {
+            final int bytesRead = decoder.read(this.bbuf);
+            if (bytesRead <= 0) {
+                break;
+            }
             this.bbuf.flip();
             onByteReceived(this.bbuf, ioctrl);
             this.bbuf.clear();
+            if (iosession != null && (iosession.getEventMask() & SelectionKey.OP_READ) == 0) {
+                break;
+            }
         }
     }
 

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=1553382&r1=1553381&r2=1553382&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 Wed Dec 25 13:57:28 2013
@@ -29,6 +29,7 @@ package org.apache.http.nio.client.metho
 import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.nio.CharBuffer;
+import java.nio.channels.SelectionKey;
 import java.nio.charset.Charset;
 import java.nio.charset.CharsetDecoder;
 import java.nio.charset.CoderResult;
@@ -37,7 +38,9 @@ import org.apache.http.HttpEntity;
 import org.apache.http.entity.ContentType;
 import org.apache.http.nio.ContentDecoder;
 import org.apache.http.nio.IOControl;
+import org.apache.http.nio.conn.ManagedNHttpClientConnection;
 import org.apache.http.nio.protocol.AbstractAsyncResponseConsumer;
+import org.apache.http.nio.reactor.IOSession;
 import org.apache.http.protocol.HTTP;
 import org.apache.http.util.Asserts;
 
@@ -94,8 +97,20 @@ public abstract class AsyncCharConsumer<
     protected final void onContentReceived(
             final ContentDecoder decoder, final IOControl ioctrl) throws IOException {
         Asserts.notNull(this.bbuf, "Byte buffer");
-        final int bytesRead = decoder.read(this.bbuf);
-        if (bytesRead > 0) {
+
+        //FIXME: IOControl needs to expose event mask in order to avoid this extreme ugliness
+        final IOSession iosession;
+        if (ioctrl instanceof ManagedNHttpClientConnection) {
+            final ManagedNHttpClientConnection conn = (ManagedNHttpClientConnection) ioctrl;
+            iosession = conn != null ? conn.getIOSession() : null;
+        } else {
+            iosession = null;
+        }
+        for (;;) {
+            final int bytesRead = decoder.read(this.bbuf);
+            if (bytesRead <= 0) {
+                break;
+            }
             this.bbuf.flip();
             final boolean completed = decoder.isCompleted();
             CoderResult result = this.chardecoder.decode(this.bbuf, this.cbuf, completed);
@@ -105,6 +120,9 @@ public abstract class AsyncCharConsumer<
                 result = this.chardecoder.flush(this.cbuf);
                 handleDecodingResult(result, ioctrl);
             }
+            if (iosession != null && (iosession.getEventMask() & SelectionKey.OP_READ) == 0) {
+                break;
+            }
         }
     }