Package org.lwjgl

Class PointerBuffer

java.lang.Object
org.lwjgl.PointerBuffer
All Implemented Interfaces:
Comparable

public class PointerBuffer extends Object implements Comparable
This class is a container for architecture independent pointer data. The interface mirrors the NIO LongBuffer API for convenience.
Author:
Spasi
  • Field Details

  • Constructor Details

    • PointerBuffer

      public PointerBuffer(int capacity)
      Creates a new PointerBuffer with the specified capacity.
      Parameters:
      capacity - the PointerBuffer size, in number of pointers
    • PointerBuffer

      public PointerBuffer(ByteBuffer source)
      Creates a new PointerBuffer using the specified ByteBuffer as its pointer data source. This is useful for users that do their own memory management over a big ByteBuffer, instead of allocating many small ones.
      Parameters:
      source - the source buffer
  • Method Details

    • getBuffer

      public ByteBuffer getBuffer()
      Returns the ByteBuffer that backs this PointerBuffer.
      Returns:
      the pointer ByteBuffer
    • is64Bit

      public static boolean is64Bit()
      Returns true if the underlying architecture is 64bit.
    • getPointerSize

      public static int getPointerSize()
      Returns the pointer size in bytes, based on the underlying architecture.
      Returns:
      The pointer size in bytes
    • capacity

      public final int capacity()
      Returns this buffer's capacity.

      Returns:
      The capacity of this buffer
    • position

      public final int position()
      Returns this buffer's position.

      Returns:
      The position of this buffer
    • positionByte

      public final int positionByte()
      Returns this buffer's position, in bytes.

      Returns:
      The position of this buffer in bytes.
    • position

      public final PointerBuffer position(int newPosition)
      Sets this buffer's position. If the mark is defined and larger than the new position then it is discarded.

      Parameters:
      newPosition - The new position value; must be non-negative and no larger than the current limit
      Returns:
      This buffer
      Throws:
      IllegalArgumentException - If the preconditions on newPosition do not hold
    • limit

      public final int limit()
      Returns this buffer's limit.

      Returns:
      The limit of this buffer
    • limit

      public final PointerBuffer limit(int newLimit)
      Sets this buffer's limit. If the position is larger than the new limit then it is set to the new limit. If the mark is defined and larger than the new limit then it is discarded.

      Parameters:
      newLimit - The new limit value; must be non-negative and no larger than this buffer's capacity
      Returns:
      This buffer
      Throws:
      IllegalArgumentException - If the preconditions on newLimit do not hold
    • mark

      public final PointerBuffer mark()
      Sets this buffer's mark at its position.

      Returns:
      This buffer
    • reset

      public final PointerBuffer reset()
      Resets this buffer's position to the previously-marked position.

      Invoking this method neither changes nor discards the mark's value.

      Returns:
      This buffer
      Throws:
      InvalidMarkException - If the mark has not been set
    • clear

      public final PointerBuffer clear()
      Clears this buffer. The position is set to zero, the limit is set to the capacity, and the mark is discarded.

      Invoke this method before using a sequence of channel-read or put operations to fill this buffer. For example:

       buf.clear();     // Prepare buffer for reading
       in.read(buf);    // Read data

      This method does not actually erase the data in the buffer, but it is named as if it did because it will most often be used in situations in which that might as well be the case.

      Returns:
      This buffer
    • flip

      public final PointerBuffer flip()
      Flips this buffer. The limit is set to the current position and then the position is set to zero. If the mark is defined then it is discarded.

      After a sequence of channel-read or put operations, invoke this method to prepare for a sequence of channel-write or relative get operations. For example:

       buf.put(magic);    // Prepend header
       in.read(buf);      // Read data into rest of buffer
       buf.flip();        // Flip buffer
       out.write(buf);    // Write header + data to channel

      This method is often used in conjunction with the compact method when transferring data from one place to another.

      Returns:
      This buffer
    • rewind

      public final PointerBuffer rewind()
      Rewinds this buffer. The position is set to zero and the mark is discarded.

      Invoke this method before a sequence of channel-write or get operations, assuming that the limit has already been set appropriately. For example:

       out.write(buf);    // Write remaining data
       buf.rewind();      // Rewind buffer
       buf.get(array);    // Copy data into array
      Returns:
      This buffer
    • remaining

      public final int remaining()
      Returns the number of elements between the current position and the limit.

      Returns:
      The number of elements remaining in this buffer
    • remainingByte

      public final int remainingByte()
      Returns the number of bytes between the current position and the limit.

      Returns:
      The number of bytes remaining in this buffer
    • hasRemaining

      public final boolean hasRemaining()
      Tells whether there are any elements between the current position and the limit.

      Returns:
      true if, and only if, there is at least one element remaining in this buffer
    • allocateDirect

      public static PointerBuffer allocateDirect(int capacity)
      Allocates a new pointer buffer.

      The new buffer's position will be zero, its limit will be its capacity, and its mark will be undefined.

      Parameters:
      capacity - The new buffer's capacity, in pointers
      Returns:
      The new pointer buffer
      Throws:
      IllegalArgumentException - If the capacity is a negative integer
    • newInstance

      protected PointerBuffer newInstance(ByteBuffer source)
      This method is used in slice and duplicate instead of normal object construction, so that subclasses can return themselves.
      Parameters:
      source -
      Returns:
      A new PointerBuffer instance
    • slice

      public PointerBuffer slice()
      Creates a new pointer buffer whose content is a shared subsequence of this buffer's content.

      The content of the new buffer will start at this buffer's current position. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independent.

      The new buffer's position will be zero, its capacity and its limit will be the number of longs remaining in this buffer, and its mark will be undefined. The new buffer will be direct if, and only if, this buffer is direct, and it will be read-only if, and only if, this buffer is read-only.

      Returns:
      The new pointer buffer
    • duplicate

      public PointerBuffer duplicate()
      Creates a new pointer buffer that shares this buffer's content.

      The content of the new buffer will be that of this buffer. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independent.

      The new buffer's capacity, limit and position will be identical to those of this buffer. The new buffer will be direct if, and only if, this buffer is direct, and it will be read-only if, and only if, this buffer is read-only.

      Returns:
      The new pointer buffer
    • asReadOnlyBuffer

      public PointerBuffer asReadOnlyBuffer()
      Creates a new, read-only pointer buffer that shares this buffer's content.

      The content of the new buffer will be that of this buffer. Changes to this buffer's content will be visible in the new buffer; the new buffer itself, however, will be read-only and will not allow the shared content to be modified. The two buffers' position, limit, and mark values will be independent.

      The new buffer's capacity, limit and position will be identical to those of this buffer.

      If this buffer is itself read-only then this method behaves in exactly the same way as the duplicate method.

      Returns:
      The new, read-only pointer buffer
    • isReadOnly

      public boolean isReadOnly()
    • get

      public long get()
      Relative get method. Reads the long at this buffer's current position, and then increments the position.

      Returns:
      The long at the buffer's current position
      Throws:
      BufferUnderflowException - If the buffer's current position is not smaller than its limit
    • put

      public PointerBuffer put(long l)
      Relative put method  (optional operation).

      Writes the given long into this buffer at the current position, and then increments the position.

      Parameters:
      l - The long to be written
      Returns:
      This buffer
      Throws:
      BufferOverflowException - If this buffer's current position is not smaller than its limit
      ReadOnlyBufferException - If this buffer is read-only
    • put

      public PointerBuffer put(PointerWrapper pointer)
      Convenience put that accepts PointerWrapper objects.
      See Also:
    • put

      public static void put(ByteBuffer target, long l)
      Convenience put on a target ByteBuffer.
      Parameters:
      target - the target ByteBuffer
      l - the long value to be written
    • get

      public long get(int index)
      Absolute get method. Reads the long at the given index.

      Parameters:
      index - The index from which the long will be read
      Returns:
      The long at the given index
      Throws:
      IndexOutOfBoundsException - If index is negative or not smaller than the buffer's limit
    • put

      public PointerBuffer put(int index, long l)
      Absolute put method  (optional operation).

      Writes the given long into this buffer at the given index.

      Parameters:
      index - The index at which the long will be written
      l - The long value to be written
      Returns:
      This buffer
      Throws:
      IndexOutOfBoundsException - If index is negative or not smaller than the buffer's limit
      ReadOnlyBufferException - If this buffer is read-only
    • put

      public PointerBuffer put(int index, PointerWrapper pointer)
      Convenience put that accepts PointerWrapper objects.
      See Also:
    • put

      public static void put(ByteBuffer target, int index, long l)
      Convenience put on a target ByteBuffer.
      Parameters:
      target - the target ByteBuffer
      index - the index at which the long will be written
      l - the long value to be written
    • get

      public PointerBuffer get(long[] dst, int offset, int length)
      Relative bulk get method.

      This method transfers longs from this buffer into the given destination array. If there are fewer longs remaining in the buffer than are required to satisfy the request, that is, if length > remaining(), then no longs are transferred and a BufferUnderflowException is thrown.

      Otherwise, this method copies length longs from this buffer into the given array, starting at the current position of this buffer and at the given offset in the array. The position of this buffer is then incremented by length.

      In other words, an invocation of this method of the form src.get(dst, off, len) has exactly the same effect as the loop

           for (int i = off; i invalid input: '<' off + len; i++)
               dst[i] = src.get(); 

      except that it first checks that there are sufficient longs in this buffer and it is potentially much more efficient.

      Parameters:
      dst - The array into which longs are to be written
      offset - The offset within the array of the first long to be written; must be non-negative and no larger than dst.length
      length - The maximum number of longs to be written to the given array; must be non-negative and no larger than dst.length - offset
      Returns:
      This buffer
      Throws:
      BufferUnderflowException - If there are fewer than length longs remaining in this buffer
      IndexOutOfBoundsException - If the preconditions on the offset and length parameters do not hold
    • get

      public PointerBuffer get(long[] dst)
      Relative bulk get method.

      This method transfers longs from this buffer into the given destination array. An invocation of this method of the form src.get(a) behaves in exactly the same way as the invocation

           src.get(a, 0, a.length) 
      Returns:
      This buffer
      Throws:
      BufferUnderflowException - If there are fewer than length longs remaining in this buffer
    • put

      public PointerBuffer put(PointerBuffer src)
      Relative bulk put method  (optional operation).

      This method transfers the longs remaining in the given source buffer into this buffer. If there are more longs remaining in the source buffer than in this buffer, that is, if src.remaining() > remaining(), then no longs are transferred and a BufferOverflowException is thrown.

      Otherwise, this method copies n = src.remaining() longs from the given buffer into this buffer, starting at each buffer's current position. The positions of both buffers are then incremented by n.

      In other words, an invocation of this method of the form dst.put(src) has exactly the same effect as the loop

           while (src.hasRemaining())
               dst.put(src.get()); 

      except that it first checks that there is sufficient space in this buffer and it is potentially much more efficient.

      Parameters:
      src - The source buffer from which longs are to be read; must not be this buffer
      Returns:
      This buffer
      Throws:
      BufferOverflowException - If there is insufficient space in this buffer for the remaining longs in the source buffer
      IllegalArgumentException - If the source buffer is this buffer
      ReadOnlyBufferException - If this buffer is read-only
    • put

      public PointerBuffer put(long[] src, int offset, int length)
      Relative bulk put method  (optional operation).

      This method transfers longs into this buffer from the given source array. If there are more longs to be copied from the array than remain in this buffer, that is, if length > remaining(), then no longs are transferred and a BufferOverflowException is thrown.

      Otherwise, this method copies length longs from the given array into this buffer, starting at the given offset in the array and at the current position of this buffer. The position of this buffer is then incremented by length.

      In other words, an invocation of this method of the form dst.put(src, off, len) has exactly the same effect as the loop

           for (int i = off; i invalid input: '<' off + len; i++)
               dst.put(a[i]); 

      except that it first checks that there is sufficient space in this buffer and it is potentially much more efficient.

      Parameters:
      src - The array from which longs are to be read
      offset - The offset within the array of the first long to be read; must be non-negative and no larger than array.length
      length - The number of longs to be read from the given array; must be non-negative and no larger than array.length - offset
      Returns:
      This buffer
      Throws:
      BufferOverflowException - If there is insufficient space in this buffer
      IndexOutOfBoundsException - If the preconditions on the offset and length parameters do not hold
      ReadOnlyBufferException - If this buffer is read-only
    • put

      public final PointerBuffer put(long[] src)
      Relative bulk put method  (optional operation).

      This method transfers the entire content of the given source long array into this buffer. An invocation of this method of the form dst.put(a) behaves in exactly the same way as the invocation

           dst.put(a, 0, a.length) 
      Returns:
      This buffer
      Throws:
      BufferOverflowException - If there is insufficient space in this buffer
      ReadOnlyBufferException - If this buffer is read-only
    • compact

      public PointerBuffer compact()
      Compacts this buffer  (optional operation).

      The longs between the buffer's current position and its limit, if any, are copied to the beginning of the buffer. That is, the long at index p = position() is copied to index zero, the long at index p + 1 is copied to index one, and so forth until the long at index limit() - 1 is copied to index n = limit() - 1 - p. The buffer's position is then set to n+1 and its limit is set to its capacity. The mark, if defined, is discarded.

      The buffer's position is set to the number of longs copied, rather than to zero, so that an invocation of this method can be followed immediately by an invocation of another relative put method.

      Returns:
      This buffer
      Throws:
      ReadOnlyBufferException - If this buffer is read-only
    • order

      public ByteOrder order()
      Retrieves this buffer's byte order.

      The byte order of a pointer buffer created by allocation or by wrapping an existing long array is the native order of the underlying hardware. The byte order of a pointer buffer created as a view of a byte buffer is that of the byte buffer at the moment that the view is created.

      Returns:
      This buffer's byte order
    • toString

      public String toString()
      Returns a string summarizing the state of this buffer.

      Overrides:
      toString in class Object
      Returns:
      A summary string
    • hashCode

      public int hashCode()
      Returns the current hash code of this buffer.

      The hash code of a pointer buffer depends only upon its remaining elements; that is, upon the elements from position() up to, and including, the element at limit() - 1.

      Because buffer hash codes are content-dependent, it is inadvisable to use buffers as keys in hash maps or similar data structures unless it is known that their contents will not change.

      Overrides:
      hashCode in class Object
      Returns:
      The current hash code of this buffer
    • equals

      public boolean equals(Object ob)
      Tells whether or not this buffer is equal to another object.

      Two pointer buffers are equal if, and only if,

      1. They have the same element type,

      2. They have the same number of remaining elements, and

      3. The two sequences of remaining elements, considered independently of their starting positions, are pointwise equal.

      A pointer buffer is not equal to any other type of object.

      Overrides:
      equals in class Object
      Parameters:
      ob - The object to which this buffer is to be compared
      Returns:
      true if, and only if, this buffer is equal to the given object
    • compareTo

      public int compareTo(Object o)
      Compares this buffer to another.

      Two pointer buffers are compared by comparing their sequences of remaining elements lexicographically, without regard to the starting position of each sequence within its corresponding buffer.

      A pointer buffer is not comparable to any other type of object.

      Specified by:
      compareTo in interface Comparable
      Returns:
      A negative integer, zero, or a positive integer as this buffer is less than, equal to, or greater than the given buffer