/*
  * Output methods
  */
 @Override
 public void flush() throws IOException, RemoteException {
   try {
     org.apache.tomcat.util.net.NioEndpointKeyAttachmentRemoteInterface att =
         (org.apache.tomcat.util.net.NioEndpointKeyAttachmentRemoteInterface)
             nioChannel.getAttachment(false);
     if (att == null) {
       throw new IOException("Key must be cancelled");
     }
     long writeTimeout = att.getTimeout();
     Selector selector = null;
     try {
       selector = pool.get();
     } catch (IOException x) {
       // ignore
     }
     try {
       do {
         if (nioChannel.flush(true, selector, writeTimeout)) {
           break;
         }
       } while (true);
     } finally {
       if (selector != null) {
         pool.put(selector);
       }
     }
   } catch (Exception excp) {
     excp.printStackTrace();
   }
 }
  /*
   * Adapted from the NioOutputBuffer
   */
  public synchronized int writeToSocket(byte[] bytes, int off, int len)
      throws IOException, RemoteException {
    try {

      nioChannel.getBufHandler().getWriteBuffer().clear();
      nioChannel.getBufHandler().getWriteBuffer().put(bytes, off, len);
      nioChannel.getBufHandler().getWriteBuffer().flip();

      int written = 0;
      org.apache.tomcat.util.net.NioEndpointKeyAttachmentRemoteInterface att =
          (org.apache.tomcat.util.net.NioEndpointKeyAttachmentRemoteInterface)
              nioChannel.getAttachment(false);
      if (att == null) {
        throw new IOException("Key must be cancelled");
      }
      long writeTimeout = att.getTimeout();
      Selector selector = null;
      try {
        selector = pool.get();
      } catch (IOException x) {
        // ignore
      }
      try {
        written =
            pool.write(
                nioChannel.getBufHandler().getWriteBuffer(),
                nioChannel,
                selector,
                writeTimeout,
                true);
      } finally {
        if (selector != null) {
          pool.put(selector);
        }
      }
      return written;
    } catch (Exception excp) {
      excp.printStackTrace();
    }
    return 0;
  }
 public int fillReadBuffer(boolean block) throws IOException, RemoteException {
   try {
     int nRead;
     if (block) {
       Selector selector = null;
       try {
         selector = pool.get();
       } catch (IOException x) {
         // Ignore
       }
       try {
         org.apache.tomcat.util.net.NioEndpointKeyAttachmentRemoteInterface att =
             (org.apache.tomcat.util.net.NioEndpointKeyAttachmentRemoteInterface)
                 nioChannel.getAttachment(false);
         if (att == null) {
           throw new IOException("Key must be cancelled.");
         }
         nRead =
             pool.read(
                 nioChannel.getBufHandler().getReadBuffer(),
                 nioChannel,
                 selector,
                 att.getTimeout());
       } catch (EOFException eof) {
         nRead = -1;
       } finally {
         if (selector != null) {
           pool.put(selector);
         }
       }
     } else {
       nRead = nioChannel.readRemote(nioChannel.getBufHandler().getReadBuffer());
     }
     return nRead;
   } catch (Exception excp) {
     excp.printStackTrace();
   }
   return 0;
 }