Class SecureSocketChannel
- All Implemented Interfaces:
Closeable, AutoCloseable, ByteChannel, Channel, GatheringByteChannel, InterruptibleChannel, NetworkChannel, ReadableByteChannel, ScatteringByteChannel, WritableByteChannel
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
-
Constructor Summary
ConstructorsConstructorDescriptionSecureSocketChannel(SocketChannel delegate, SSLEngine engine, ExecutorService taskExecutor) -
Method Summary
Modifier and TypeMethodDescriptionbind(SocketAddress local) booleanconnect(SocketAddress remote) delegate()Returns the underlying raw (unencrypted) channel, for use with selector registration.boolean<T> TgetOption(SocketOption<T> name) protected voidprotected voidimplConfigureBlocking(boolean block) booleanbooleanintread(ByteBuffer dst) longread(ByteBuffer[] dsts, int offset, int length) <T> SocketChannelsetOption(SocketOption<T> name, T value) socket()Set<SocketOption<?>> intwrite(ByteBuffer src) longwrite(ByteBuffer[] srcs, int offset, int length) Methods inherited from class AbstractSelectableChannel
blockingLock, configureBlocking, implCloseChannel, isBlocking, isRegistered, keyFor, provider, registerMethods inherited from class SelectableChannel
registerMethods inherited from class AbstractInterruptibleChannel
begin, close, end, isOpen
-
Constructor Details
-
SecureSocketChannel
-
-
Method Details
-
delegate
Returns the underlying raw (unencrypted) channel, for use with selector registration. -
read
- Specified by:
readin interfaceReadableByteChannel- Specified by:
readin classSocketChannel- Throws:
IllegalBlockingModeException- if this channel is in blocking mode; only non-blocking mode is supported (see the class documentation)IOException
-
read
- Specified by:
readin interfaceScatteringByteChannel- Specified by:
readin classSocketChannel- Throws:
IOException
-
write
- Specified by:
writein interfaceWritableByteChannel- Specified by:
writein classSocketChannel- Throws:
IllegalBlockingModeException- if this channel is in blocking mode; only non-blocking mode is supported (see the class documentation)IOException
-
write
- Specified by:
writein interfaceGatheringByteChannel- Specified by:
writein classSocketChannel- Throws:
IOException
-
implCloseSelectableChannel
- Specified by:
implCloseSelectableChannelin classAbstractSelectableChannel- Throws:
IOException
-
implConfigureBlocking
- Specified by:
implConfigureBlockingin classAbstractSelectableChannel- Throws:
IOException
-
isConnected
public boolean isConnected()- Specified by:
isConnectedin classSocketChannel
-
isConnectionPending
public boolean isConnectionPending()- Specified by:
isConnectionPendingin classSocketChannel
-
connect
- Specified by:
connectin classSocketChannel- Throws:
IOException
-
finishConnect
- Specified by:
finishConnectin classSocketChannel- Throws:
IOException
-
getRemoteAddress
- Specified by:
getRemoteAddressin classSocketChannel- Throws:
IOException
-
getLocalAddress
- Specified by:
getLocalAddressin interfaceNetworkChannel- Specified by:
getLocalAddressin classSocketChannel- Throws:
IOException
-
bind
- Specified by:
bindin interfaceNetworkChannel- Specified by:
bindin classSocketChannel- Throws:
IOException
-
setOption
- Specified by:
setOptionin interfaceNetworkChannel- Specified by:
setOptionin classSocketChannel- Throws:
IOException
-
getOption
- Throws:
IOException
-
supportedOptions
-
shutdownInput
- Specified by:
shutdownInputin classSocketChannel- Throws:
IOException
-
shutdownOutput
- Specified by:
shutdownOutputin classSocketChannel- Throws:
IOException
-
socket
- Specified by:
socketin classSocketChannel
-