Package com.google.protobuf
Class AllocatedBuffer
java.lang.Object
com.google.protobuf.AllocatedBuffer
A buffer that was allocated by a
BufferAllocator
. For every buffer, it is guaranteed that
at least one of hasArray()
or hasNioBuffer()
will be true
.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract byte[]
array()
Returns the byte array that backs this buffer (optional operation).abstract int
Returns the offset within this buffer's backing array of the first element of the buffer (optional operation).abstract boolean
hasArray()
Indicates whether this buffer contains a backing array (i.e.abstract boolean
Indicates whether this buffer contains a backingByteBuffer
(i.e.abstract int
limit()
Returns this buffer's limit.abstract ByteBuffer
Returns theByteBuffer
that backs this buffer (optional operation).abstract int
position()
Returns this buffer's position.abstract AllocatedBuffer
position
(int position) Sets this buffer's position.abstract int
Returns the number of elements between the currentposition()
and thelimit()
.static AllocatedBuffer
wrap
(byte[] bytes) Creates a newAllocatedBuffer
that is backed by the given array.static AllocatedBuffer
wrap
(byte[] bytes, int offset, int length) Creates a newAllocatedBuffer
that is backed by the given array.static AllocatedBuffer
wrap
(ByteBuffer buffer) Creates a newAllocatedBuffer
that is backed by the givenByteBuffer
.private static AllocatedBuffer
wrapNoCheck
(byte[] bytes, int offset, int length)
-
Constructor Details
-
AllocatedBuffer
AllocatedBuffer()
-
-
Method Details
-
hasNioBuffer
public abstract boolean hasNioBuffer()Indicates whether this buffer contains a backingByteBuffer
(i.e. it is safe to callnioBuffer()
). -
hasArray
public abstract boolean hasArray()Indicates whether this buffer contains a backing array (i.e. it is safe to callarray()
). -
nioBuffer
Returns theByteBuffer
that backs this buffer (optional operation).Call
hasNioBuffer()
before invoking this method in order to ensure that this buffer has a backingByteBuffer
.- Returns:
- The
ByteBuffer
that backs this buffer - Throws:
UnsupportedOperationException
- If this buffer is not backed by aByteBuffer
.
-
array
public abstract byte[] array()Returns the byte array that backs this buffer (optional operation).Call
hasArray()
before invoking this method in order to ensure that this buffer has an accessible backing array.- Returns:
- The array that backs this buffer
- Throws:
ReadOnlyBufferException
- If this buffer is backed by an array but is read-onlyUnsupportedOperationException
- If this buffer is not backed by an accessible array
-
arrayOffset
public abstract int arrayOffset()Returns the offset within this buffer's backing array of the first element of the buffer (optional operation).If this buffer is backed by an array then
position()
corresponds to the array indexposition()
+
arrayOffset()
.Invoke the
hasArray
method before invoking this method in order to ensure that this buffer has an accessible backing array.- Returns:
- The offset within this buffer's array of the first element of the buffer
- Throws:
ReadOnlyBufferException
- If this buffer is backed by an array but is read-onlyUnsupportedOperationException
- If this buffer is not backed by an accessible array
-
position
public abstract int position()Returns this buffer's position.- Returns:
- The position of this buffer
-
position
Sets this buffer's position.- Parameters:
position
- The new position value; must be non-negative and no larger than the current limit- Returns:
- This buffer
- Throws:
IllegalArgumentException
- If the preconditions onposition
do not hold
-
limit
public abstract int limit()Returns this buffer's limit.- Returns:
- The limit of this buffer
-
remaining
public abstract int remaining()Returns the number of elements between the currentposition()
and thelimit()
.- Returns:
- The number of elements remaining in this buffer
-
wrap
Creates a newAllocatedBuffer
that is backed by the given array. The returned buffer will havehasArray()
==true
,arrayOffset()
==0
,position()
==0
andlimit()
equal to the length ofbytes
. -
wrap
Creates a newAllocatedBuffer
that is backed by the given array. The returned buffer will havehasArray()
==true
,arrayOffset()
==offset
,position()
==0
andlimit()
==length
. -
wrap
Creates a newAllocatedBuffer
that is backed by the givenByteBuffer
. The returned buffer will havehasNioBuffer()
==true
. -
wrapNoCheck
-