public final class ByteBuffer extends Buffer implements Comparable<ByteBuffer>, HasArrayBufferView, TypedArrayHelper.Wrapper
| Modifier and Type | Method and Description |
|---|---|
static ByteBuffer |
allocate(int capacity)
Creates a byte buffer based on a newly allocated byte array.
|
static ByteBuffer |
allocateDirect(int capacity)
Creates a direct byte buffer based on a newly allocated memory block.
|
FloatBuffer |
asFloatBuffer()
Returns a float buffer which is based on the remaining content of this byte buffer.
|
IntBuffer |
asIntBuffer()
Returns a int buffer which is based on the remaining content of this byte buffer.
|
ShortBuffer |
asShortBuffer()
Returns a short buffer which is based on the remaining content of this byte buffer.
|
ByteBuffer |
compact()
Compacts this byte buffer.
|
int |
compareTo(ByteBuffer otherBuffer)
Compares the remaining bytes of this buffer to another byte buffer's remaining bytes.
|
ByteBuffer |
duplicate()
Returns a duplicated buffer that shares its content with this buffer.
|
boolean |
equals(Object other)
Checks whether this byte buffer is equal to another object.
|
byte |
get()
Returns the byte at the current position and increases the position by 1.
|
ByteBuffer |
get(byte[] dest)
Reads bytes from the current position into the specified byte array and increases the
position by the number of bytes read.
|
ByteBuffer |
get(byte[] dest,
int off,
int len)
Reads bytes from the current position into the specified byte array, starting at the
specified offset, and increases the position by the number of bytes read.
|
byte |
get(int index)
Returns the byte at the specified index and does not change the position.
|
char |
getChar()
Returns the char at the current position and increases the position by 2.
|
char |
getChar(int index)
Returns the char at the specified index.
|
double |
getDouble()
Returns the double at the current position and increases the position by 8.
|
double |
getDouble(int index)
Returns the double at the specified index.
|
int |
getElementSize()
Returns the element size in bytes (e.g.
|
int |
getElementType()
Returns the open GL element type constant corresponding to the buffer contents.
|
float |
getFloat()
Returns the float at the current position and increases the position by 4.
|
float |
getFloat(int index)
Returns the float at the specified index.
|
int |
getInt()
Returns the int at the current position and increases the position by 4.
|
int |
getInt(int index)
Returns the int at the specified index.
|
long |
getLong()
Returns the long at the current position and increases the position by 8.
|
long |
getLong(int baseOffset)
Returns the long at the specified index.
|
short |
getShort()
Returns the short at the current position and increases the position by 2.
|
short |
getShort(int baseOffset)
Returns the short at the specified index.
|
elemental2.core.ArrayBufferView |
getTypedArray() |
boolean |
hasArray()
Indicates whether this buffer is based on a byte array and provides read/write access.
|
int |
hashCode()
Calculates this buffer's hash code from the remaining chars.
|
boolean |
isDirect()
Indicates whether this buffer is direct.
|
boolean |
isReadOnly()
Indicates whether this buffer is read-only.
|
ByteOrder |
order()
Returns the byte order used by this buffer when converting bytes from/to other primitive
types.
|
ByteBuffer |
order(ByteOrder byteOrder)
Sets the byte order of this buffer.
|
ByteBuffer |
put(byte b)
Writes the given byte to the current position and increases the position by 1.
|
ByteBuffer |
put(byte[] src)
Writes bytes in the given byte array to the current position and increases the position by
the number of bytes written.
|
ByteBuffer |
put(byte[] src,
int off,
int len)
Writes bytes in the given byte array, starting from the specified offset, to the current
position and increases the position by the number of bytes written.
|
ByteBuffer |
put(ByteBuffer src)
Writes all the remaining bytes of the
src byte buffer to this buffer's current
position, and increases both buffers' position by the number of bytes copied. |
ByteBuffer |
put(int index,
byte b)
Write a byte to the specified index of this buffer without changing the position.
|
ByteBuffer |
putChar(char value)
Writes the given char to the current position and increases the position by 2.
|
ByteBuffer |
putChar(int index,
char value)
Writes the given char to the specified index of this buffer.
|
ByteBuffer |
putDouble(double value)
Writes the given double to the current position and increases the position by 8.
|
ByteBuffer |
putDouble(int index,
double value)
Writes the given double to the specified index of this buffer.
|
ByteBuffer |
putFloat(float value)
Writes the given float to the current position and increases the position by 4.
|
ByteBuffer |
putFloat(int index,
float value)
Writes the given float to the specified index of this buffer.
|
ByteBuffer |
putInt(int value)
Writes the given int to the current position and increases the position by 4.
|
ByteBuffer |
putInt(int baseOffset,
int value)
Writes the given int to the specified index of this buffer.
|
ByteBuffer |
putLong(int baseOffset,
long value)
Writes the given long to the specified index of this buffer.
|
ByteBuffer |
putLong(long value)
Writes the given long to the current position and increases the position by 8.
|
ByteBuffer |
putShort(int baseOffset,
short value)
Writes the given short to the specified index of this buffer.
|
ByteBuffer |
putShort(short value)
Writes the given short to the current position and increases the position by 2.
|
ByteBuffer |
slice()
Returns a sliced buffer that shares its content with this buffer.
|
String |
toString()
Returns a string representing the state of this byte buffer.
|
ByteBuffer |
wrap(elemental2.core.ArrayBuffer arrayBuffer) |
static ByteBuffer |
wrapArrayBuffer(elemental2.core.ArrayBuffer arrayBuffer) |
public static ByteBuffer allocate(int capacity)
capacity - the capacity of the new bufferIllegalArgumentException - if capacity < 0.public static ByteBuffer allocateDirect(int capacity)
capacity - the capacity of the new bufferIllegalArgumentException - if capacity < 0.public static ByteBuffer wrapArrayBuffer(elemental2.core.ArrayBuffer arrayBuffer)
public ByteBuffer wrap(elemental2.core.ArrayBuffer arrayBuffer)
wrap in interface TypedArrayHelper.Wrapperpublic FloatBuffer asFloatBuffer()
The new buffer's position is zero, its limit and capacity is the number of remaining bytes divided by four, and its mark is not set. The new buffer's read-only property and byte order are the same as this buffer's. The new buffer is direct if this byte buffer is direct.
The new buffer shares its content with this buffer, which means either buffer's change of content will be visible to the other. The two buffer's position, limit and mark are independent.
public IntBuffer asIntBuffer()
The new buffer's position is zero, its limit and capacity is the number of remaining bytes divided by four, and its mark is not set. The new buffer's read-only property and byte order are the same as this buffer's. The new buffer is direct if this byte buffer is direct.
The new buffer shares its content with this buffer, which means either buffer's change of content will be visible to the other. The two buffer's position, limit and mark are independent.
public ShortBuffer asShortBuffer()
The new buffer's position is zero, its limit and capacity is the number of remaining bytes divided by two, and its mark is not set. The new buffer's read-only property and byte order are the same as this buffer's. The new buffer is direct if this byte buffer is direct.
The new buffer shares its content with this buffer, which means either buffer's change of content will be visible to the other. The two buffer's position, limit and mark are independent.
public ByteBuffer compact()
The remaining bytes will be moved to the head of the buffer, starting from position
zero. Then the position is set to remaining(); the limit is set to capacity; the
mark is cleared.
ReadOnlyBufferException - if no changes may be made to the contents of this buffer.public int compareTo(ByteBuffer otherBuffer)
compareTo in interface Comparable<ByteBuffer>otherBuffer - another byte buffer.other; 0 if this equals to other; a positive value if this is greater than other.ClassCastException - if other is not a byte buffer.public ByteBuffer duplicate()
The duplicated buffer's position, limit, capacity and mark are the same as this buffer's. The duplicated buffer's read-only property and byte order are the same as this buffer's too.
The new buffer shares its content with this buffer, which means either buffer's change of content will be visible to the other. The two buffer's position, limit and mark are independent.
public boolean equals(Object other)
If other is not a byte buffer then false is returned. Two byte buffers
are equal if and only if their remaining bytes are exactly the same. Position, limit,
capacity and mark are not considered.
public final byte get()
BufferUnderflowException - if the position is equal or greater than limit.public ByteBuffer get(byte[] dest)
Calling this method has the same effect as get(dest, 0, dest.length).
dest - the destination byte array.BufferUnderflowException - if dest.length is greater than remaining().public final ByteBuffer get(byte[] dest, int off, int len)
dest - the target byte array.off - the offset of the byte array, must not be negative and not greater than dest.length.len - the number of bytes to read, must not be negative and not greater than dest.length - offIndexOutOfBoundsException - if either off or len is invalid.BufferUnderflowException - if len is greater than remaining().public final byte get(int index)
index - the index, must not be negative and less than limit.IndexOutOfBoundsException - if index is invalid.public final char getChar()
The 2 bytes starting at the current position are composed into a char according to the current byte order and returned.
BufferUnderflowException - if the position is greater than limit - 2.public final char getChar(int index)
The 2 bytes starting from the specified index are composed into a char according to the current byte order and returned. The position is not changed.
index - the index, must not be negative and equal or less than limit - 2.IndexOutOfBoundsException - if index is invalid.public final double getDouble()
The 8 bytes starting from the current position are composed into a double according to the current byte order and returned.
BufferUnderflowException - if the position is greater than limit - 8.public final double getDouble(int index)
The 8 bytes starting at the specified index are composed into a double according to the current byte order and returned. The position is not changed.
index - the index, must not be negative and equal or less than limit - 8.IndexOutOfBoundsException - if index is invalid.public final float getFloat()
The 4 bytes starting at the current position are composed into a float according to the current byte order and returned.
BufferUnderflowException - if the position is greater than limit - 4.public final float getFloat(int index)
The 4 bytes starting at the specified index are composed into a float according to the current byte order and returned. The position is not changed.
index - the index, must not be negative and equal or less than limit - 4.IndexOutOfBoundsException - if index is invalid.public final int getInt()
The 4 bytes starting at the current position are composed into a int according to the current byte order and returned.
BufferUnderflowException - if the position is greater than limit - 4.public final int getInt(int index)
The 4 bytes starting at the specified index are composed into a int according to the current byte order and returned. The position is not changed.
index - the index, must not be negative and equal or less than limit - 4.IndexOutOfBoundsException - if index is invalid.public final long getLong()
The 8 bytes starting at the current position are composed into a long according to the current byte order and returned.
BufferUnderflowException - if the position is greater than limit - 8.public final long getLong(int baseOffset)
The 8 bytes starting at the specified index are composed into a long according to the current byte order and returned. The position is not changed.
index - the index, must not be negative and equal or less than limit - 8.IndexOutOfBoundsException - if index is invalid.public final short getShort()
The 2 bytes starting at the current position are composed into a short according to the current byte order and returned.
BufferUnderflowException - if the position is greater than limit - 2.public final short getShort(int baseOffset)
The 2 bytes starting at the specified index are composed into a short according to the current byte order and returned. The position is not changed.
index - the index, must not be negative and equal or less than limit - 2.IndexOutOfBoundsException - if index is invalid.public final boolean hasArray()
true if this buffer is based on a byte array and provides read/write access,
false otherwise.public int hashCode()
public final boolean isDirect()
true if this buffer is direct, false otherwise.public final ByteOrder order()
The default byte order of byte buffer is always BIG_ENDIAN
public final ByteBuffer order(ByteOrder byteOrder)
byteOrder - the byte order to set. If null then the order will be LITTLE_ENDIAN .ByteOrderpublic ByteBuffer put(byte b)
b - the byte to write.BufferOverflowException - if position is equal or greater than limit.ReadOnlyBufferException - if no changes may be made to the contents of this buffer.public final ByteBuffer put(byte[] src)
Calling this method has the same effect as put(src,
0, src.length).
src - the source byte array.BufferOverflowException - if remaining() is less than src.length.ReadOnlyBufferException - if no changes may be made to the contents of this buffer.public ByteBuffer put(byte[] src, int off, int len)
src - the source byte array.off - the offset of byte array, must not be negative and not greater than src.length.len - the number of bytes to write, must not be negative and not greater than src.length - off.BufferOverflowException - if remaining() is less than len.IndexOutOfBoundsException - if either off or len is invalid.ReadOnlyBufferException - if no changes may be made to the contents of this buffer.public ByteBuffer put(ByteBuffer src)
src byte buffer to this buffer's current
position, and increases both buffers' position by the number of bytes copied.src - the source byte buffer.BufferOverflowException - if src.remaining() is greater than this buffer's
remaining().IllegalArgumentException - if src is this buffer.ReadOnlyBufferException - if no changes may be made to the contents of this buffer.public ByteBuffer put(int index, byte b)
index - the index, must not be negative and less than the limit.b - the byte to write.IndexOutOfBoundsException - if index is invalid.ReadOnlyBufferException - if no changes may be made to the contents of this buffer.public final ByteBuffer putChar(char value)
The char is converted to bytes using the current byte order.
value - the char to write.BufferOverflowException - if position is greater than limit - 2.ReadOnlyBufferException - if no changes may be made to the contents of this buffer.public final ByteBuffer putChar(int index, char value)
The char is converted to bytes using the current byte order. The position is not changed.
index - the index, must not be negative and equal or less than limit - 2.value - the char to write.IndexOutOfBoundsException - if index is invalid.ReadOnlyBufferException - if no changes may be made to the contents of this buffer.public ByteBuffer putDouble(double value)
The double is converted to bytes using the current byte order.
value - the double to write.BufferOverflowException - if position is greater than limit - 8.ReadOnlyBufferException - if no changes may be made to the contents of this buffer.public ByteBuffer putDouble(int index, double value)
The double is converted to bytes using the current byte order. The position is not changed.
index - the index, must not be negative and equal or less than limit - 8.value - the double to write.IndexOutOfBoundsException - if index is invalid.ReadOnlyBufferException - if no changes may be made to the contents of this buffer.public ByteBuffer putFloat(float value)
The float is converted to bytes using the current byte order.
value - the float to write.BufferOverflowException - if position is greater than limit - 4.ReadOnlyBufferException - if no changes may be made to the contents of this buffer.public ByteBuffer putFloat(int index, float value)
The float is converted to bytes using the current byte order. The position is not changed.
index - the index, must not be negative and equal or less than limit - 4.value - the float to write.IndexOutOfBoundsException - if index is invalid.ReadOnlyBufferException - if no changes may be made to the contents of this buffer.public ByteBuffer putInt(int value)
The int is converted to bytes using the current byte order.
value - the int to write.BufferOverflowException - if position is greater than limit - 4.ReadOnlyBufferException - if no changes may be made to the contents of this buffer.public final ByteBuffer putInt(int baseOffset, int value)
The int is converted to bytes using the current byte order. The position is not changed.
index - the index, must not be negative and equal or less than limit - 4.value - the int to write.IndexOutOfBoundsException - if index is invalid.ReadOnlyBufferException - if no changes may be made to the contents of this buffer.public ByteBuffer putLong(long value)
The long is converted to bytes using the current byte order.
value - the long to write.BufferOverflowException - if position is greater than limit - 8.ReadOnlyBufferException - if no changes may be made to the contents of this buffer.public final ByteBuffer putLong(int baseOffset, long value)
The long is converted to bytes using the current byte order. The position is not changed.
index - the index, must not be negative and equal or less than limit - 8.value - the long to write.IndexOutOfBoundsException - if index is invalid.ReadOnlyBufferException - if no changes may be made to the contents of this buffer.public ByteBuffer putShort(short value)
The short is converted to bytes using the current byte order.
value - the short to write.BufferOverflowException - if position is greater than limit - 2.ReadOnlyBufferException - if no changes may be made to the contents of this buffer.public final ByteBuffer putShort(int baseOffset, short value)
The short is converted to bytes using the current byte order. The position is not changed.
index - the index, must not be negative and equal or less than limit - 2.value - the short to write.IndexOutOfBoundsException - if index is invalid.ReadOnlyBufferException - if no changes may be made to the contents of this buffer.public ByteBuffer slice()
The sliced buffer's capacity will be this buffer's remaining(), and it's zero
position will correspond to this buffer's current position. The new buffer's position will
be 0, limit will be its capacity, and its mark is cleared. The new buffer's read-only
property and byte order are the same as this buffer's.
The new buffer shares its content with this buffer, which means either buffer's change of content will be visible to the other. The two buffer's position, limit and mark are independent.
public String toString()
public elemental2.core.ArrayBufferView getTypedArray()
getTypedArray in interface HasArrayBufferViewpublic int getElementSize()
HasArrayBufferViewgetElementSize in interface HasArrayBufferViewpublic int getElementType()
HasArrayBufferViewgetElementType in interface HasArrayBufferViewpublic boolean isReadOnly()
BufferisReadOnly in class Buffertrue if this buffer is read-only, false otherwise.Copyright © 2019–2020. All rights reserved.