Class TNonblockingSocket

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class TNonblockingSocket
    extends TNonblockingTransport
    Transport for use with async client.
    • Constructor Detail

      • TNonblockingSocket

        public TNonblockingSocket​(java.lang.String host,
                                  int port,
                                  int timeout)
                           throws java.io.IOException,
                                  TTransportException
        Create a new nonblocking socket transport that will be connected to host:port.
        Parameters:
        host -
        port -
        Throws:
        java.io.IOException
        TTransportException
      • TNonblockingSocket

        public TNonblockingSocket​(java.nio.channels.SocketChannel socketChannel)
                           throws java.io.IOException,
                                  TTransportException
        Constructor that takes an already created socket.
        Parameters:
        socketChannel - Already created SocketChannel object
        Throws:
        java.io.IOException - if there is an error setting up the streams
        TTransportException
    • Method Detail

      • registerSelector

        public java.nio.channels.SelectionKey registerSelector​(java.nio.channels.Selector selector,
                                                               int interests)
                                                        throws java.io.IOException
        Register the new SocketChannel with our Selector, indicating we'd like to be notified when it's ready for I/O.
        Specified by:
        registerSelector in class TNonblockingTransport
        Parameters:
        selector -
        Returns:
        the selection key for this socket.
        Throws:
        java.io.IOException
      • setTimeout

        public void setTimeout​(int timeout)
        Sets the socket timeout, although this implementation never uses blocking operations so it is unused.
        Parameters:
        timeout - Milliseconds timeout
      • getSocketChannel

        public java.nio.channels.SocketChannel getSocketChannel()
        Returns a reference to the underlying SocketChannel.
      • isOpen

        public boolean isOpen()
        Checks whether the socket is connected.
        Specified by:
        isOpen in class TTransport
        Returns:
        True if the transport is open.
      • read

        public int read​(java.nio.ByteBuffer buffer)
                 throws TTransportException
        Perform a nonblocking read into buffer.
        Overrides:
        read in class TTransport
        Parameters:
        buffer - The buffer into which bytes are to be transferred
        Returns:
        The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream
        Throws:
        TTransportException - if there was an error reading data
      • read

        public int read​(byte[] buf,
                        int off,
                        int len)
                 throws TTransportException
        Reads from the underlying input stream if not null.
        Specified by:
        read in class TTransport
        Parameters:
        buf - Array to read into
        off - Index to start reading at
        len - Maximum number of bytes to read
        Returns:
        The number of bytes actually read
        Throws:
        TTransportException - if there was an error reading data
      • write

        public int write​(java.nio.ByteBuffer buffer)
                  throws TTransportException
        Perform a nonblocking write of the data in buffer;
        Overrides:
        write in class TTransport
        Parameters:
        buffer - The buffer from which bytes are to be retrieved
        Returns:
        The number of bytes written, possibly zero
        Throws:
        TTransportException - if there was an error writing data
      • write

        public void write​(byte[] buf,
                          int off,
                          int len)
                   throws TTransportException
        Writes to the underlying output stream if not null.
        Specified by:
        write in class TTransport
        Parameters:
        buf - The output data buffer
        off - The offset to start writing from
        len - The number of bytes to write
        Throws:
        TTransportException - if there was an error writing data
      • close

        public void close()
        Closes the socket.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Specified by:
        close in class TTransport
      • startConnect

        public boolean startConnect()
                             throws java.io.IOException
        Non-blocking connection initialization.
        Specified by:
        startConnect in class TNonblockingTransport
        Throws:
        java.io.IOException
        See Also:
        SocketChannel.connect(SocketAddress remote)
      • finishConnect

        public boolean finishConnect()
                              throws java.io.IOException
        Non-blocking connection completion.
        Specified by:
        finishConnect in class TNonblockingTransport
        Throws:
        java.io.IOException
        See Also:
        SocketChannel.finishConnect()
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object