Interface EndPoint

All Superinterfaces:
AutoCloseable, Closeable
All Known Implementing Classes:
AbstractEndPoint, ByteArrayEndPoint, ClientHTTP2StreamEndPoint, DatagramChannelEndPoint, HTTP2StreamEndPoint, LocalConnector.LocalEndPoint, NetworkTrafficSocketChannelEndPoint, ProxyConnectionFactory.ProxyEndPoint, QuicStreamEndPoint, SelectableChannelEndPoint, ServerHTTP2StreamEndPoint, SocketChannelEndPoint, SslConnection.DecryptedEndPoint, UnixSocketEndPoint

public interface EndPoint extends Closeable

EndPoint is the abstraction for an I/O channel that transports bytes.

Asynchronous Methods

The asynchronous scheduling methods of EndPoint has been influenced by NIO.2 Futures and Completion handlers, but does not use those actual interfaces because they have some inefficiencies.

This class will frequently be used in conjunction with some of the utility implementations of Callback, such as FutureCallback and IteratingCallback.

Reads

A FutureCallback can be used to block until an endpoint is ready to fill bytes - the notification will be emitted by the NIO subsystem:

 FutureCallback callback = new FutureCallback();
 endPoint.fillInterested(callback);

 // Blocks until read to fill bytes.
 callback.get();

 // Now bytes can be filled in a ByteBuffer.
 int filled = endPoint.fill(byteBuffer);
 

Asynchronous Reads

A Callback can be used to read asynchronously in its own dispatched thread:

 endPoint.fillInterested(new Callback()
 {
   public void onSucceeded()
   {
     executor.execute(() ->
     {
       // Fill bytes in a different thread.
       int filled = endPoint.fill(byteBuffer);
     });
   }
   public void onFailed(Throwable failure)
   {
     endPoint.close();
   }
 });
 

Blocking Writes

The write contract is that the callback is completed when all the bytes have been written or there is a failure. Blocking writes look like this:

 FutureCallback callback = new FutureCallback();
 endpoint.write(callback, headerBuffer, contentBuffer);

 // Blocks until the write succeeds or fails.
 future.get();
 

Note also that multiple buffers may be passed in write(Callback, ByteBuffer...) so that gather writes can be performed for efficiency.