Class Buffers

java.lang.Object
cl.netswitch.lib.util.Buffers

public final class Buffers extends Object
Provides static utility methods that operate on ByteBuffers.
  • Field Details

  • Method Details

    • readByte

      public static int readByte(ByteBuffer buffer)
      Reads a byte from the supplied ByteBuffer. The position of the buffer is incremented by 1.
      Parameters:
      buffer - the byte buffer that will be read.
      Returns:
      the next available byte in supplied ByteBuffer.
      Throws:
      NullPointerException - if buffer is null.
      BufferUnderflowException - if buffer is missing bytes.
    • readShort

      public static short readShort(ByteBuffer buffer)
      Reads a short from the supplied ByteBuffer. The position of the buffer is incremented by 2. The bytes of the buffer are always composed in big endian order (most significant to least significant).
      Parameters:
      buffer - the ByteBuffer containing the input bytes.
      Returns:
      the short value read from the supplied ByteBuffer.
      Throws:
      NullPointerException - if buffer is null.
      BufferUnderflowException - if buffer is missing bytes.
    • readInt

      public static int readInt(ByteBuffer buffer)
      Reads an integer from the supplied ByteBuffer. The position of the buffer is incremented by 4. The bytes of the buffer are always composed in big endian order (most significant to least significant).
      Parameters:
      buffer - the ByteBuffer containing the input bytes.
      Returns:
      the integer value read from the supplied ByteBuffer.
      Throws:
      NullPointerException - if buffer is null.
      BufferUnderflowException - if buffer is missing bytes.
    • readLong

      public static long readLong(ByteBuffer buffer)
      Reads a long from the supplied ByteBuffer. The position of the buffer is incremented by 8. The bytes of the buffer are always composed in big endian order (most significant to least significant).
      Parameters:
      buffer - the ByteBuffer containing the input bytes.
      Returns:
      the long value read from the supplied ByteBuffer.
      Throws:
      NullPointerException - if buffer is null.
      BufferUnderflowException - if buffer is missing bytes.
    • readDouble

      public static double readDouble(ByteBuffer buffer)
      Reads a double from the supplied ByteBuffer. The position of the buffer is incremented by 8. The bytes of the buffer are always composed in big endian order (most significant to least significant).
      Parameters:
      buffer - the ByteBuffer containing the input bytes.
      Returns:
      the double value read from the supplied ByteBuffer.
      Throws:
      NullPointerException - if buffer is null.
      BufferUnderflowException - if buffer is missing bytes.
    • readFloat

      public static float readFloat(ByteBuffer buffer)
      Reads a float from the supplied ByteBuffer. The position of the buffer is incremented by 4. The bytes of the buffer are always composed in big endian order (most significant to least significant).
      Parameters:
      buffer - the ByteBuffer containing the input bytes.
      Returns:
      the float value read from the supplied ByteBuffer.
      Throws:
      NullPointerException - if buffer is null.
      BufferUnderflowException - if buffer is missing bytes.
    • readBytes

      public static byte[] readBytes(ByteBuffer buffer, int length)
      Reads a byte array from the supplied ByteBuffer. The position of the buffer is incremented by length.
      Parameters:
      buffer - the byte buffer that will be read.
      length - the number of bytes to read.
      Returns:
      the byte array read from the ByteBuffer.
      Throws:
      NullPointerException - if buffer is null.
      IllegalArgumentException - if length is negative.
      BufferUnderflowException - if buffer is missing bytes.
    • read7bitInt

      public static int read7bitInt(ByteBuffer buffer)
      Reads a 7bit integer from the supplied ByteBuffer.
      Parameters:
      buffer - the byte buffer that will be read.
      Returns:
      the integer value read from the ByteBuffer.
      Throws:
      NullPointerException - if buffer is null.
      BufferUnderflowException - if buffer is missing bytes.
    • read7bitLong

      public static long read7bitLong(ByteBuffer buffer)
      Reads a 7bit long from the supplied ByteBuffer.
      Parameters:
      buffer - the byte buffer that will be read.
      Returns:
      the long value read from the ByteBuffer.
      Throws:
      NullPointerException - if buffer is null.
      BufferUnderflowException - if buffer is missing bytes.
    • read7bitBytes

      public static byte[] read7bitBytes(ByteBuffer buffer)
      Reads a 7bit byte array from the supplied ByteBuffer.
      Parameters:
      buffer - the byte buffer that will be read.
      Returns:
      the byte array read from the ByteBuffer.
      Throws:
      NullPointerException - if buffer is null.
      BufferUnderflowException - if buffer is missing bytes.
    • read7bitString

      public static String read7bitString(ByteBuffer buffer)
      Reads a 7bit string from the supplied ByteBuffer.
      Parameters:
      buffer - the byte buffer that will be read.
      Returns:
      the string read from the ByteBuffer.
      Throws:
      NullPointerException - if buffer is null.
      BufferUnderflowException - if buffer is missing bytes.
    • toTrace

      public static String toTrace(byte[] bytes, int offset, int length, boolean inbound)
      Returns a trace representation of the supplied array of bytes.
      Parameters:
      bytes - the array of bytes to be converted to a string.
      offset - the offset of the subarray to be used.
      length - the length of the subarray to be used.
      inbound - true if inbound message; otherwise outbound.
      Returns:
      a trace representation of the supplied array of bytes.
      Throws:
      NullPointerException - if bytes is null.
    • toTrace

      public static String toTrace(ByteBuffer buffer, boolean inbound)
      Returns a trace representation of the supplied ByteBuffer.
      Parameters:
      buffer - the ByteBuffer to be converted to a string.
      inbound - true if inbound message; otherwise outbound.
      Returns:
      a trace representation of the supplied ByteBuffer.
      Throws:
      NullPointerException - if buffer is null.