naga
Class NIOUtils

java.lang.Object
  extended by naga.NIOUtils

public class NIOUtils
extends java.lang.Object

A collection of utilites used by various classes.

Author:
Christoffer Lerno

Method Summary
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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.

getByteBufferFromPacketSize

public 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. biggest or smallest byte first).

Parameters:
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.
Returns:
a byte buffer with the number encoded.
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,
                                              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.
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.