title: socketchannel.write() becomes very slow when message size is large
body: <p>In my program using java nio, the socketchannel.write() becomes very slow when it tries to write 10 KB messages consecutively. The measured time for writing a complete 10 KB message is between 160 ms and 200 ms. But the time for writing a complete 5 KB message is only takes 0.8 ms.</p>
  
  <p>In the selector, I only have Selection.OP_READ and do not handle Selection.OP_WRITE. When a large complete message is received, it is written to another receiver 4 times. </p>
  
  <p>Is anyone accounter same problem? There is a post about socketchannel.write() slow. My question is how to alternate change between OP_READ and OP_WRITE? </p>
  
  <p>If I add an inerval e.g, 150 ms, the response time is reduced. Is there any way to find when the buffer is full so I can let the program waits. My operating system is windows xp.</p>
  
  <p>Thanks.</p>
  
  <p>I follow EPJ suggestion by checking the number of written bytes. But the response time is still high. I post part of my code here and would like to examine whether there is wrong with my code.</p>
  
  <p>// this is the writeData() part using nio:</p>
  
  <pre><code>      while (buffer.hasRemaining()) {   
          try {           
                  buffer.flip();                  
                  n = socket.write(buffer);           
                  if(n == 0) {                
                      key.interestOps(SelectionKey.OP_WRITE);
                      key.attach(buffer);             
                      break;
                  }                               
          } catch (IOException e) {               
              e.printStackTrace();
          } finally {
              buffer.compact();
          }
      }   
  
      if(buffer.position()==0) {                  
          key.interestOps(SelectionKey.OP_READ);
      }
  </code></pre>