This class defines six categories of operations upon byte buffers:
Absolute and relative get
and
put
methods that read and write
single bytes;
Absolute and relative bulk get
methods that transfer contiguous sequences of bytes from this buffer
into an array;
Absolute and relative bulk put
methods that transfer contiguous sequences of bytes from a
byte array or some other byte
buffer into this buffer;
Absolute and relative get
and put
methods that read and
write values of other primitive types, translating them to and from
sequences of bytes in a particular byte order;
Methods for creating view buffers, which allow a byte buffer to be viewed as a buffer containing values of some other primitive type; and
A method for compacting
a byte buffer.
Byte buffers can be created either by allocation
, which allocates space for the buffer's
content, or by wrapping
an
existing byte array into a buffer.
A byte buffer is either direct or non-direct. Given a direct byte buffer, the Java virtual machine will make a best effort to perform native I/O operations directly upon it. That is, it will attempt to avoid copying the buffer's content to (or from) an intermediate buffer before (or after) each invocation of one of the underlying operating system's native I/O operations.
A direct byte buffer may be created by invoking the allocateDirect
factory method of this class. The
buffers returned by this method typically have somewhat higher allocation
and deallocation costs than non-direct buffers. The contents of direct
buffers may reside outside of the normal garbage-collected heap, and so
their impact upon the memory footprint of an application might not be
obvious. It is therefore recommended that direct buffers be allocated
primarily for large, long-lived buffers that are subject to the underlying
system's native I/O operations. In general it is best to allocate direct
buffers only when they yield a measurable gain in program performance.
A direct byte buffer may also be created by mapping
a region of a file
directly into memory. An implementation of the Java platform may optionally
support the creation of direct byte buffers from native code via JNI. If an
instance of one of these kinds of buffers refers to an inaccessible region
of memory then an attempt to access that region will not change the buffer's
content and will cause an unspecified exception to be thrown either at the
time of the access or at some later time.
Whether a byte buffer is direct or non-direct may be determined by
invoking its isDirect
method. This method is provided so
that explicit buffer management can be done in performance-critical code.
This class defines methods for reading and writing values of all other
primitive types, except boolean
. Primitive values are translated
to (or from) sequences of bytes according to the buffer's current byte
order, which may be retrieved and modified via the order
methods. Specific byte orders are represented by instances of the ByteOrder
class. The initial order of a byte buffer is always BIG_ENDIAN
.
For access to heterogeneous binary data, that is, sequences of values of different types, this class defines a family of absolute and relative get and put methods for each type. For 32-bit floating-point values, for example, this class defines:
float getFloat() float getFloat(int index) ByteBuffer putFloat(float f) ByteBuffer putFloat(int index, float f)
Corresponding methods are defined for the types char,
short, int, long
, and double
. The index
parameters of the absolute get and put methods are in terms of
bytes rather than of the type being read or written.
For access to homogeneous binary data, that is, sequences of values of
the same type, this class defines methods that can create views of a
given byte buffer. A view buffer is simply another buffer whose
content is backed by the byte buffer. Changes to the byte buffer's content
will be visible in the view buffer, and vice versa; the two buffers'
position, limit, and mark values are independent. The asFloatBuffer
method, for example, creates an instance of
the FloatBuffer
class that is backed by the byte buffer upon which
the method is invoked. Corresponding view-creation methods are defined for
the types char, short, int, long
, and double
.
View buffers have three important advantages over the families of type-specific get and put methods described above:
A view buffer is indexed not in terms of bytes but rather in terms of the type-specific size of its values;
A view buffer provides relative bulk get and put methods that can transfer contiguous sequences of values between a buffer and an array or some other buffer of the same type; and
A view buffer is potentially much more efficient because it will be direct if, and only if, its backing byte buffer is direct.
The byte order of a view buffer is fixed to be that of its byte buffer at the time that the view is created.
Methods in this class that do not otherwise have a value to return are specified to return the buffer upon which they are invoked. This allows method invocations to be chained. The sequence of statements
bb.putInt(0xCAFEBABE); bb.putShort(3); bb.putShort(45);
bb.putInt(0xCAFEBABE).putShort(3).putShort(45);
Modifier and Type | Field and Description |
---|---|
private static final long | |
pack-priv boolean | |
pack-priv final byte[] | |
pack-priv boolean | |
pack-priv boolean | |
pack-priv final int |
Access | Constructor and Description |
---|---|
pack-priv | |
pack-priv | |
pack-priv |
Modifier and Type | Method and Description |
---|---|
public final ByteBuffer | Returns: The new byte bufferThe unit size in bytes, must be a power of unitSize)2 Creates a new byte buffer whose content is a shared and aligned subsequence of this buffer's content. |
public final int | Returns: The indexed byte's memory address modulo the unit sizeThe index to query for alignment offset, must be non-negative, no
upper bounds check is performed index, int The unit size in bytes, must be a power of unitSize)2 Returns the memory address, pointing to the byte at the given index, modulo the given unit size. |
public static ByteBuffer | Returns: The new byte bufferThe new buffer's capacity, in bytes capacity)Allocates a new byte buffer. |
public static ByteBuffer | Returns: The new byte bufferThe new buffer's capacity, in bytes capacity)Allocates a new direct byte buffer. |
public final byte[] | Returns: The array that backs this bufferImplements abstract java. Returns the byte array that backs this buffer (optional operation). |
public final int | Returns: The offset within this buffer's array of the first element of the bufferImplements abstract java. Returns the offset within this buffer's backing array of the first element of the buffer (optional operation). |
public abstract CharBuffer | |
public abstract DoubleBuffer | |
public abstract FloatBuffer | |
public abstract IntBuffer | |
public abstract LongBuffer | |
public abstract ByteBuffer | Returns: The new, read-only byte bufferCreates a new, read-only byte buffer that shares this buffer's content. |
public abstract ShortBuffer | |
pack-priv Object | |
public ByteBuffer | |
public abstract ByteBuffer | |
private static int | |
public int | Returns: A negative integer, zero, or a positive integer as this buffer is less than, equal to, or greater than the given bufferthe object to be compared. that)Implements java. Compares this buffer to another. |
public abstract ByteBuffer | Returns: The new byte bufferImplements abstract java. Creates a new byte buffer that shares this buffer's content. |
public boolean | Returns: true if, and only if, this buffer is equal to the
given objectThe object to which this buffer is to be compared ob)Overrides java. Tells whether or not this buffer is equal to another object. |
public ByteBuffer | |
public abstract byte | |
public abstract byte | Returns: The byte at the given indexThe index from which the byte will be read index)Absolute get method. |
public ByteBuffer | Returns: This bufferThe array into which bytes are to be written dst, int The offset within the array of the first byte to be
written; must be non-negative and no larger than
offset, int dst.length The maximum number of bytes to be written to the given
array; must be non-negative and no larger than
length)dst.length - offset Relative bulk get method. |
public ByteBuffer | |
public ByteBuffer | Returns: This bufferThe index in this buffer from which the first byte will be
read; must be non-negative and less than index, byte[] limit() The destination array dst, int The offset within the array of the first byte to be
written; must be non-negative and less than
offset, int dst.length The number of bytes to be written to the given array;
must be non-negative and no larger than the smaller of
length)limit() - index and dst.length - offset Absolute bulk get method. |
public ByteBuffer | Returns: This bufferThe index in this buffer from which the first byte will be
read; must be non-negative and less than index, byte[] limit() The destination array dst)Absolute bulk get method. |
private ByteBuffer | |
public abstract char | Returns: The char value at the buffer's current positionRelative get method for reading a char value. |
public abstract char | Returns: The char value at the given indexThe index from which the bytes will be read index)Absolute get method for reading a char value. |
public abstract double | Returns: The double value at the buffer's current positionRelative get method for reading a double value. |
public abstract double | Returns: The double value at the given indexThe index from which the bytes will be read index)Absolute get method for reading a double value. |
public abstract float | Returns: The float value at the buffer's current positionRelative get method for reading a float value. |
public abstract float | Returns: The float value at the given indexThe index from which the bytes will be read index)Absolute get method for reading a float value. |
public abstract int | Returns: The int value at the buffer's current positionRelative get method for reading an int value. |
public abstract int | Returns: The int value at the given indexThe index from which the bytes will be read index)Absolute get method for reading an int value. |
public abstract long | Returns: The long value at the buffer's current positionRelative get method for reading a long value. |
public abstract long | Returns: The long value at the given indexThe index from which the bytes will be read index)Absolute get method for reading a long value. |
public abstract short | Returns: The short value at the buffer's current positionRelative get method for reading a short value. |
public abstract short | Returns: The short value at the given indexThe index from which the bytes will be read index)Absolute get method for reading a short value. |
public final boolean | Returns: true if, and only if, this buffer
is backed by an array and is not read-onlyImplements abstract java. Tells whether or not this buffer is backed by an accessible byte array. |
public int | Returns: The current hash code of this bufferOverrides java. Returns the current hash code of this buffer. |
public abstract boolean | Returns: true if, and only if, this buffer is directImplements abstract java. Tells whether or not this byte buffer is direct. |
public ByteBuffer | limit(int
The new limit value; must be non-negative
and no larger than this buffer's capacity newLimit)Overrides java. Sets this buffer's limit. |
public ByteBuffer | |
public int | Returns: The relative index of the first mismatch between this and the given buffer, otherwise -1 if no mismatch.The byte buffer to be tested for a mismatch with this buffer that)Finds and returns the relative index of the first mismatch between this buffer and a given buffer. |
public final ByteOrder | |
public final ByteBuffer | Returns: This bufferThe new byte order,
either bo)BIG_ENDIAN
or LITTLE_ENDIAN Modifies this buffer's byte order. |
public ByteBuffer | position(int
The new position value; must be non-negative
and no larger than the current limit newPosition)Overrides java. Sets this buffer's position. |
public abstract ByteBuffer | |
public abstract ByteBuffer | Returns: This bufferThe index at which the byte will be written index, byte The byte value to be written b)Absolute put method (optional operation). |
public ByteBuffer | Returns: This bufferThe source buffer from which bytes are to be read;
must not be this buffer src)Relative bulk put method (optional operation). |
public ByteBuffer | Returns: This bufferThe index in this buffer at which the first byte will be
written; must be non-negative and less than index, ByteBuffer limit() The buffer from which bytes are to be read src, int The index within the source buffer of the first byte to be
read; must be non-negative and less than offset, int src.limit() The number of bytes to be read from the given buffer;
must be non-negative and no larger than the smaller of
length)limit() - index and src.limit() - offset Absolute bulk put method (optional operation). |
public ByteBuffer | Returns: This bufferThe array from which bytes are to be read src, int The offset within the array of the first byte to be read;
must be non-negative and no larger than offset, int src.length The number of bytes to be read from the given array;
must be non-negative and no larger than
length)src.length - offset Relative bulk put method (optional operation). |
public final ByteBuffer | |
public ByteBuffer | Returns: This bufferThe index in this buffer at which the first byte will be
written; must be non-negative and less than index, byte[] limit() The array from which bytes are to be read src, int The offset within the array of the first byte to be read;
must be non-negative and less than offset, int src.length The number of bytes to be read from the given array;
must be non-negative and no larger than the smaller of
length)limit() - index and src.length - offset Absolute bulk put method (optional operation). |
public ByteBuffer | Returns: This bufferThe index in this buffer at which the first byte will be
written; must be non-negative and less than index, byte[] limit() The array from which bytes are to be read src)Absolute bulk put method (optional operation). |
pack-priv ByteBuffer | |
pack-priv void | |
public abstract ByteBuffer | Returns: This bufferThe char value to be written value)Relative put method for writing a char value (optional operation). |
public abstract ByteBuffer | Returns: This bufferThe index at which the bytes will be written index, char The char value to be written value)Absolute put method for writing a char value (optional operation). |
public abstract ByteBuffer | Returns: This bufferThe double value to be written value)Relative put method for writing a double value (optional operation). |
public abstract ByteBuffer | Returns: This bufferThe index at which the bytes will be written index, double The double value to be written value)Absolute put method for writing a double value (optional operation). |
public abstract ByteBuffer | Returns: This bufferThe float value to be written value)Relative put method for writing a float value (optional operation). |
public abstract ByteBuffer | Returns: This bufferThe index at which the bytes will be written index, float The float value to be written value)Absolute put method for writing a float value (optional operation). |
public abstract ByteBuffer | Returns: This bufferThe int value to be written value)Relative put method for writing an int value (optional operation). |
public abstract ByteBuffer | Returns: This bufferThe index at which the bytes will be written index, int The int value to be written value)Absolute put method for writing an int value (optional operation). |
public abstract ByteBuffer | Returns: This bufferThe long value to be written value)Relative put method for writing a long value (optional operation). |
public abstract ByteBuffer | Returns: This bufferThe index at which the bytes will be written index, long The long value to be written value)Absolute put method for writing a long value (optional operation). |
public abstract ByteBuffer | Returns: This bufferThe short value to be written value)Relative put method for writing a short value (optional operation). |
public abstract ByteBuffer | Returns: This bufferThe index at which the bytes will be written index, short The short value to be written value)Absolute put method for writing a short value (optional operation). |
public ByteBuffer | reset()
Overrides java. Resets this buffer's position to the previously-marked position. |
public ByteBuffer | |
public abstract ByteBuffer | Returns: The new byte bufferImplements abstract java. Creates a new byte buffer whose content is a shared subsequence of this buffer's content. |
public abstract ByteBuffer | Returns: The new bufferThe position in this buffer at which the content of the new
buffer will start; must be non-negative and no larger than
index, int limit() The number of elements the new buffer will contain; must be
non-negative and no larger than length)limit() - index Implements abstract java. Creates a new byte buffer whose content is a shared subsequence of this buffer's content. |
public String | Returns: A summary stringOverrides java. Returns a string summarizing the state of this buffer. |
public static ByteBuffer | Returns: The new byte bufferThe array that will back the new buffer array, int The offset of the subarray to be used; must be non-negative and
no larger than offset, int array.length . The new buffer's position
will be set to this value.The length of the subarray to be used;
must be non-negative and no larger than
length)array.length - offset .
The new buffer's limit will be set to offset + length .Wraps a byte array into a buffer. |
public static ByteBuffer | Returns: The new byte bufferThe array that will back this buffer array)Wraps a byte array into a buffer. |