Author: markt
Date: Wed Aug 15 15:21:12 2018
New Revision: 1838106

URL: http://svn.apache.org/viewvc?rev=1838106&view=rev
Log:
Reduce code duplication

Modified:
    tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Endpoint.java
    tomcat/trunk/java/org/apache/tomcat/util/net/WriteBuffer.java

Modified: tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Endpoint.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Endpoint.java?rev=1838106&r1=1838105&r2=1838106&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Endpoint.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Endpoint.java Wed Aug 15 
15:21:12 2018
@@ -32,8 +32,6 @@ import java.nio.channels.CompletionHandl
 import java.nio.channels.FileChannel;
 import java.nio.channels.NetworkChannel;
 import java.nio.file.StandardOpenOption;
-import java.util.ArrayList;
-import java.util.List;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Future;
@@ -577,11 +575,7 @@ public class Nio2Endpoint extends Abstra
                         } else if (!writeBuffer.isEmpty()) {
                             nestedWriteCompletionCount.get().incrementAndGet();
                             // Continue writing data using a gathering write
-                            List<ByteBuffer> arrayList = new ArrayList<>();
-                            if (attachment.hasRemaining()) {
-                                arrayList.add(attachment);
-                            }
-                            ByteBuffer[] array = 
writeBuffer.transferToListAsArray(arrayList);
+                            ByteBuffer[] array = 
writeBuffer.toArray(attachment);
                             getSocket().write(array, 0, array.length,
                                     toNio2Timeout(getWriteTimeout()), 
TimeUnit.MILLISECONDS,
                                     array, gatheringWriteCompletionHandler);
@@ -628,15 +622,9 @@ public class Nio2Endpoint extends Abstra
                         if (nBytes.longValue() < 0) {
                             failed(new 
EOFException(sm.getString("iob.failedwrite")), attachment);
                         } else if (!writeBuffer.isEmpty() || 
arrayHasData(attachment)) {
-                            // Continue writing data
+                            // Continue writing data using a gathering write
                             nestedWriteCompletionCount.get().incrementAndGet();
-                            List<ByteBuffer> arrayList = new ArrayList<>();
-                            for (ByteBuffer buffer : attachment) {
-                                if (buffer.hasRemaining()) {
-                                    arrayList.add(buffer);
-                                }
-                            }
-                            ByteBuffer[] array = 
writeBuffer.transferToListAsArray(arrayList);
+                            ByteBuffer[] array = 
writeBuffer.toArray(attachment);
                             getSocket().write(array, 0, array.length,
                                     toNio2Timeout(getWriteTimeout()), 
TimeUnit.MILLISECONDS,
                                     array, gatheringWriteCompletionHandler);
@@ -1287,12 +1275,7 @@ public class Nio2Endpoint extends Abstra
                 if (hasPermit || writePending.tryAcquire()) {
                     socketBufferHandler.configureWriteBufferForRead();
                     if (!writeBuffer.isEmpty()) {
-                        // Gathering write of the main buffer plus all 
leftovers
-                        List<ByteBuffer> arrayList = new ArrayList<>();
-                        if 
(socketBufferHandler.getWriteBuffer().hasRemaining()) {
-                            
arrayList.add(socketBufferHandler.getWriteBuffer());
-                        }
-                        ByteBuffer[] array = 
writeBuffer.transferToListAsArray(arrayList);
+                        ByteBuffer[] array = 
writeBuffer.toArray(socketBufferHandler.getWriteBuffer());
                         Nio2Endpoint.startInline();
                         getSocket().write(array, 0, array.length, 
toNio2Timeout(getWriteTimeout()),
                                 TimeUnit.MILLISECONDS, array, 
gatheringWriteCompletionHandler);

Modified: tomcat/trunk/java/org/apache/tomcat/util/net/WriteBuffer.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/net/WriteBuffer.java?rev=1838106&r1=1838105&r2=1838106&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/net/WriteBuffer.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/net/WriteBuffer.java Wed Aug 15 
15:21:12 2018
@@ -18,6 +18,7 @@ package org.apache.tomcat.util.net;
 
 import java.io.IOException;
 import java.nio.ByteBuffer;
+import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 import java.util.concurrent.LinkedBlockingDeque;
@@ -71,13 +72,27 @@ public class WriteBuffer {
     }
 
 
-    ByteBuffer[] transferToListAsArray(List<ByteBuffer> target) {
+    /**
+     * Create an array of ByteBuffers from the current WriteBuffer, prefixing
+     * that array with the provided ByteBuffers.
+     *
+     * @param prefixes The additional ByteBuffers to add to the start of the
+     *                 array
+     *
+     * @return an array of ByteBuffers from the current WriteBuffer prefixed by
+     *         the provided ByteBuffers
+     */
+    ByteBuffer[] toArray(ByteBuffer... prefixes) {
+        List<ByteBuffer> result = new ArrayList<>();
+        for (ByteBuffer prefix : prefixes) {
+            result.add(prefix);
+        }
         for (ByteBufferHolder buffer : buffers) {
             buffer.flip();
-            target.add(buffer.getBuf());
+            result.add(buffer.getBuf());
         }
         buffers.clear();
-        return target.toArray(new ByteBuffer[target.size()]);
+        return result.toArray(new ByteBuffer[result.size()]);
     }
 
 



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

Reply via email to