Package naga

Class NIOUtils

java.lang.Object
naga.NIOUtils

public class NIOUtils extends Object
A collection of utilites used by various classes.
Author:
Christoffer Lerno
  • Method Details

    • closeKeyAndChannelSilently

      public static void closeKeyAndChannelSilently(SelectionKey key, 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(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 for
      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.
    • getPacketSizeFromByteBuffer

      public static int getPacketSizeFromByteBuffer(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(Channel channel)
      Silently close a channel.
      Parameters:
      channel - the channel to close, may be null.
    • cancelKeySilently

      public static void cancelKeySilently(SelectionKey key)
      Silently cancel a key.
      Parameters:
      key - the key to cancel, may be null.
    • compact

      public static ByteBuffer[] compact(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 ByteBuffer[] concat(ByteBuffer[] buffers, ByteBuffer buffer)
    • concat

      public static ByteBuffer[] concat(ByteBuffer buffer, ByteBuffer[] buffers2)
    • concat

      public static ByteBuffer[] concat(ByteBuffer[] buffers1, ByteBuffer[] buffers2)
    • copy

      public static ByteBuffer copy(ByteBuffer buffer)
    • remaining

      public static long remaining(ByteBuffer[] byteBuffers)
    • isEmpty

      public static boolean isEmpty(ByteBuffer[] byteBuffers)
    • join

      public static ByteBuffer join(ByteBuffer buffer1, ByteBuffer buffer2)