Package biz.onixs.util.network
Interface Socket
-
- All Superinterfaces:
AutoCloseable
public interface Socket extends AutoCloseable
The interface provides methods for configuring sockets and performing IO operations.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description SocketAddressgetLocalAddress()Returns the local address to which the socket is bound.SocketAddressgetRemoteAddress()Returns the remote address to which the socket is connected.booleanisBlocking()Returns whether every I/O operation on this channel will block until it completes.intread(ByteBuffer dst)Reads a sequence of bytes into the given buffer.voidsetBlocking(boolean blocking)Sets whether every I/O operation on this channel will block until it completes.<T> voidsetOption(SocketOption<T> name, T value)Sets the value of the socket option (e.g.voidsetPerformancePreferences(int connectionTime, int latency, int bandwidth)Sets the performance preferences for this socket.voidwaitRead(long timeout, TimeUnit timeUnit)Waits for read I/O operation to be ready.voidwaitWrite(long timeout, TimeUnit timeUnit)Waits for write I/O operation to be ready.voidwakeupRead()Causes thewaitRead(long, TimeUnit)operation that has not yet returned to return immediately.voidwakeupWrite()Causes thewaitWrite(long, TimeUnit)operation that has not yet returned to return immediately.intwrite(ByteBuffer src)Writes a sequence of bytes from the given buffer.-
Methods inherited from interface java.lang.AutoCloseable
close
-
-
-
-
Method Detail
-
getLocalAddress
SocketAddress getLocalAddress() throws IOException
Returns the local address to which the socket is bound.- Returns:
- the local address to which the socket is bound
- Throws:
IOException- if an I/O error occurs when getting the local address
-
getRemoteAddress
SocketAddress getRemoteAddress() throws IOException
Returns the remote address to which the socket is connected.- Returns:
- the remote address to which the socket is connected
- Throws:
IOException- if an I/O error occurs when getting the remote address
-
setOption
<T> void setOption(SocketOption<T> name, T value) throws IOException
Sets the value of the socket option (e.g.StandardSocketOptions).- Type Parameters:
T- the type of the socket option value- Parameters:
name- the name of the socket optionvalue- the value of the socket option- Throws:
IOException- if an I/O error occurs when setting the socket optionUnsupportedOperationException- if the socket option is not supportedIllegalArgumentException- if the value is invalid for the socket option
-
setPerformancePreferences
void setPerformancePreferences(int connectionTime, int latency, int bandwidth)Sets the performance preferences for this socket.This method does not have a consistent behavior across different operating systems and platforms.
- Parameters:
connectionTime- anintexpressing the relative weighting given to a short connection timelatency- anintexpressing the relative weighting given to low latencybandwidth- anintexpressing the relative weighting given to high bandwidth
-
read
int read(ByteBuffer dst) throws IOException
Reads a sequence of bytes into the given buffer.- Parameters:
dst- the buffer into which bytes are to be transferred.- Returns:
- the number of bytes read, possibly zero, or -1 if the socket has reached end-of-stream.
- Throws:
IOException- If some other I/O error occurs.
-
waitRead
void waitRead(long timeout, TimeUnit timeUnit) throws IOExceptionWaits for read I/O operation to be ready. This method performs a blocking operation. It returns only after read operation is ready,wakeupRead()method is invoked, the current thread is interrupted, or the given timeout period expires, whichever comes first.- Parameters:
timeout- if positive, block for up to timeout in specified time unit, more or less, while waiting for a socket to become read ready; if zero, block indefinitely; must not be negative.timeUnit- time unit of the timeout.- Throws:
IOException- If some other I/O error occurs.
-
wakeupRead
void wakeupRead()
Causes thewaitRead(long, TimeUnit)operation that has not yet returned to return immediately.
-
write
int write(ByteBuffer src) throws IOException
Writes a sequence of bytes from the given buffer.- Parameters:
src- the buffer from which bytes are to be retrieved.- Returns:
- the number of bytes written, possibly zero.
- Throws:
IOException- If some other I/O error occurs.
-
waitWrite
void waitWrite(long timeout, TimeUnit timeUnit) throws IOExceptionWaits for write I/O operation to be ready. This method performs a blocking operation. It returns only after write operation is ready,wakeupWrite()method is invoked, the current thread is interrupted, or the given timeout period expires, whichever comes first.- Parameters:
timeout- if positive, block for up to timeout in specified time unit, more or less, while waiting for a socket to become write ready; if zero, block indefinitely; must not be negative.timeUnit- time unit of the timeout.- Throws:
IOException- If some other I/O error occurs.
-
wakeupWrite
void wakeupWrite()
Causes thewaitWrite(long, TimeUnit)operation that has not yet returned to return immediately.
-
isBlocking
boolean isBlocking()
Returns whether every I/O operation on this channel will block until it completes.- Returns:
trueif, and only if, this socket is in blocking mode.
-
setBlocking
void setBlocking(boolean blocking) throws IOExceptionSets whether every I/O operation on this channel will block until it completes.- Parameters:
blocking-truefor block until I/O operation completes,falseotherwise.- Throws:
IOException- If some other I/O error occurs.
-
-