You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by re...@apache.org on 2020/02/12 21:16:58 UTC

[tomcat] branch 8.5.x updated: Add offset to the buffer remaining method

This is an automated email from the ASF dual-hosted git repository.

remm pushed a commit to branch 8.5.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/8.5.x by this push:
     new fa5b7d1  Add offset to the buffer remaining method
fa5b7d1 is described below

commit fa5b7d15004e5df5047c5f20b1fbddc091de6ad7
Author: remm <re...@apache.org>
AuthorDate: Wed Feb 12 21:59:30 2020 +0100

    Add offset to the buffer remaining method
    
    Add a zero length write to the existing test.
---
 java/org/apache/tomcat/util/net/Nio2Endpoint.java            | 12 ++++++------
 java/org/apache/tomcat/util/net/NioEndpoint.java             |  4 ++--
 java/org/apache/tomcat/util/net/SocketWrapperBase.java       |  6 +++---
 .../coyote/http11/upgrade/TestUpgradeInternalHandler.java    | 11 +++++++++++
 4 files changed, 22 insertions(+), 11 deletions(-)

diff --git a/java/org/apache/tomcat/util/net/Nio2Endpoint.java b/java/org/apache/tomcat/util/net/Nio2Endpoint.java
index d17adc0..9719b1d 100644
--- a/java/org/apache/tomcat/util/net/Nio2Endpoint.java
+++ b/java/org/apache/tomcat/util/net/Nio2Endpoint.java
@@ -644,7 +644,7 @@ public class Nio2Endpoint extends AbstractJsseEndpoint<Nio2Channel> {
                     synchronized (writeCompletionHandler) {
                         if (nBytes.longValue() < 0) {
                             failed(new EOFException(sm.getString("iob.failedwrite")), attachment);
-                        } else if (!nonBlockingWriteBuffer.isEmpty() || arrayHasData(attachment)) {
+                        } else if (!nonBlockingWriteBuffer.isEmpty() || buffersArrayHasRemaining(attachment, 0, attachment.length)) {
                             // Continue writing data using a gathering write
                             ByteBuffer[] array = nonBlockingWriteBuffer.toArray(attachment);
                             getSocket().write(array, 0, array.length,
@@ -993,17 +993,17 @@ public class Nio2Endpoint extends AbstractJsseEndpoint<Nio2Channel> {
                     if (!socketBufferHandler.isWriteBufferEmpty()) {
                         synchronized (writeCompletionHandler) {
                             socketBufferHandler.configureWriteBufferForRead();
-                            final ByteBuffer[] array = nonBlockingWriteBuffer.toArray(socketBufferHandler.getWriteBuffer());
-                            if (arrayHasData(array)) {
+                            ByteBuffer[] array = nonBlockingWriteBuffer.toArray(socketBufferHandler.getWriteBuffer());
+                            if (buffersArrayHasRemaining(array, 0, array.length)) {
                                 getSocket().write(array, 0, array.length, timeout, unit,
                                         array, new CompletionHandler<Long, ByteBuffer[]>() {
                                             @Override
                                             public void completed(Long nBytes, ByteBuffer[] buffers) {
                                                 if (nBytes.longValue() < 0) {
                                                     failed(new EOFException(), null);
-                                                } else if (arrayHasData(buffers)) {
-                                                    getSocket().write(array, 0, array.length, toTimeout(getWriteTimeout()),
-                                                            TimeUnit.MILLISECONDS, array, this);
+                                                } else if (buffersArrayHasRemaining(buffers, 0, buffers.length)) {
+                                                    getSocket().write(buffers, 0, buffers.length, toTimeout(getWriteTimeout()),
+                                                            TimeUnit.MILLISECONDS, buffers, this);
                                                 } else {
                                                     // Continue until everything is written
                                                     process();
diff --git a/java/org/apache/tomcat/util/net/NioEndpoint.java b/java/org/apache/tomcat/util/net/NioEndpoint.java
index fe6f65f..4e67f7e 100644
--- a/java/org/apache/tomcat/util/net/NioEndpoint.java
+++ b/java/org/apache/tomcat/util/net/NioEndpoint.java
@@ -1532,7 +1532,7 @@ public class NioEndpoint extends AbstractJsseEndpoint<NioChannel> {
                                     updateLastWrite();
                                 }
                             }
-                            if (nBytes != 0 || !arrayHasData(buffers)) {
+                            if (nBytes != 0 || !buffersArrayHasRemaining(buffers, offset, length)) {
                                 completionDone = false;
                             }
                         }
@@ -1540,7 +1540,7 @@ public class NioEndpoint extends AbstractJsseEndpoint<NioChannel> {
                         setError(e);
                     }
                 }
-                if (nBytes > 0 || (nBytes == 0 && !arrayHasData(buffers))) {
+                if (nBytes > 0 || (nBytes == 0 && !buffersArrayHasRemaining(buffers, offset, length))) {
                     // The bytes processed are only updated in the completion handler
                     completion.completed(Long.valueOf(nBytes), this);
                 } else if (nBytes < 0 || getError() != null) {
diff --git a/java/org/apache/tomcat/util/net/SocketWrapperBase.java b/java/org/apache/tomcat/util/net/SocketWrapperBase.java
index 3a859e7..c14471b 100644
--- a/java/org/apache/tomcat/util/net/SocketWrapperBase.java
+++ b/java/org/apache/tomcat/util/net/SocketWrapperBase.java
@@ -1426,9 +1426,9 @@ public abstract class SocketWrapperBase<E> {
         return max;
     }
 
-    protected static boolean arrayHasData(ByteBuffer[] byteBuffers) {
-        for (ByteBuffer byteBuffer : byteBuffers) {
-            if (byteBuffer.hasRemaining()) {
+    protected static boolean buffersArrayHasRemaining(ByteBuffer[] buffers, int offset, int length) {
+        for (int pos = offset; pos < offset + length; pos++) {
+            if (buffers[pos].hasRemaining()) {
                 return true;
             }
         }
diff --git a/test/org/apache/coyote/http11/upgrade/TestUpgradeInternalHandler.java b/test/org/apache/coyote/http11/upgrade/TestUpgradeInternalHandler.java
index babab0d..4f406fc 100644
--- a/test/org/apache/coyote/http11/upgrade/TestUpgradeInternalHandler.java
+++ b/test/org/apache/coyote/http11/upgrade/TestUpgradeInternalHandler.java
@@ -226,6 +226,17 @@ public class TestUpgradeInternalHandler extends TomcatBaseTest {
                 }
             }, buffer);
             System.out.println("CompletionState: " + state);
+            // Test zero length write used by websockets
+            state = wrapper.write(BlockingMode.BLOCK, 10, TimeUnit.SECONDS, null, SocketWrapperBase.COMPLETE_WRITE_WITH_COMPLETION, new CompletionHandler<Long, Void>() {
+                @Override
+                public void completed(Long result, Void attachment) {
+                    System.out.println("Write: " + result.longValue());
+                }
+                @Override
+                public void failed(Throwable exc, Void attachment) {
+                    exc.printStackTrace();
+                }
+            }, buffer);
         }
 
         @Override


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org