Package org.apache.thrift.transport
Class TNonblockingSocket
- java.lang.Object
-
- org.apache.thrift.transport.TTransport
-
- org.apache.thrift.transport.TEndpointTransport
-
- org.apache.thrift.transport.TNonblockingTransport
-
- org.apache.thrift.transport.TNonblockingSocket
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class TNonblockingSocket extends TNonblockingTransport
Transport for use with async client.
-
-
Field Summary
-
Fields inherited from class org.apache.thrift.transport.TEndpointTransport
knownMessageSize, remainingMessageSize
-
-
Constructor Summary
Constructors Constructor Description TNonblockingSocket(java.lang.String host, int port)TNonblockingSocket(java.lang.String host, int port, int timeout)Create a new nonblocking socket transport that will be connected to host:port.TNonblockingSocket(java.nio.channels.SocketChannel socketChannel)Constructor that takes an already created socket.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes the socket.booleanfinishConnect()Non-blocking connection completion.voidflush()Noop.java.nio.channels.SocketChannelgetSocketChannel()Returns a reference to the underlying SocketChannel.booleanisOpen()Checks whether the socket is connected.voidopen()Do not call, the implementation provides its own lazy non-blocking connect.intread(byte[] buf, int off, int len)Reads from the underlying input stream if not null.intread(java.nio.ByteBuffer buffer)Perform a nonblocking read into buffer.java.nio.channels.SelectionKeyregisterSelector(java.nio.channels.Selector selector, int interests)Register the new SocketChannel with our Selector, indicating we'd like to be notified when it's ready for I/O.voidsetTimeout(int timeout)Sets the socket timeout, although this implementation never uses blocking operations so it is unused.booleanstartConnect()Non-blocking connection initialization.java.lang.StringtoString()voidwrite(byte[] buf, int off, int len)Writes to the underlying output stream if not null.intwrite(java.nio.ByteBuffer buffer)Perform a nonblocking write of the data in buffer;-
Methods inherited from class org.apache.thrift.transport.TEndpointTransport
checkReadBytesAvailable, countConsumedMessageBytes, getConfiguration, getMaxMessageSize, resetConsumedMessageSize, updateKnownMessageSize
-
Methods inherited from class org.apache.thrift.transport.TTransport
consumeBuffer, getBuffer, getBufferPosition, getBytesRemainingInBuffer, peek, readAll, write
-
-
-
-
Constructor Detail
-
TNonblockingSocket
public TNonblockingSocket(java.lang.String host, int port) throws java.io.IOException, TTransportException- Throws:
java.io.IOExceptionTTransportException
-
TNonblockingSocket
public TNonblockingSocket(java.lang.String host, int port, int timeout) throws java.io.IOException, TTransportExceptionCreate a new nonblocking socket transport that will be connected to host:port.- Parameters:
host-port-- Throws:
java.io.IOExceptionTTransportException
-
TNonblockingSocket
public TNonblockingSocket(java.nio.channels.SocketChannel socketChannel) throws java.io.IOException, TTransportExceptionConstructor that takes an already created socket.- Parameters:
socketChannel- Already created SocketChannel object- Throws:
java.io.IOException- if there is an error setting up the streamsTTransportException
-
-
Method Detail
-
registerSelector
public java.nio.channels.SelectionKey registerSelector(java.nio.channels.Selector selector, int interests) throws java.io.IOExceptionRegister the new SocketChannel with our Selector, indicating we'd like to be notified when it's ready for I/O.- Specified by:
registerSelectorin classTNonblockingTransport- 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:
isOpenin classTTransport- Returns:
- True if the transport is open.
-
open
public void open() throws TTransportExceptionDo not call, the implementation provides its own lazy non-blocking connect.- Specified by:
openin classTTransport- Throws:
TTransportException- if the transport could not be opened
-
read
public int read(java.nio.ByteBuffer buffer) throws TTransportExceptionPerform a nonblocking read into buffer.- Overrides:
readin classTTransport- 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 TTransportExceptionReads from the underlying input stream if not null.- Specified by:
readin classTTransport- Parameters:
buf- Array to read intooff- Index to start reading atlen- 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 TTransportExceptionPerform a nonblocking write of the data in buffer;- Overrides:
writein classTTransport- 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 TTransportExceptionWrites to the underlying output stream if not null.- Specified by:
writein classTTransport- Parameters:
buf- The output data bufferoff- The offset to start writing fromlen- The number of bytes to write- Throws:
TTransportException- if there was an error writing data
-
flush
public void flush() throws TTransportExceptionNoop.- Overrides:
flushin classTTransport- Throws:
TTransportException- if there was an error writing out data.
-
close
public void close()
Closes the socket.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Specified by:
closein classTTransport
-
startConnect
public boolean startConnect() throws java.io.IOExceptionNon-blocking connection initialization.- Specified by:
startConnectin classTNonblockingTransport- Throws:
java.io.IOException- See Also:
SocketChannel.connect(SocketAddress remote)
-
finishConnect
public boolean finishConnect() throws java.io.IOExceptionNon-blocking connection completion.- Specified by:
finishConnectin classTNonblockingTransport- Throws:
java.io.IOException- See Also:
SocketChannel.finishConnect()
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-