straightened out buffer handling
authorfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Fri, 8 Jun 2007 10:18:21 +0000 (10:18 +0000)
committerfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Fri, 8 Jun 2007 10:18:21 +0000 (10:18 +0000)
git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@545469 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/coyote/http11/InternalNioOutputBuffer.java
java/org/apache/tomcat/util/net/NioBlockingSelector.java
java/org/apache/tomcat/util/net/NioSelectorPool.java

index df5dc71..7499294 100644 (file)
@@ -406,7 +406,7 @@ public class InternalNioOutputBuffer
         if (!committed) {
             //Socket.send(socket, Constants.ACK_BYTES, 0, Constants.ACK_BYTES.length) < 0
             ByteBuffer buf = ByteBuffer.wrap(Constants.ACK_BYTES,0,Constants.ACK_BYTES.length);    
-            writeToSocket(buf,false,true);
+            writeToSocket(buf,true);
         }
 
     }
@@ -419,9 +419,12 @@ public class InternalNioOutputBuffer
      * @throws IOException
      * @todo Fix non blocking write properly
      */
-    private synchronized int writeToSocket(ByteBuffer bytebuffer, boolean flip, boolean block) throws IOException {
-        //int limit = bytebuffer.position();
-        if ( flip ) bytebuffer.flip();
+    private synchronized int writeToSocket(ByteBuffer bytebuffer, boolean block) throws IOException {
+        if (socket.getBufHandler().getWriteBuffer() != bytebuffer) {
+            socket.getBufHandler().getWriteBuffer().put(bytebuffer);
+            bytebuffer = socket.getBufHandler().getWriteBuffer();
+        }
+
         int written = 0;
         NioEndpoint.KeyAttachment att = (NioEndpoint.KeyAttachment)socket.getAttachment(false);
         if ( att == null ) throw new IOException("Key must be cancelled");
@@ -441,7 +444,7 @@ public class InternalNioOutputBuffer
         }finally { 
             if ( selector != null ) getSelectorPool().put(selector);
         }
-        if ( block ) socket.getBufHandler().getWriteBuffer().clear(); //only clear
+        if ( block ) bytebuffer.clear(); //only clear
         this.total = 0;
         return written;
     } 
@@ -762,7 +765,8 @@ public class InternalNioOutputBuffer
 
         //write to the socket, if there is anything to write
         if (socket.getBufHandler().getWriteBuffer().position() > 0) {
-            writeToSocket(socket.getBufHandler().getWriteBuffer(),true,true);
+            socket.getBufHandler().getWriteBuffer().flip();
+            writeToSocket(socket.getBufHandler().getWriteBuffer(),true);
         }
     }
 
index 5b06b67..d5a0f36 100644 (file)
@@ -206,7 +206,11 @@ public class NioBlockingSelector {
         public void add(final KeyAttachment key, final int ops) {
             Runnable r = new Runnable() {
                 public void run() {
-                    SocketChannel ch = key.getChannel().getIOChannel();
+                    if ( key == null ) return;
+                    NioChannel nch = key.getChannel();
+                    if ( nch == null ) return;
+                    SocketChannel ch = nch.getIOChannel();
+                    if ( ch == null ) return;
                     SelectionKey sk = ch.keyFor(selector);
                     try {
                         if (sk == null) {
@@ -230,7 +234,11 @@ public class NioBlockingSelector {
         public void remove(final KeyAttachment key, final int ops) {
             Runnable r = new Runnable() {
                 public void run() {
-                    SocketChannel ch = key.getChannel().getIOChannel();
+                    if ( key == null ) return;
+                    NioChannel nch = key.getChannel();
+                    if ( nch == null ) return;
+                    SocketChannel ch = nch.getIOChannel();
+                    if ( ch == null ) return;
                     SelectionKey sk = ch.keyFor(selector);
                     try {
                         if (sk == null) {
index b3b8cbb..aa61ffd 100644 (file)
@@ -152,10 +152,6 @@ public class NioSelectorPool {
     
     public int write(ByteBuffer buf, NioChannel socket, Selector selector, 
                      long writeTimeout, boolean block,MutableInteger lastWrite) throws IOException {
-        if (socket.getBufHandler().getWriteBuffer() != buf) {
-            socket.getBufHandler().getWriteBuffer().put(buf);
-            buf = socket.getBufHandler().getWriteBuffer();
-        }
         if ( SHARED && block ) {
             return blockingSelector.write(buf,socket,writeTimeout,lastWrite);
         }