naga
public class NIOUtils extends java.lang.Object
Modifier and Type | Method and Description |
---|---|
static void |
cancelKeySilently(java.nio.channels.SelectionKey key)
Silently cancel a key.
|
static void |
closeChannelSilently(java.nio.channels.Channel channel)
Silently close a channel.
|
static void |
closeKeyAndChannelSilently(java.nio.channels.SelectionKey key,
java.nio.channels.Channel channel)
Silently close both a key and a channel.
|
static java.nio.ByteBuffer |
getByteBufferFromPacketSize(int headerSize,
int valueToEncode,
boolean bigEndian)
Creates a byte buffer with a given length with an encoded value,
in either big or little endian encoding (i.e.
|
static int |
getPacketSizeFromByteBuffer(java.nio.ByteBuffer header,
boolean bigEndian)
Converts a value in a header buffer encoded in either big or little endian
encoding.
|
public static void closeKeyAndChannelSilently(java.nio.channels.SelectionKey key, java.nio.channels.Channel channel)
key
- the key to cancel, may be null.channel
- the channel to close, may be null.public static java.nio.ByteBuffer getByteBufferFromPacketSize(int headerSize, int valueToEncode, boolean bigEndian)
headerSize
- the header size in bytes. 1-4.valueToEncode
- the value to encode, 0 <= value < 2^(headerSize * 8)bigEndian
- if the encoding is big endian or not.java.lang.IllegalArgumentException
- if the value is out of range for the given header size.public static int getPacketSizeFromByteBuffer(java.nio.ByteBuffer header, boolean bigEndian)
Note that trying to decode a value larger than 2^31 - 2 is not supported.
header
- the header to encode from.bigEndian
- if the encoding is big endian or not.public static void closeChannelSilently(java.nio.channels.Channel channel)
channel
- the channel to close, may be null.public static void cancelKeySilently(java.nio.channels.SelectionKey key)
key
- the key to cancel, may be null.