Package naga
Class NIOUtils
- java.lang.Object
-
- naga.NIOUtils
-
public class NIOUtils extends java.lang.Object
A collection of utilites used by various classes.- Author:
- Christoffer Lerno
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method 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[]
compact(java.nio.ByteBuffer[] buffers)
Compacts an array of byte buffers, retaining only the buffers that have remaining data.static java.nio.ByteBuffer[]
concat(java.nio.ByteBuffer[] buffers, java.nio.ByteBuffer buffer)
static java.nio.ByteBuffer[]
concat(java.nio.ByteBuffer[] buffers1, java.nio.ByteBuffer[] buffers2)
static java.nio.ByteBuffer[]
concat(java.nio.ByteBuffer buffer, java.nio.ByteBuffer[] buffers2)
static java.nio.ByteBuffer
copy(java.nio.ByteBuffer buffer)
static int
getPacketSizeFromByteArray(byte[] data, int length, boolean bigEndian)
Converts a value in a header byte array encoded in either big or little endian encoding.static int
getPacketSizeFromByteBuffer(java.nio.ByteBuffer header, int size, boolean bigEndian)
Converts a value in a header buffer encoded in either big or little endian encoding.static boolean
isEmpty(java.nio.ByteBuffer[] byteBuffers)
static java.nio.ByteBuffer
join(java.nio.ByteBuffer buffer1, java.nio.ByteBuffer buffer2)
static long
remaining(java.nio.ByteBuffer[] byteBuffers)
static void
setHeaderForPacketSize(byte[] buffer, int headerSize, int valueToEncode, boolean bigEndian)
Inserts a header in the first bytes of a byte array in either big or little endian encoding (i.e.static void
setPacketSizeInByteBuffer(java.nio.ByteBuffer byteBuffer, int headerSize, int valueToEncode, boolean bigEndian)
Encodes a length into byte buffer using either big or little endian encoding (i.e.
-
-
-
Method Detail
-
closeKeyAndChannelSilently
public static void closeKeyAndChannelSilently(java.nio.channels.SelectionKey key, java.nio.channels.Channel channel)
Silently close both a key and a channel.- Parameters:
key
- the key to cancel, may be null.channel
- the channel to close, may be null.
-
setPacketSizeInByteBuffer
public static void setPacketSizeInByteBuffer(java.nio.ByteBuffer byteBuffer, int headerSize, int valueToEncode, boolean bigEndian)
Encodes a length into byte buffer using either big or little endian encoding (i.e. biggest or smallest byte first).- Parameters:
byteBuffer
- the ByteBuffer to use.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.- Throws:
java.lang.IllegalArgumentException
- if the value is out of range for the given header size.
-
setHeaderForPacketSize
public static void setHeaderForPacketSize(byte[] buffer, int headerSize, int valueToEncode, boolean bigEndian)
Inserts a header in the first bytes of a byte array in either big or little endian encoding (i.e. biggest or smallest byte first).- Parameters:
buffer
- the byte array to set the header forheaderSize
- 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.- Throws:
java.lang.IllegalArgumentException
- if the value is out of range for the given header size.
-
getPacketSizeFromByteBuffer
public static int getPacketSizeFromByteBuffer(java.nio.ByteBuffer header, int size, boolean bigEndian)
Converts a value in a header buffer encoded in either big or little endian encoding.Note that trying to decode a value larger than 2^31 - 2 is not supported.
- Parameters:
header
- the header to encode from.size
- the header size, 1-4.bigEndian
- if the encoding is big endian or not.- Returns:
- the decoded number.
-
getPacketSizeFromByteArray
public static int getPacketSizeFromByteArray(byte[] data, int length, boolean bigEndian)
Converts a value in a header byte array encoded in either big or little endian encoding.Note that trying to decode a value larger than 2^31 - 2 is not supported.
- Parameters:
data
- the data to encode from.length
- the length of the header.bigEndian
- if the encoding is big endian or not.- Returns:
- the decoded number.
-
closeChannelSilently
public static void closeChannelSilently(java.nio.channels.Channel channel)
Silently close a channel.- Parameters:
channel
- the channel to close, may be null.
-
cancelKeySilently
public static void cancelKeySilently(java.nio.channels.SelectionKey key)
Silently cancel a key.- Parameters:
key
- the key to cancel, may be null.
-
compact
public static java.nio.ByteBuffer[] compact(java.nio.ByteBuffer[] buffers)
Compacts an array of byte buffers, retaining only the buffers that have remaining data.- Parameters:
buffers
- the buffers to compact.- Returns:
- a compacted ByteBuffer array.
-
concat
public static java.nio.ByteBuffer[] concat(java.nio.ByteBuffer[] buffers, java.nio.ByteBuffer buffer)
-
concat
public static java.nio.ByteBuffer[] concat(java.nio.ByteBuffer buffer, java.nio.ByteBuffer[] buffers2)
-
concat
public static java.nio.ByteBuffer[] concat(java.nio.ByteBuffer[] buffers1, java.nio.ByteBuffer[] buffers2)
-
copy
public static java.nio.ByteBuffer copy(java.nio.ByteBuffer buffer)
-
remaining
public static long remaining(java.nio.ByteBuffer[] byteBuffers)
-
isEmpty
public static boolean isEmpty(java.nio.ByteBuffer[] byteBuffers)
-
join
public static java.nio.ByteBuffer join(java.nio.ByteBuffer buffer1, java.nio.ByteBuffer buffer2)
-
-