Class SecureSocketChannel

All Implemented Interfaces:
Closeable, AutoCloseable, ByteChannel, Channel, GatheringByteChannel, InterruptibleChannel, NetworkChannel, ReadableByteChannel, ScatteringByteChannel, WritableByteChannel

public class SecureSocketChannel extends SocketChannel
A SocketChannel decorator that transparently wraps a raw channel with TLS/SSL encryption and decryption. All cryptographic operations are handled internally via TLSByteChannel, while non-I/O operations are delegated to the underlying raw channel.

Read and write operations are mutually exclusive — they share the same lock to serialize concurrent access to the TLS engine, which is not thread-safe.

This channel supports non-blocking mode only. It is designed to be driven by a Selector: each read(ByteBuffer) or write(ByteBuffer) pumps the TLS engine opportunistically and hands control back so the selector can signal when the socket is next readable or writable. In blocking mode the underlying read() would wait for the peer to fill the ~16KB inbound buffer (a read of a small record would hang until far more data arrived) and the read-after-write that pumps the handshake would block indefinitely. Both read(ByteBuffer) and write(ByteBuffer) therefore reject blocking mode with an IllegalBlockingModeException; call configureBlocking(false) before performing I/O.

This channel cannot be directly registered with a Selector. Instead, the underlying raw channel should be registered, with this secure channel attached to the resulting SelectionKey:

SelectionKey key = secureChannel.delegate().register(selector, SelectionKey.OP_READ);
key.attach(secureChannel);
Author:
Radoslav Husar