Ejemplo n.º 1
0
 /**
  * Wraps an existing socket in a TLS-enabled socket. Any data within 'data' will be pushed into
  * the TLS layer.
  *
  * <p>This currently only works for creating server-side TLS sockets.
  *
  * <p>You must ensure that <code>isTLSCapable</code> returns true for the socket, otherwise an
  * <code>IllegalArgumentException</code> is thrown.
  */
 public static TLSNIOSocket startTLS(Socket socket, ByteBuffer data) throws IOException {
   if (socket instanceof AbstractNBSocket) {
     TLSNIOSocket tlsSocket = new TLSNIOSocket(socket);
     // Tell the channel to read in the buffered data.
     if (data.hasRemaining()) {
       SSLReadWriteChannel sslChannel = tlsSocket.getSSLChannel();
       InterestReadableByteChannel oldReader = sslChannel.getReadChannel();
       sslChannel.setReadChannel(new BufferReader(data));
       sslChannel.read(BufferUtils.getEmptyBuffer());
       if (data.hasRemaining())
         throw new IllegalStateException("unable to read all prebuffered data in one pass!");
       sslChannel.setReadChannel(oldReader);
     }
     return tlsSocket;
   } else {
     throw new IllegalArgumentException("cannot wrap non AbstractNBSocket");
   }
 }
 public int read(ByteBuffer b) {
   return BufferUtils.transfer(buffer, b);
 }