Class ByteBuffer


  • public class ByteBuffer
    extends Object
    A byte buffer class.

    This class is modeled on the java.nio.Buffer interface. Specifically, the properties capacity and limit are defined as for Buffer.

    • A buffer's capacity is the number of elements it contains. The capacity of a buffer is never negative and never changes.
    • A buffer's limit is the index of the first element that should not be read or written. A buffer's limit is never negative and is never greater than its capacity.
    Author:
    Karel Maesen, Geovise BVBA creation-date: Oct 29, 2010
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int DOUBLE_SIZE
      byte size for doubles
      static long UINT_MAX_VALUE
      Max.
      static int UINT_SIZE
      byte size for unsigned int
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static ByteBuffer allocate​(int capacity)
      Allocates a new ByteBuffer of the specified capacity.
      int capacity()
      Returns this buffer's capacity.
      boolean equals​(Object o)  
      static ByteBuffer from​(byte[] bytes)
      Wraps a byte array into a ByteBuffer.
      static ByteBuffer from​(String hexString)
      Creates a ByteBuffer from a hexadecimal string.
      byte get()
      Relative get method.
      ByteOrder getByteOrder()
      Gets the byte order of this instance.
      double getDouble()
      Reads the next 8 bytes as a double from this instance at the current position, taking into account the byte-order, and then increments the position by eight.
      float getFloat()
      Reads the next 4 bytes as a float from this instance at the current position, taking into account the byte-order, and then increments the position by four.
      int getInt()
      Reads the next 4 bytes as an int from this instance at the current position, taking into account the byte-order, and then increments the position by four.
      long getLong()
      Reads the next 8 bytes as a long from this instance at the current position, taking into account the byte-order, and then increments the position by eight.
      long getUInt()
      Reads the next 4 bytes as an unsigned integer from this instance at the current position, taking into account the byte-order, and then increments the position by four.
      int hashCode()  
      boolean hasSameContent​(ByteBuffer other)
      Used for testing purposes.
      boolean isEmpty()
      Reports if this buffer is empty (holds no bytes).
      int limit()
      Returns this buffer's limit.
      void put​(byte value)
      Writes the given byte into this buffer at the current position, and then increments the position.
      void putDouble​(Double value)
      Writes the specified double-value as 8 bytes to this instance at the current position, respecting the byte-order, and then increments the position by eight.
      void putFloat​(float value)
      Writes the specified float-value as 4 bytes to this instance at the current position, respecting the byte-order, and then increments the position by four.
      void putInt​(int value)
      Writes the specified int-value as 4 bytes to this instance at the current position, respecting the byte-order, and then increments the position by four.
      void putLong​(long value)
      Writes the specified long value as 8 bytes to this instance at the current position, respecting the byte-order, and then increments the position by eight.
      void putUInt​(long value)
      Interprets the specified long-value as and unsigned integer, and appends it as 4 bytes to this instance at the current position, respecting the byte-order.
      void rewind()
      Rewinds the buffer.
      void setByteOrder​(ByteOrder wbo)
      Sets the byte order for this instance.
      byte[] toByteArray()
      Returns the byte array that backs this buffer.
      String toString()
      Returns this instance as a hexadecimal string.
    • Field Detail

      • UINT_SIZE

        public static final int UINT_SIZE
        byte size for unsigned int
        See Also:
        Constant Field Values
      • UINT_MAX_VALUE

        public static final long UINT_MAX_VALUE
        Max. permissible value for an unsigned int.
        See Also:
        Constant Field Values
    • Method Detail

      • from

        public static ByteBuffer from​(String hexString)
        Creates a ByteBuffer from a hexadecimal string.

        Every two chars in the string are interpreted as the hexadecimal representation of a byte. If the string length is odd, the last character will be ignored.

        Parameters:
        hexString - the bytes represented in hexadecimal form
        Returns:
        A ByteBuffer based on the hexadecimal string
      • toString

        public String toString()
        Returns this instance as a hexadecimal string.
        Overrides:
        toString in class Object
        Returns:
        A string representation of this ByteBuffer in hexadecimal form
      • from

        public static ByteBuffer from​(byte[] bytes)
        Wraps a byte array into a ByteBuffer. The new buffer will be backed by the given byte array; that is, modifications to the buffer will cause the array to be modified and vice versa. The new buffer's capacity and limit will be bytes.length.
        Parameters:
        bytes - The array that will back this buffer
        Returns:
        The new byte buffer.
      • allocate

        public static ByteBuffer allocate​(int capacity)
        Allocates a new ByteBuffer of the specified capacity. The new buffer's position will be zero, its limit will be its capacity and each of its elements will be initialized to zero.
        Parameters:
        capacity - The new buffer's capacity, in bytes
        Returns:
        a new ByteBuffer instance of the specified capacity
      • get

        public byte get()
        Relative get method. Reads the byte at this buffer's current position, and then increments the position.
        Returns:
        The byte at the buffer's current position
        Throws:
        BufferAccessException - If the buffer's current position is not smaller than its limit.
      • put

        public void put​(byte value)
        Writes the given byte into this buffer at the current position, and then increments the position.
        Parameters:
        value - The byte to be written
        Throws:
        BufferAccessException - If this buffer's current position is not smaller than its limit.
      • capacity

        public int capacity()
        Returns this buffer's capacity.
        Returns:
        The capacity of this buffer
      • limit

        public int limit()
        Returns this buffer's limit.
        Returns:
        The limit of this buffer
      • rewind

        public void rewind()
        Rewinds the buffer.

        After rewind, the next get() or put() will take place on the first element of this instance.

      • isEmpty

        public boolean isEmpty()
        Reports if this buffer is empty (holds no bytes).
        Returns:
        True if limit is 0, otherwise false
      • setByteOrder

        public void setByteOrder​(ByteOrder wbo)
        Sets the byte order for this instance.
        Parameters:
        wbo - The new byte order, either XDR or NDR
      • getInt

        public int getInt()
        Reads the next 4 bytes as an int from this instance at the current position, taking into account the byte-order, and then increments the position by four.
        Returns:
        The int value at the buffer's current position
        Throws:
        BufferAccessException - If there are fewer than four bytes remaining in this buffer.
      • putInt

        public void putInt​(int value)
        Writes the specified int-value as 4 bytes to this instance at the current position, respecting the byte-order, and then increments the position by four.
        Parameters:
        value - The int value to be written
        Throws:
        BufferAccessException - If there are fewer than four bytes remaining in this buffer.
      • getLong

        public long getLong()
        Reads the next 8 bytes as a long from this instance at the current position, taking into account the byte-order, and then increments the position by eight.
        Returns:
        The long value at the buffer's current position
        Throws:
        BufferAccessException - If there are fewer than eight bytes remaining in this buffer.
      • putLong

        public void putLong​(long value)
        Writes the specified long value as 8 bytes to this instance at the current position, respecting the byte-order, and then increments the position by eight.
        Parameters:
        value - The long value to be written
        Throws:
        BufferAccessException - If there are fewer than eight bytes remaining in this buffer.
      • getFloat

        public float getFloat()
        Reads the next 4 bytes as a float from this instance at the current position, taking into account the byte-order, and then increments the position by four.
        Returns:
        The float value at the buffer's current position
        Throws:
        BufferAccessException - If there are fewer than four bytes remaining in this buffer.
      • putFloat

        public void putFloat​(float value)
        Writes the specified float-value as 4 bytes to this instance at the current position, respecting the byte-order, and then increments the position by four.
        Parameters:
        value - The float value to be written
        Throws:
        BufferAccessException - If there are fewer than four bytes remaining in this buffer.
      • getDouble

        public double getDouble()
        Reads the next 8 bytes as a double from this instance at the current position, taking into account the byte-order, and then increments the position by eight.
        Returns:
        The double value at the buffer's current position
        Throws:
        BufferAccessException - If there are fewer than eight bytes remaining in this buffer.
      • putDouble

        public void putDouble​(Double value)
        Writes the specified double-value as 8 bytes to this instance at the current position, respecting the byte-order, and then increments the position by eight.
        Parameters:
        value - The double value to be written
        Throws:
        BufferAccessException - If there are fewer than eight bytes remaining in this buffer.
      • getUInt

        public long getUInt()
        Reads the next 4 bytes as an unsigned integer from this instance at the current position, taking into account the byte-order, and then increments the position by four.
        Returns:
        The value of the 4-byte unsigned integer at the current position as a long
        Throws:
        BufferAccessException - If there are fewer than four bytes remaining in this buffer
      • putUInt

        public void putUInt​(long value)
        Interprets the specified long-value as and unsigned integer, and appends it as 4 bytes to this instance at the current position, respecting the byte-order.
        Parameters:
        value - The unsigned integer value to be written
        Throws:
        RuntimeException - If the specified value is larger than the largest unsigned integer (4294967295L)
        BufferAccessException - If there are fewer than eight bytes remaining in this buffer.
      • getByteOrder

        public ByteOrder getByteOrder()
        Gets the byte order of this instance.
        Returns:
        This buffer's byte order
      • toByteArray

        public byte[] toByteArray()
        Returns the byte array that backs this buffer.
        Returns:
        The array that backs this buffer
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • hasSameContent

        public boolean hasSameContent​(ByteBuffer other)
        Used for testing purposes.
        Parameters:
        other - another ByteBuffer
        Returns:
        true if both buffers contain the same bytes, false otherwise