/*
  * 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();
   }
 }
  public UpgradeNioProcessor(
      org.apache.tomcat.util.net.SocketWrapperRemoteInterface<
              org.apache.tomcat.util.net.NioChannelRemoteInterface>
          wrapper,
      UpgradeInbound upgradeInbound,
      org.apache.tomcat.util.net.NioSelectorPoolRemoteInterface pool)
      throws RemoteException {
    super(upgradeInbound);

    startManagers();
    wrapper.setTimeout(upgradeInbound.getReadTimeout());

    this.nioChannel = wrapper.getSocket();
    this.pool = pool;
    this.maxRead = nioChannel.getBufHandler().getReadBuffer().capacity();
    this.maxWrite = nioChannel.getBufHandler().getWriteBuffer().capacity();
  }
  /*
   * 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;
 }
  /*
   * Adapted from the NioInputBuffer.
   */
  public int readSocket(boolean block, byte[] bytes, int offset, int len)
      throws IOException, RemoteException {
    try {

      ByteBuffer readBuffer = nioChannel.getBufHandler().getReadBuffer();
      int remaining = readBuffer.remaining();

      // Is there enough data in the read buffer to satisfy this request?
      if (remaining >= len) {
        readBuffer.get(bytes, offset, len);
        return len;
      }

      // Copy what data there is in the read buffer to the byte array
      int leftToWrite = len;
      int newOffset = offset;
      if (remaining > 0) {
        readBuffer.get(bytes, offset, remaining);
        leftToWrite -= remaining;
        newOffset += remaining;
      }

      // Fill the read buffer as best we can
      readBuffer.clear();
      int nRead = fillReadBuffer(block);

      // Full as much of the remaining byte array as possible with the data
      // that was just read
      if (nRead > 0) {
        readBuffer.flip();
        readBuffer.limit(nRead);
        if (nRead > leftToWrite) {
          readBuffer.get(bytes, newOffset, leftToWrite);
          leftToWrite = 0;
        } else {
          readBuffer.get(bytes, newOffset, nRead);
          leftToWrite -= nRead;
        }
      } else if (nRead == 0) {
        readBuffer.flip();
        readBuffer.limit(nRead);
      } else if (nRead == -1) {
        throw new EOFException(getSm().getString("nio.eof.error"));
      }

      return len - leftToWrite;
    } catch (Exception excp) {
      excp.printStackTrace();
    }
    return 0;
  }