Class NioSocketChannel

All Implemented Interfaces:
Channel, ChannelOutboundInvoker, DuplexChannel, SocketChannel, AttributeMap, java.lang.Comparable<Channel>

public class NioSocketChannel extends AbstractNioByteChannel implements SocketChannel
SocketChannel which uses NIO selector based implementation.
  • Field Details

    • logger

      private static final InternalLogger logger
    • DEFAULT_SELECTOR_PROVIDER

      private static final java.nio.channels.spi.SelectorProvider DEFAULT_SELECTOR_PROVIDER
    • OPEN_SOCKET_CHANNEL_WITH_FAMILY

      private static final java.lang.reflect.Method OPEN_SOCKET_CHANNEL_WITH_FAMILY
    • config

      private final SocketChannelConfig config
  • Constructor Details

    • NioSocketChannel

      public NioSocketChannel()
      Create a new instance
    • NioSocketChannel

      public NioSocketChannel(java.nio.channels.spi.SelectorProvider provider)
      Create a new instance using the given SelectorProvider.
    • NioSocketChannel

      @Deprecated public NioSocketChannel(java.nio.channels.spi.SelectorProvider provider, InternetProtocolFamily family)
      Create a new instance using the given SelectorProvider and protocol family (supported only since JDK 15).
    • NioSocketChannel

      public NioSocketChannel(java.nio.channels.spi.SelectorProvider provider, SocketProtocolFamily family)
      Create a new instance using the given SelectorProvider and protocol family (supported only since JDK 15).
    • NioSocketChannel

      public NioSocketChannel(java.nio.channels.SocketChannel socket)
      Create a new instance using the given SocketChannel.
    • NioSocketChannel

      public NioSocketChannel(Channel parent, java.nio.channels.SocketChannel socket)
      Create a new instance
      Parameters:
      parent - the Channel which created this instance or null if it was created by the user
      socket - the SocketChannel which will be used
  • Method Details

    • newChannel

      private static java.nio.channels.SocketChannel newChannel(java.nio.channels.spi.SelectorProvider provider, SocketProtocolFamily family)
    • parent

      public ServerSocketChannel parent()
      Description copied from interface: Channel
      Returns the parent of this channel.
      Specified by:
      parent in interface Channel
      Specified by:
      parent in interface SocketChannel
      Overrides:
      parent in class AbstractChannel
      Returns:
      the parent channel. null if this channel does not have a parent channel.
    • config

      public SocketChannelConfig config()
      Description copied from interface: Channel
      Returns the configuration of this channel.
      Specified by:
      config in interface Channel
      Specified by:
      config in interface SocketChannel
    • javaChannel

      protected java.nio.channels.SocketChannel javaChannel()
      Overrides:
      javaChannel in class AbstractNioChannel
    • isActive

      public boolean isActive()
      Description copied from interface: Channel
      Return true if the Channel is active and so connected.
      Specified by:
      isActive in interface Channel
    • isOutputShutdown

      public boolean isOutputShutdown()
      Specified by:
      isOutputShutdown in interface DuplexChannel
      See Also:
      • Socket.isOutputShutdown()
    • isInputShutdown

      public boolean isInputShutdown()
      Description copied from interface: DuplexChannel
      Returns true if and only if the remote peer shut down its output so that no more data is received from this channel. Note that the semantic of this method is different from that of Socket.shutdownInput() and Socket.isInputShutdown().
      Specified by:
      isInputShutdown in interface DuplexChannel
    • isShutdown

      public boolean isShutdown()
      Description copied from interface: DuplexChannel
      Determine if both the input and output of this channel have been shutdown.
      Specified by:
      isShutdown in interface DuplexChannel
    • localAddress

      public java.net.InetSocketAddress localAddress()
      Description copied from interface: Channel
      Returns the local address where this channel is bound to. The returned SocketAddress is supposed to be down-cast into more concrete type such as InetSocketAddress to retrieve the detailed information.
      Specified by:
      localAddress in interface Channel
      Specified by:
      localAddress in interface SocketChannel
      Overrides:
      localAddress in class AbstractChannel
      Returns:
      the local address of this channel. null if this channel is not bound.
    • remoteAddress

      public java.net.InetSocketAddress remoteAddress()
      Description copied from interface: Channel
      Returns the remote address where this channel is connected to. The returned SocketAddress is supposed to be down-cast into more concrete type such as InetSocketAddress to retrieve the detailed information.
      Specified by:
      remoteAddress in interface Channel
      Specified by:
      remoteAddress in interface SocketChannel
      Overrides:
      remoteAddress in class AbstractChannel
      Returns:
      the remote address of this channel. null if this channel is not connected. If this channel is not connected but it can receive messages from arbitrary remote addresses (e.g. DatagramChannel, use DefaultAddressedEnvelope.recipient() to determine the origination of the received message as this method will return null.
    • doShutdownOutput

      protected final void doShutdownOutput() throws java.lang.Exception
      Description copied from class: AbstractChannel
      Called when conditions justify shutting down the output portion of the channel. This may happen if a write operation throws an exception.
      Overrides:
      doShutdownOutput in class AbstractChannel
      Throws:
      java.lang.Exception
    • shutdownOutput

      public ChannelFuture shutdownOutput()
      Specified by:
      shutdownOutput in interface DuplexChannel
      See Also:
      • Socket.shutdownOutput()
    • shutdownOutput

      public ChannelFuture shutdownOutput(ChannelPromise promise)
      Description copied from interface: DuplexChannel
      Will shutdown the output and notify ChannelPromise.
      Specified by:
      shutdownOutput in interface DuplexChannel
      See Also:
      • Socket.shutdownOutput()
    • shutdownInput

      public ChannelFuture shutdownInput()
      Description copied from class: AbstractNioByteChannel
      Shutdown the input side of the channel.
      Specified by:
      shutdownInput in interface DuplexChannel
      Specified by:
      shutdownInput in class AbstractNioByteChannel
      See Also:
      • Socket.shutdownInput()
    • isInputShutdown0

      protected boolean isInputShutdown0()
      Overrides:
      isInputShutdown0 in class AbstractNioByteChannel
    • shutdownInput

      public ChannelFuture shutdownInput(ChannelPromise promise)
      Description copied from interface: DuplexChannel
      Will shutdown the input and notify ChannelPromise.
      Specified by:
      shutdownInput in interface DuplexChannel
      See Also:
      • Socket.shutdownInput()
    • shutdown

      public ChannelFuture shutdown()
      Description copied from interface: DuplexChannel
      Will shutdown the input and output sides of this channel.
      Specified by:
      shutdown in interface DuplexChannel
      Returns:
      will be completed when both shutdown operations complete.
    • shutdown

      public ChannelFuture shutdown(ChannelPromise promise)
      Description copied from interface: DuplexChannel
      Will shutdown the input and output sides of this channel.
      Specified by:
      shutdown in interface DuplexChannel
      Parameters:
      promise - will be completed when both shutdown operations complete.
      Returns:
      will be completed when both shutdown operations complete.
    • shutdownOutputDone

      private void shutdownOutputDone(ChannelFuture shutdownOutputFuture, ChannelPromise promise)
    • shutdownDone

      private static void shutdownDone(ChannelFuture shutdownOutputFuture, ChannelFuture shutdownInputFuture, ChannelPromise promise)
    • shutdownInput0

      private void shutdownInput0(ChannelPromise promise)
    • shutdownInput0

      private void shutdownInput0() throws java.lang.Exception
      Throws:
      java.lang.Exception
    • localAddress0

      protected java.net.SocketAddress localAddress0()
      Description copied from class: AbstractChannel
      Returns the SocketAddress which is bound locally.
      Specified by:
      localAddress0 in class AbstractChannel
    • remoteAddress0

      protected java.net.SocketAddress remoteAddress0()
      Description copied from class: AbstractChannel
      Return the SocketAddress which the Channel is connected to.
      Specified by:
      remoteAddress0 in class AbstractChannel
    • doBind

      protected void doBind(java.net.SocketAddress localAddress) throws java.lang.Exception
      Description copied from class: AbstractChannel
      Bind the Channel to the SocketAddress
      Specified by:
      doBind in class AbstractChannel
      Throws:
      java.lang.Exception
    • doBind0

      private void doBind0(java.net.SocketAddress localAddress) throws java.lang.Exception
      Throws:
      java.lang.Exception
    • doConnect

      protected boolean doConnect(java.net.SocketAddress remoteAddress, java.net.SocketAddress localAddress) throws java.lang.Exception
      Description copied from class: AbstractNioChannel
      Connect to the remote peer
      Specified by:
      doConnect in class AbstractNioChannel
      Throws:
      java.lang.Exception
    • doFinishConnect

      protected void doFinishConnect() throws java.lang.Exception
      Description copied from class: AbstractNioChannel
      Finish the connect
      Specified by:
      doFinishConnect in class AbstractNioChannel
      Throws:
      java.lang.Exception
    • doDisconnect

      protected void doDisconnect() throws java.lang.Exception
      Description copied from class: AbstractChannel
      Disconnect this Channel from its remote peer
      Specified by:
      doDisconnect in class AbstractChannel
      Throws:
      java.lang.Exception
    • doClose

      protected void doClose() throws java.lang.Exception
      Description copied from class: AbstractChannel
      Close the Channel
      Overrides:
      doClose in class AbstractNioChannel
      Throws:
      java.lang.Exception
    • doReadBytes

      protected int doReadBytes(ByteBuf byteBuf) throws java.lang.Exception
      Description copied from class: AbstractNioByteChannel
      Read bytes into the given ByteBuf and return the amount.
      Specified by:
      doReadBytes in class AbstractNioByteChannel
      Throws:
      java.lang.Exception
    • doWriteBytes

      protected int doWriteBytes(ByteBuf buf) throws java.lang.Exception
      Description copied from class: AbstractNioByteChannel
      Write bytes form the given ByteBuf to the underlying Channel.
      Specified by:
      doWriteBytes in class AbstractNioByteChannel
      Parameters:
      buf - the ByteBuf from which the bytes should be written
      Returns:
      amount the amount of written bytes
      Throws:
      java.lang.Exception
    • doWriteFileRegion

      protected long doWriteFileRegion(FileRegion region) throws java.lang.Exception
      Description copied from class: AbstractNioByteChannel
      Write a FileRegion
      Specified by:
      doWriteFileRegion in class AbstractNioByteChannel
      Parameters:
      region - the FileRegion from which the bytes should be written
      Returns:
      amount the amount of written bytes
      Throws:
      java.lang.Exception
    • adjustMaxBytesPerGatheringWrite

      private void adjustMaxBytesPerGatheringWrite(int attempted, int written, int oldMaxBytesPerGatheringWrite)
    • doWrite

      protected void doWrite(ChannelOutboundBuffer in) throws java.lang.Exception
      Description copied from class: AbstractChannel
      Flush the content of the given buffer to the remote peer.
      Overrides:
      doWrite in class AbstractNioByteChannel
      Throws:
      java.lang.Exception
    • newUnsafe

      protected AbstractNioChannel.AbstractNioUnsafe newUnsafe()
      Description copied from class: AbstractChannel
      Create a new AbstractChannel.AbstractUnsafe instance which will be used for the life-time of the Channel
      Overrides:
      newUnsafe in class AbstractNioByteChannel