Package naga
Class NIOUtils
java.lang.Object
naga.NIOUtils
A collection of utilites used by various classes.
- Author:
- Christoffer Lerno
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
Silently cancel a key.static void
closeChannelSilently
(Channel channel) Silently close a channel.static void
closeKeyAndChannelSilently
(SelectionKey key, Channel channel) Silently close both a key and a channel.static ByteBuffer[]
compact
(ByteBuffer[] buffers) Compacts an array of byte buffers, retaining only the buffers that have remaining data.static ByteBuffer[]
concat
(ByteBuffer[] buffers, ByteBuffer buffer) static ByteBuffer[]
concat
(ByteBuffer[] buffers1, ByteBuffer[] buffers2) static ByteBuffer[]
concat
(ByteBuffer buffer, ByteBuffer[] buffers2) static ByteBuffer
copy
(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
(ByteBuffer header, int size, boolean bigEndian) Converts a value in a header buffer encoded in either big or little endian encoding.static boolean
isEmpty
(ByteBuffer[] byteBuffers) static ByteBuffer
join
(ByteBuffer buffer1, ByteBuffer buffer2) static long
remaining
(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
(ByteBuffer byteBuffer, int headerSize, int valueToEncode, boolean bigEndian) Encodes a length into byte buffer using either big or little endian encoding (i.e.
-
Method Details
-
closeKeyAndChannelSilently
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(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:
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:
IllegalArgumentException
- if the value is out of range for the given header size.
-
getPacketSizeFromByteBuffer
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
Silently close a channel.- Parameters:
channel
- the channel to close, may be null.
-
cancelKeySilently
Silently cancel a key.- Parameters:
key
- the key to cancel, may be null.
-
compact
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
-
concat
-
concat
-
copy
-
remaining
-
isEmpty
-
join
-