You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2014/11/10 17:47:19 UTC

svn commit: r1637934 - in /tomcat/trunk/java/org/apache: coyote/http11/upgrade/NioServletOutputStream.java tomcat/util/net/NioEndpoint.java

Author: markt
Date: Mon Nov 10 16:47:19 2014
New Revision: 1637934

URL: http://svn.apache.org/r1637934
Log:
Push write methods down to SocketWrapper for NIO

Modified:
    tomcat/trunk/java/org/apache/coyote/http11/upgrade/NioServletOutputStream.java
    tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java

Modified: tomcat/trunk/java/org/apache/coyote/http11/upgrade/NioServletOutputStream.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/coyote/http11/upgrade/NioServletOutputStream.java?rev=1637934&r1=1637933&r2=1637934&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/coyote/http11/upgrade/NioServletOutputStream.java (original)
+++ tomcat/trunk/java/org/apache/coyote/http11/upgrade/NioServletOutputStream.java Mon Nov 10 16:47:19 2014
@@ -17,118 +17,28 @@
 package org.apache.coyote.http11.upgrade;
 
 import java.io.IOException;
-import java.nio.channels.SelectionKey;
-import java.nio.channels.Selector;
 
 import org.apache.tomcat.util.net.NioChannel;
-import org.apache.tomcat.util.net.NioEndpoint;
-import org.apache.tomcat.util.net.NioSelectorPool;
+import org.apache.tomcat.util.net.NioEndpoint.NioSocketWrapper;
 import org.apache.tomcat.util.net.SocketWrapperBase;
 
 public class NioServletOutputStream extends AbstractServletOutputStream<NioChannel> {
 
-    private final NioChannel channel;
-    private final NioSelectorPool pool;
-    private final int maxWrite;
-
-
     public NioServletOutputStream(SocketWrapperBase<NioChannel> socketWrapper,
             int asyncWriteBufferSize) {
         super(socketWrapper, asyncWriteBufferSize);
-        channel = socketWrapper.getSocket();
-        this.pool = ((NioEndpoint) socketWrapper.getEndpoint()).getSelectorPool();
-        maxWrite = channel.getBufHandler().getWriteBuffer().capacity();
     }
 
 
     @Override
     protected int doWrite(boolean block, byte[] b, int off, int len)
             throws IOException {
-        int leftToWrite = len;
-        int count = 0;
-        int offset = off;
-
-        while (leftToWrite > 0) {
-            int writeThisLoop;
-            int writtenThisLoop;
-
-            if (leftToWrite > maxWrite) {
-                writeThisLoop = maxWrite;
-            } else {
-                writeThisLoop = leftToWrite;
-            }
-
-            writtenThisLoop = doWriteInternal(block, b, offset, writeThisLoop);
-            count += writtenThisLoop;
-            offset += writtenThisLoop;
-            leftToWrite -= writtenThisLoop;
-
-            if (writtenThisLoop < writeThisLoop) {
-                break;
-            }
-        }
-
-        return count;
-    }
-
-    private int doWriteInternal (boolean block, byte[] b, int off, int len)
-            throws IOException {
-        channel.getBufHandler().getWriteBuffer().clear();
-        channel.getBufHandler().getWriteBuffer().put(b, off, len);
-        channel.getBufHandler().getWriteBuffer().flip();
-
-        int written = 0;
-        NioEndpoint.NioSocketWrapper att =
-                (NioEndpoint.NioSocketWrapper) channel.getAttachment(false);
-        if (att == null) {
-            throw new IOException("Key must be cancelled");
-        }
-        long writeTimeout = att.getWriteTimeout();
-        Selector selector = null;
-        try {
-            selector = pool.get();
-        } catch ( IOException x ) {
-            //ignore
-        }
-        try {
-            written = pool.write(channel.getBufHandler().getWriteBuffer(),
-                    channel, selector, writeTimeout, block);
-        } finally {
-            if (selector != null) {
-                pool.put(selector);
-            }
-        }
-        if (written < len) {
-            channel.getPoller().add(channel, SelectionKey.OP_WRITE);
-        }
-        return written;
+        return ((NioSocketWrapper) socketWrapper).write(block, b, off, len);
     }
 
 
     @Override
     protected void doFlush() throws IOException {
-        NioEndpoint.NioSocketWrapper att =
-                (NioEndpoint.NioSocketWrapper) channel.getAttachment(false);
-        if (att == null) {
-            throw new IOException("Key must be cancelled");
-        }
-        long writeTimeout = att.getWriteTimeout();
-        Selector selector = null;
-        try {
-            selector = pool.get();
-        } catch ( IOException x ) {
-            //ignore
-        }
-        try {
-            do {
-                if (channel.flush(true, selector, writeTimeout)) {
-                    break;
-                }
-            } while (true);
-        } finally {
-            if (selector != null) {
-                pool.put(selector);
-            }
-        }
+        ((NioSocketWrapper) socketWrapper).flush();
     }
 }

Modified: tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java?rev=1637934&r1=1637933&r2=1637934&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java Mon Nov 10 16:47:19 2014
@@ -1319,9 +1319,11 @@ public class NioEndpoint extends Abstrac
         }
     }
 
-// ----------------------------------------------------- Key Attachment Class
+    // ---------------------------------------------------- Key Attachment Class
     public static class NioSocketWrapper extends SocketWrapperBase<NioChannel> {
 
+        private final int maxWrite;
+
         private Poller poller = null;
         private int interestOps = 0;
         private boolean callBackNotify = false;
@@ -1332,6 +1334,7 @@ public class NioEndpoint extends Abstrac
 
         public NioSocketWrapper(NioChannel channel, NioEndpoint endpoint) {
             super(channel, endpoint);
+            maxWrite = channel.getBufHandler().getWriteBuffer().capacity();
         }
 
         public void reset(Poller poller, NioChannel channel, long soTimeout) {
@@ -1513,8 +1516,101 @@ public class NioEndpoint extends Abstrac
             return nRead;
         }
 
+
+        public int write(boolean block, byte[] b, int off, int len)
+                throws IOException {
+            int leftToWrite = len;
+            int count = 0;
+            int offset = off;
+
+            while (leftToWrite > 0) {
+                int writeThisLoop;
+                int writtenThisLoop;
+
+                if (leftToWrite > maxWrite) {
+                    writeThisLoop = maxWrite;
+                } else {
+                    writeThisLoop = leftToWrite;
+                }
+
+                writtenThisLoop = writeInternal(block, b, offset, writeThisLoop);
+                count += writtenThisLoop;
+                offset += writtenThisLoop;
+                leftToWrite -= writtenThisLoop;
+
+                if (writtenThisLoop < writeThisLoop) {
+                    break;
+                }
+            }
+
+            return count;
+        }
+
+
+        private int writeInternal (boolean block, byte[] b, int off, int len)
+                throws IOException {
+            getSocket().getBufHandler().getWriteBuffer().clear();
+            getSocket().getBufHandler().getWriteBuffer().put(b, off, len);
+            getSocket().getBufHandler().getWriteBuffer().flip();
+
+            int written = 0;
+            NioEndpoint.NioSocketWrapper att =
+                    (NioEndpoint.NioSocketWrapper) getSocket().getAttachment(false);
+            if (att == null) {
+                throw new IOException("Key must be cancelled");
+            }
+            long writeTimeout = att.getWriteTimeout();
+            NioSelectorPool pool = ((NioEndpoint) getEndpoint()).getSelectorPool();
+            Selector selector = null;
+            try {
+                selector = pool.get();
+            } catch ( IOException x ) {
+                //ignore
+            }
+            try {
+                written = pool.write(getSocket().getBufHandler().getWriteBuffer(),
+                        getSocket(), selector, writeTimeout, block);
+            } finally {
+                if (selector != null) {
+                    pool.put(selector);
+                }
+            }
+            if (written < len) {
+                getSocket().getPoller().add(getSocket(), SelectionKey.OP_WRITE);
+            }
+            return written;
+        }
+
+
+        public void flush() throws IOException {
+            NioEndpoint.NioSocketWrapper att =
+                    (NioEndpoint.NioSocketWrapper) getSocket().getAttachment(false);
+            if (att == null) {
+                throw new IOException("Key must be cancelled");
+            }
+            long writeTimeout = att.getWriteTimeout();
+            NioSelectorPool pool = ((NioEndpoint) getEndpoint()).getSelectorPool();
+            Selector selector = null;
+            try {
+                selector = pool.get();
+            } catch ( IOException x ) {
+                //ignore
+            }
+            try {
+                do {
+                    if (getSocket().flush(true, selector, writeTimeout)) {
+                        break;
+                    }
+                } while (true);
+            } finally {
+                if (selector != null) {
+                    pool.put(selector);
+                }
+            }
+        }
     }
 
+
     // ------------------------------------------------ Application Buffer Handler
     public static class NioBufferHandler implements ApplicationBufferHandler {
         private ByteBuffer readbuf = null;



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