Class Buffer
- Direct Known Subclasses:
ByteBuffer,CharBuffer,DoubleBuffer,FloatBuffer,IntBuffer,LongBuffer,ShortBuffer
public abstract class Buffer extends Object
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 Objectarray()Returns the array that backs this buffer (optional operation).abstract intarrayOffset()Returns the offset into the array returned byarrayof the first element of the buffer (optional operation).intcapacity()Returns the capacity of this buffer.Bufferclear()Clears this buffer.Bufferflip()Flips this buffer.abstract booleanhasArray()Returns true ifarrayandarrayOffsetwon't throw.booleanhasRemaining()Indicates if there are elements remaining in this buffer, that is ifposition < limit.abstract booleanisDirect()Returns true if this is a direct buffer.abstract booleanisReadOnly()Indicates whether this buffer is read-only.intlimit()Returns the limit of this buffer.Bufferlimit(int newLimit)Sets the limit of this buffer.Buffermark()Marks the current position, so that the position may return to this point later by callingreset().intposition()Returns the position of this buffer.Bufferposition(int newPosition)Sets the position of this buffer.intremaining()Returns the number of remaining elements in this buffer, that islimit - position.Bufferreset()Resets the position of this buffer to themark.Bufferrewind()Rewinds this buffer.StringtoString()Returns a string describing this buffer.
-
Method Details
-
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
hasArrayto ensure this method won't throw. (A separate call toisReadOnlyis 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 byarrayof 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. Usebuffer.array()[offset + buffer.arrayOffset()to access elementoffsetinbuffer.Use
hasArrayto ensure this method won't throw. (A separate call toisReadOnlyis 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
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
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 ifarrayandarrayOffsetwon't throw. This method does not return true for buffers not backed by arrays because the other methods would throwUnsupportedOperationException, nor does it return true for buffers backed by read-only arrays, because the other methods would throwReadOnlyBufferException.- Since:
- 1.6
-
hasRemaining
public final boolean hasRemaining()Indicates if there are elements remaining in this buffer, that is ifposition < limit.- Returns:
trueif there are elements remaining in this buffer,falseotherwise.
-
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:
trueif this buffer is read-only,falseotherwise.
-
limit
public final int limit()Returns the limit of this buffer.- Returns:
- the limit of this buffer.
-
limit
Sets the limit of this buffer.If the current position in the buffer is in excess of
newLimitthen, on returning from this call, it will have been adjusted to be equivalent tonewLimit. 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- ifnewLimitis invalid.
-
mark
Marks the current position, so that the position may return to this point later by callingreset().- Returns:
- this buffer.
-
position
public final int position()Returns the position of this buffer.- Returns:
- the value of this buffer's current position.
-
position
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- ifnewPositionis invalid.
-
remaining
public final int remaining()Returns the number of remaining elements in this buffer, that islimit - position.- Returns:
- the number of remaining elements in this buffer.
-
reset
Resets the position of this buffer to themark.- Returns:
- this buffer.
- Throws:
InvalidMarkException- if the mark is not set.
-
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
Returns a string describing this buffer.
-