Class ByteBuffer
- All Implemented Interfaces:
Comparable<ByteBuffer>
- Direct Known Subclasses:
MappedByteBuffer
public abstract class ByteBuffer extends Buffer implements Comparable<ByteBuffer>
-
Method Summary
Modifier and Type Method Description static ByteBufferallocate(int capacity)Creates a byte buffer based on a newly allocated byte array.static ByteBufferallocateDirect(int capacity)Creates a direct byte buffer based on a newly allocated memory block.byte[]array()Returns the byte array which this buffer is based on, if there is one.intarrayOffset()Returns the offset of the byte array which this buffer is based on, if there is one.abstract CharBufferasCharBuffer()Returns a char buffer which is based on the remaining content of this byte buffer.abstract DoubleBufferasDoubleBuffer()Returns a double buffer which is based on the remaining content of this byte buffer.abstract FloatBufferasFloatBuffer()Returns a float buffer which is based on the remaining content of this byte buffer.abstract IntBufferasIntBuffer()Returns a int buffer which is based on the remaining content of this byte buffer.abstract LongBufferasLongBuffer()Returns a long buffer which is based on the remaining content of this byte buffer.abstract ByteBufferasReadOnlyBuffer()Returns a read-only buffer that shares its content with this buffer.abstract ShortBufferasShortBuffer()Returns a short buffer which is based on the remaining content of this byte buffer.abstract ByteBuffercompact()Compacts this byte buffer.intcompareTo(ByteBuffer otherBuffer)Compares the remaining bytes of this buffer to another byte buffer's remaining bytes.abstract ByteBufferduplicate()Returns a duplicated buffer that shares its content with this buffer.booleanequals(Object other)Checks whether this byte buffer is equal to another object.abstract byteget()Returns the byte at the current position and increases the position by 1.ByteBufferget(byte[] dst)Reads bytes from the current position into the specified byte array and increases the position by the number of bytes read.ByteBufferget(byte[] dst, int dstOffset, int byteCount)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.abstract byteget(int index)Returns the byte at the specified index and does not change the position.abstract chargetChar()Returns the char at the current position and increases the position by 2.abstract chargetChar(int index)Returns the char at the specified index.abstract doublegetDouble()Returns the double at the current position and increases the position by 8.abstract doublegetDouble(int index)Returns the double at the specified index.abstract floatgetFloat()Returns the float at the current position and increases the position by 4.abstract floatgetFloat(int index)Returns the float at the specified index.abstract intgetInt()Returns the int at the current position and increases the position by 4.abstract intgetInt(int index)Returns the int at the specified index.abstract longgetLong()Returns the long at the current position and increases the position by 8.abstract longgetLong(int index)Returns the long at the specified index.abstract shortgetShort()Returns the short at the current position and increases the position by 2.abstract shortgetShort(int index)Returns the short at the specified index.booleanhasArray()Returns true ifarrayandarrayOffsetwon't throw.inthashCode()Calculates this buffer's hash code from the remaining chars.abstract booleanisDirect()Indicates whether this buffer is direct.ByteOrderorder()Returns the byte order used by this buffer when converting bytes from/to other primitive types.ByteBufferorder(ByteOrder byteOrder)Sets the byte order of this buffer.abstract ByteBufferput(byte b)Writes the given byte to the current position and increases the position by 1.ByteBufferput(byte[] src)Writes bytes in the given byte array to the current position and increases the position by the number of bytes written.ByteBufferput(byte[] src, int srcOffset, int byteCount)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.abstract ByteBufferput(int index, byte b)Write a byte to the specified index of this buffer without changing the position.ByteBufferput(ByteBuffer src)Writes all the remaining bytes of thesrcbyte buffer to this buffer's current position, and increases both buffers' position by the number of bytes copied.abstract ByteBufferputChar(char value)Writes the given char to the current position and increases the position by 2.abstract ByteBufferputChar(int index, char value)Writes the given char to the specified index of this buffer.abstract ByteBufferputDouble(double value)Writes the given double to the current position and increases the position by 8.abstract ByteBufferputDouble(int index, double value)Writes the given double to the specified index of this buffer.abstract ByteBufferputFloat(float value)Writes the given float to the current position and increases the position by 4.abstract ByteBufferputFloat(int index, float value)Writes the given float to the specified index of this buffer.abstract ByteBufferputInt(int value)Writes the given int to the current position and increases the position by 4.abstract ByteBufferputInt(int index, int value)Writes the given int to the specified index of this buffer.abstract ByteBufferputLong(int index, long value)Writes the given long to the specified index of this buffer.abstract ByteBufferputLong(long value)Writes the given long to the current position and increases the position by 8.abstract ByteBufferputShort(int index, short value)Writes the given short to the specified index of this buffer.abstract ByteBufferputShort(short value)Writes the given short to the current position and increases the position by 2.abstract ByteBufferslice()Returns a sliced buffer that shares its content with this buffer.static ByteBufferwrap(byte[] array)Creates a new byte buffer by wrapping the given byte array.static ByteBufferwrap(byte[] array, int start, int byteCount)Creates a new byte buffer by wrapping the given byte array.
-
Method Details
-
allocate
Creates a byte buffer based on a newly allocated byte array.- Parameters:
capacity- the capacity of the new buffer- Returns:
- the created byte buffer.
- Throws:
IllegalArgumentException- ifcapacity < 0.
-
allocateDirect
Creates a direct byte buffer based on a newly allocated memory block.- Parameters:
capacity- the capacity of the new buffer- Returns:
- the created byte buffer.
- Throws:
IllegalArgumentException- ifcapacity < 0.
-
wrap
Creates a new byte buffer by wrapping the given byte array.Calling this method has the same effect as
wrap(array, 0, array.length).- Parameters:
array- the byte array which the new buffer will be based on- Returns:
- the created byte buffer.
-
wrap
Creates a new byte buffer by wrapping the given byte array.The new buffer's position will be
start, limit will bestart + byteCount, capacity will be the length of the array.- Parameters:
array- the byte array which the new buffer will be based on.start- the start index, must not be negative and not greater thanarray.length.byteCount- the length, must not be negative and not greater thanarray.length - start.- Returns:
- the created byte buffer.
- Throws:
IndexOutOfBoundsException- if eitherstartorbyteCountis invalid.
-
array
public final byte[] array()Returns the byte array which this buffer is based on, if there is one.- Specified by:
arrayin classBuffer- Returns:
- the byte array which this buffer is based on.
- Throws:
ReadOnlyBufferException- if this buffer is based on a read-only array.UnsupportedOperationException- if this buffer is not based on an array.
-
arrayOffset
public final int arrayOffset()Returns the offset of the byte array which this buffer is based on, if there is one.The offset is the index of the array which corresponds to the zero position of the buffer.
- Specified by:
arrayOffsetin classBuffer- Returns:
- the offset of the byte array which this buffer is based on.
- Throws:
ReadOnlyBufferException- if this buffer is based on a read-only array.UnsupportedOperationException- if this buffer is not based on an array.
-
asCharBuffer
Returns a char buffer which is based on the remaining content of this byte buffer.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 buffers' position, limit and mark are independent.
-
asDoubleBuffer
Returns a double buffer which is based on the remaining content of this byte buffer.The new buffer's position is zero, its limit and capacity is the number of remaining bytes divided by eight, 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 buffers' position, limit and mark are independent.
-
asFloatBuffer
Returns a float buffer which is based on the remaining content of this byte buffer.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 buffers' position, limit and mark are independent.
-
asIntBuffer
Returns a int buffer which is based on the remaining content of this byte buffer.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 buffers' position, limit and mark are independent.
-
asLongBuffer
Returns a long buffer which is based on the remaining content of this byte buffer.The new buffer's position is zero, its limit and capacity is the number of remaining bytes divided by eight, 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 buffers' position, limit and mark are independent.
-
asReadOnlyBuffer
Returns a read-only buffer that shares its content with this buffer.The returned buffer is guaranteed to be a new instance, even if this buffer is read-only itself. The new buffer's position, limit, capacity and mark are the same as this buffer.
The new buffer shares its content with this buffer, which means this buffer's change of content will be visible to the new buffer. The two buffer's position, limit and mark are independent.
- Returns:
- a read-only version of this buffer.
-
asShortBuffer
Returns a short buffer which is based on the remaining content of this byte buffer.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 buffers' position, limit and mark are independent.
-
compact
Compacts this byte buffer.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.- Returns:
this- Throws:
ReadOnlyBufferException- if no changes may be made to the contents of this buffer.
-
compareTo
Compares the remaining bytes of this buffer to another byte buffer's remaining bytes.- Specified by:
compareToin interfaceComparable<ByteBuffer>- Parameters:
otherBuffer- another byte buffer.- Returns:
- a negative value if this is less than
other; 0 if this equals toother; a positive value if this is greater thanother. - Throws:
ClassCastException- ifotheris not a byte buffer.
-
duplicate
Returns a duplicated buffer that shares its content with this buffer.The duplicated buffer's position, limit, capacity and mark are the same as this buffer's. The duplicated buffer's read-only property is the same as this buffer's.
Note that in contrast to all non-
bytebuffers, byte order is not preserved in the duplicate, and is instead set to big-endian.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 buffers' position, limit and mark are independent.
-
equals
Checks whether this byte buffer is equal to another object.If
otheris not a byte buffer thenfalseis 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.- Overrides:
equalsin classObject- Parameters:
other- the object to compare with this byte buffer.- Returns:
trueif this byte buffer is equal toother,falseotherwise.- See Also:
Object.hashCode()
-
get
public abstract byte get()Returns the byte at the current position and increases the position by 1.- Returns:
- the byte at the current position.
- Throws:
BufferUnderflowException- if the position is equal or greater than limit.
-
get
Reads bytes from the current position into the specified byte array and increases the position by the number of bytes read.Calling this method has the same effect as
get(dst, 0, dst.length).- Parameters:
dst- the destination byte array.- Returns:
this- Throws:
BufferUnderflowException- ifdst.lengthis greater thanremaining().
-
get
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.- Parameters:
dst- the target byte array.dstOffset- the offset of the byte array, must not be negative and not greater thandst.length.byteCount- the number of bytes to read, must not be negative and not greater thandst.length - dstOffset- Returns:
this- Throws:
IndexOutOfBoundsException- ifdstOffset < 0 || byteCount < 0BufferUnderflowException- ifbyteCount > remaining()
-
get
public abstract byte get(int index)Returns the byte at the specified index and does not change the position.- Parameters:
index- the index, must not be negative and less than limit.- Returns:
- the byte at the specified index.
- Throws:
IndexOutOfBoundsException- if index is invalid.
-
getChar
public abstract char getChar()Returns the char at the current position and increases the position by 2.The 2 bytes starting at the current position are composed into a char according to the current byte order and returned.
- Returns:
- the char at the current position.
- Throws:
BufferUnderflowException- if the position is greater thanlimit - 2.
-
getChar
public abstract char getChar(int index)Returns the char at the specified 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.
- Parameters:
index- the index, must not be negative and equal or less thanlimit - 2.- Returns:
- the char at the specified index.
- Throws:
IndexOutOfBoundsException- ifindexis invalid.
-
getDouble
public abstract double getDouble()Returns the double at the current position and increases the position by 8.The 8 bytes starting from the current position are composed into a double according to the current byte order and returned.
- Returns:
- the double at the current position.
- Throws:
BufferUnderflowException- if the position is greater thanlimit - 8.
-
getDouble
public abstract double getDouble(int index)Returns the double at the specified 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.
- Parameters:
index- the index, must not be negative and equal or less thanlimit - 8.- Returns:
- the double at the specified index.
- Throws:
IndexOutOfBoundsException- ifindexis invalid.
-
getFloat
public abstract float getFloat()Returns the float at the current position and increases the position by 4.The 4 bytes starting at the current position are composed into a float according to the current byte order and returned.
- Returns:
- the float at the current position.
- Throws:
BufferUnderflowException- if the position is greater thanlimit - 4.
-
getFloat
public abstract float getFloat(int index)Returns the float at the specified 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.
- Parameters:
index- the index, must not be negative and equal or less thanlimit - 4.- Returns:
- the float at the specified index.
- Throws:
IndexOutOfBoundsException- ifindexis invalid.
-
getInt
public abstract int getInt()Returns the int at the current position and increases the position by 4.The 4 bytes starting at the current position are composed into a int according to the current byte order and returned.
- Returns:
- the int at the current position.
- Throws:
BufferUnderflowException- if the position is greater thanlimit - 4.
-
getInt
public abstract int getInt(int index)Returns the int at the specified 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.
- Parameters:
index- the index, must not be negative and equal or less thanlimit - 4.- Returns:
- the int at the specified index.
- Throws:
IndexOutOfBoundsException- ifindexis invalid.
-
getLong
public abstract long getLong()Returns the long at the current position and increases the position by 8.The 8 bytes starting at the current position are composed into a long according to the current byte order and returned.
- Returns:
- the long at the current position.
- Throws:
BufferUnderflowException- if the position is greater thanlimit - 8.
-
getLong
public abstract long getLong(int index)Returns the long at the specified index.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.
- Parameters:
index- the index, must not be negative and equal or less thanlimit - 8.- Returns:
- the long at the specified index.
- Throws:
IndexOutOfBoundsException- ifindexis invalid.
-
getShort
public abstract short getShort()Returns the short at the current position and increases the position by 2.The 2 bytes starting at the current position are composed into a short according to the current byte order and returned.
- Returns:
- the short at the current position.
- Throws:
BufferUnderflowException- if the position is greater thanlimit - 2.
-
getShort
public abstract short getShort(int index)Returns the short at the specified index.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.
- Parameters:
index- the index, must not be negative and equal or less thanlimit - 2.- Returns:
- the short at the specified index.
- Throws:
IndexOutOfBoundsException- ifindexis invalid.
-
hasArray
public final boolean hasArray()Description copied from class:BufferReturns 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. -
hashCode
public int hashCode()Calculates this buffer's hash code from the remaining chars. The position, limit, capacity and mark don't affect the hash code.- Overrides:
hashCodein classObject- Returns:
- the hash code calculated from the remaining bytes.
- See Also:
Object.equals(java.lang.Object)
-
isDirect
public abstract boolean isDirect()Indicates whether this buffer is direct. -
order
Returns the byte order used by this buffer when converting bytes from/to other primitive types.The default byte order of byte buffer is always
BIG_ENDIAN- Returns:
- the byte order used by this buffer when converting bytes from/to other primitive types.
-
order
Sets the byte order of this buffer.- Parameters:
byteOrder- the byte order to set. Ifnullthen the order will beLITTLE_ENDIAN.- Returns:
this- See Also:
ByteOrder
-
put
Writes the given byte to the current position and increases the position by 1.- Parameters:
b- the byte to write.- Returns:
this- Throws:
BufferOverflowException- if position is equal or greater than limit.ReadOnlyBufferException- if no changes may be made to the contents of this buffer.
-
put
Writes bytes in the given byte array to the current position and increases the position by the number of bytes written.Calling this method has the same effect as
put(src, 0, src.length).- Parameters:
src- the source byte array.- Returns:
this- Throws:
BufferOverflowException- ifremaining()is less thansrc.length.ReadOnlyBufferException- if no changes may be made to the contents of this buffer.
-
put
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.- Parameters:
src- the source byte array.srcOffset- the offset of byte array, must not be negative and not greater thansrc.length.byteCount- the number of bytes to write, must not be negative and not greater thansrc.length - srcOffset.- Returns:
this- Throws:
BufferOverflowException- ifremaining()is less thanbyteCount.IndexOutOfBoundsException- if eithersrcOffsetorbyteCountis invalid.ReadOnlyBufferException- if no changes may be made to the contents of this buffer.
-
put
Writes all the remaining bytes of thesrcbyte buffer to this buffer's current position, and increases both buffers' position by the number of bytes copied.- Parameters:
src- the source byte buffer.- Returns:
this- Throws:
BufferOverflowException- ifsrc.remaining()is greater than this buffer'sremaining().IllegalArgumentException- ifsrcis this buffer.ReadOnlyBufferException- if no changes may be made to the contents of this buffer.
-
put
Write a byte to the specified index of this buffer without changing the position.- Parameters:
index- the index, must not be negative and less than the limit.b- the byte to write.- Returns:
this- Throws:
IndexOutOfBoundsException- ifindexis invalid.ReadOnlyBufferException- if no changes may be made to the contents of this buffer.
-
putChar
Writes the given char to the current position and increases the position by 2.The char is converted to bytes using the current byte order.
- Parameters:
value- the char to write.- Returns:
this- Throws:
BufferOverflowException- if position is greater thanlimit - 2.ReadOnlyBufferException- if no changes may be made to the contents of this buffer.
-
putChar
Writes the given char to the specified index of this buffer.The char is converted to bytes using the current byte order. The position is not changed.
- Parameters:
index- the index, must not be negative and equal or less thanlimit - 2.value- the char to write.- Returns:
this- Throws:
IndexOutOfBoundsException- ifindexis invalid.ReadOnlyBufferException- if no changes may be made to the contents of this buffer.
-
putDouble
Writes the given double to the current position and increases the position by 8.The double is converted to bytes using the current byte order.
- Parameters:
value- the double to write.- Returns:
this- Throws:
BufferOverflowException- if position is greater thanlimit - 8.ReadOnlyBufferException- if no changes may be made to the contents of this buffer.
-
putDouble
Writes the given double to the specified index of this buffer.The double is converted to bytes using the current byte order. The position is not changed.
- Parameters:
index- the index, must not be negative and equal or less thanlimit - 8.value- the double to write.- Returns:
this- Throws:
IndexOutOfBoundsException- ifindexis invalid.ReadOnlyBufferException- if no changes may be made to the contents of this buffer.
-
putFloat
Writes the given float to the current position and increases the position by 4.The float is converted to bytes using the current byte order.
- Parameters:
value- the float to write.- Returns:
this- Throws:
BufferOverflowException- if position is greater thanlimit - 4.ReadOnlyBufferException- if no changes may be made to the contents of this buffer.
-
putFloat
Writes the given float to the specified index of this buffer.The float is converted to bytes using the current byte order. The position is not changed.
- Parameters:
index- the index, must not be negative and equal or less thanlimit - 4.value- the float to write.- Returns:
this- Throws:
IndexOutOfBoundsException- ifindexis invalid.ReadOnlyBufferException- if no changes may be made to the contents of this buffer.
-
putInt
Writes the given int to the current position and increases the position by 4.The int is converted to bytes using the current byte order.
- Parameters:
value- the int to write.- Returns:
this- Throws:
BufferOverflowException- if position is greater thanlimit - 4.ReadOnlyBufferException- if no changes may be made to the contents of this buffer.
-
putInt
Writes the given int to the specified index of this buffer.The int is converted to bytes using the current byte order. The position is not changed.
- Parameters:
index- the index, must not be negative and equal or less thanlimit - 4.value- the int to write.- Returns:
this- Throws:
IndexOutOfBoundsException- ifindexis invalid.ReadOnlyBufferException- if no changes may be made to the contents of this buffer.
-
putLong
Writes the given long to the current position and increases the position by 8.The long is converted to bytes using the current byte order.
- Parameters:
value- the long to write.- Returns:
this- Throws:
BufferOverflowException- if position is greater thanlimit - 8.ReadOnlyBufferException- if no changes may be made to the contents of this buffer.
-
putLong
Writes the given long to the specified index of this buffer.The long is converted to bytes using the current byte order. The position is not changed.
- Parameters:
index- the index, must not be negative and equal or less thanlimit - 8.value- the long to write.- Returns:
this- Throws:
IndexOutOfBoundsException- ifindexis invalid.ReadOnlyBufferException- if no changes may be made to the contents of this buffer.
-
putShort
Writes the given short to the current position and increases the position by 2.The short is converted to bytes using the current byte order.
- Parameters:
value- the short to write.- Returns:
this- Throws:
BufferOverflowException- if position is greater thanlimit - 2.ReadOnlyBufferException- if no changes may be made to the contents of this buffer.
-
putShort
Writes the given short to the specified index of this buffer.The short is converted to bytes using the current byte order. The position is not changed.
- Parameters:
index- the index, must not be negative and equal or less thanlimit - 2.value- the short to write.- Returns:
this- Throws:
IndexOutOfBoundsException- ifindexis invalid.ReadOnlyBufferException- if no changes may be made to the contents of this buffer.
-
slice
Returns a sliced buffer that shares its content with this buffer.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 buffers' position, limit and mark are independent.
-