Package java.nio

Class Buffer

java.lang.Object
java.nio.Buffer
Direct Known Subclasses:
ByteBuffer, CharBuffer, DoubleBuffer, FloatBuffer, IntBuffer, LongBuffer, ShortBuffer

public abstract class Buffer
extends Object
A buffer is a list of elements of a specific primitive type.

A buffer can be described by the following properties:

  • Capacity: the number of elements a buffer can hold. Capacity may not be negative and never changes.
  • Position: a cursor of this buffer. Elements are read or written at the position if you do not specify an index explicitly. Position may not be negative and not greater than the limit.
  • Limit: controls the scope of accessible elements. You can only read or write elements from index zero to limit - 1. Accessing elements out of the scope will cause an exception. Limit may not be negative and not greater than capacity.
  • Mark: used to remember the current position, so that you can reset the position later. Mark may not be negative and no greater than position.
  • A buffer can be read-only or read-write. Trying to modify the elements of a read-only buffer will cause a ReadOnlyBufferException, while changing the position, limit and mark of a read-only buffer is OK.
  • A buffer can be direct or indirect. A direct buffer will try its best to take advantage of native memory APIs and it may not stay in the Java heap, thus it is not affected by garbage collection.

Buffers are not thread-safe. If concurrent access to a buffer instance is required, then the callers are responsible to take care of the synchronization issues.

  • Method Summary

    Modifier and Type Method Description
    abstract Object array()
    Returns the array that backs this buffer (optional operation).
    abstract int arrayOffset()
    Returns the offset into the array returned by array of the first element of the buffer (optional operation).
    int capacity()
    Returns the capacity of this buffer.
    Buffer clear()
    Clears this buffer.
    Buffer flip()
    Flips this buffer.
    abstract boolean hasArray()
    Returns true if array and arrayOffset won't throw.
    boolean hasRemaining()
    Indicates if there are elements remaining in this buffer, that is if position < limit.
    abstract boolean isDirect()
    Returns true if this is a direct buffer.
    abstract boolean isReadOnly()
    Indicates whether this buffer is read-only.
    int limit()
    Returns the limit of this buffer.
    Buffer limit​(int newLimit)
    Sets the limit of this buffer.
    Buffer mark()
    Marks the current position, so that the position may return to this point later by calling reset().
    int position()
    Returns the position of this buffer.
    Buffer position​(int newPosition)
    Sets the position of this buffer.
    int remaining()
    Returns the number of remaining elements in this buffer, that is limit - position.
    Buffer reset()
    Resets the position of this buffer to the mark.
    Buffer rewind()
    Rewinds this buffer.
    String toString()
    Returns a string describing this buffer.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Method Details

    • array

      public abstract Object array()
      Returns the array that backs this buffer (optional operation). The returned value is the actual array, not a copy, so modifications to the array write through to the buffer.

      Subclasses should override this method with a covariant return type to provide the exact type of the array.

      Use hasArray to ensure this method won't throw. (A separate call to isReadOnly is not necessary.)

      Returns:
      the array
      Throws:
      ReadOnlyBufferException - if the buffer is read-only UnsupportedOperationException if the buffer does not expose an array
      Since:
      1.6
    • arrayOffset

      public abstract int arrayOffset()
      Returns the offset into the array returned by array of the first element of the buffer (optional operation). The backing array (if there is one) is not necessarily the same size as the buffer, and position 0 in the buffer is not necessarily the 0th element in the array. Use buffer.array()[offset + buffer.arrayOffset() to access element offset in buffer.

      Use hasArray to ensure this method won't throw. (A separate call to isReadOnly is not necessary.)

      Returns:
      the offset
      Throws:
      ReadOnlyBufferException - if the buffer is read-only UnsupportedOperationException if the buffer does not expose an array
      Since:
      1.6
    • capacity

      public final int capacity()
      Returns the capacity of this buffer.
      Returns:
      the number of elements that are contained in this buffer.
    • clear

      public final Buffer clear()
      Clears this buffer.

      While the content of this buffer is not changed, the following internal changes take place: the current position is reset back to the start of the buffer, the value of the buffer limit is made equal to the capacity and mark is cleared.

      Returns:
      this buffer.
    • flip

      public final Buffer flip()
      Flips this buffer.

      The limit is set to the current position, then the position is set to zero, and the mark is cleared.

      The content of this buffer is not changed.

      Returns:
      this buffer.
    • hasArray

      public abstract boolean hasArray()
      Returns true if array and arrayOffset won't throw. This method does not return true for buffers not backed by arrays because the other methods would throw UnsupportedOperationException, nor does it return true for buffers backed by read-only arrays, because the other methods would throw ReadOnlyBufferException.
      Since:
      1.6
    • hasRemaining

      public final boolean hasRemaining()
      Indicates if there are elements remaining in this buffer, that is if position < limit.
      Returns:
      true if there are elements remaining in this buffer, false otherwise.
    • isDirect

      public abstract boolean isDirect()
      Returns true if this is a direct buffer.
      Since:
      1.6
    • isReadOnly

      public abstract boolean isReadOnly()
      Indicates whether this buffer is read-only.
      Returns:
      true if this buffer is read-only, false otherwise.
    • limit

      public final int limit()
      Returns the limit of this buffer.
      Returns:
      the limit of this buffer.
    • limit

      public final Buffer limit​(int newLimit)
      Sets the limit of this buffer.

      If the current position in the buffer is in excess of newLimit then, on returning from this call, it will have been adjusted to be equivalent to newLimit. If the mark is set and is greater than the new limit, then it is cleared.

      Parameters:
      newLimit - the new limit, must not be negative and not greater than capacity.
      Returns:
      this buffer.
      Throws:
      IllegalArgumentException - if newLimit is invalid.
    • mark

      public final Buffer mark()
      Marks the current position, so that the position may return to this point later by calling reset().
      Returns:
      this buffer.
    • position

      public final int position()
      Returns the position of this buffer.
      Returns:
      the value of this buffer's current position.
    • position

      public final Buffer position​(int newPosition)
      Sets the position of this buffer.

      If the mark is set and it is greater than the new position, then it is cleared.

      Parameters:
      newPosition - the new position, must be not negative and not greater than limit.
      Returns:
      this buffer.
      Throws:
      IllegalArgumentException - if newPosition is invalid.
    • remaining

      public final int remaining()
      Returns the number of remaining elements in this buffer, that is limit - position.
      Returns:
      the number of remaining elements in this buffer.
    • reset

      public final Buffer reset()
      Resets the position of this buffer to the mark.
      Returns:
      this buffer.
      Throws:
      InvalidMarkException - if the mark is not set.
    • rewind

      public final Buffer rewind()
      Rewinds this buffer.

      The position is set to zero, and the mark is cleared. The content of this buffer is not changed.

      Returns:
      this buffer.
    • toString

      public String toString()
      Returns a string describing this buffer.
      Overrides:
      toString in class Object
      Returns:
      a printable representation of this object.