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 voidSilently cancel a key.static voidcloseChannelSilently(Channel channel) Silently close a channel.static voidcloseKeyAndChannelSilently(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 ByteBuffercopy(ByteBuffer buffer) static intgetPacketSizeFromByteArray(byte[] data, int length, boolean bigEndian) Converts a value in a header byte array encoded in either big or little endian encoding.static intgetPacketSizeFromByteBuffer(ByteBuffer header, int size, boolean bigEndian) Converts a value in a header buffer encoded in either big or little endian encoding.static booleanisEmpty(ByteBuffer[] byteBuffers) static ByteBufferjoin(ByteBuffer buffer1, ByteBuffer buffer2) static longremaining(ByteBuffer[] byteBuffers) static voidsetHeaderForPacketSize(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 voidsetPacketSizeInByteBuffer(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
-