Final

java.nio
Class ByteBuffer

java.lang.Object
  extended by java.nio.Buffer
      extended by java.nio.ByteBuffer
All Implemented Interfaces:
Comparable<ByteBuffer>
Direct Known Subclasses:
MappedByteBuffer

public abstract class ByteBuffer
extends Buffer
implements Comparable<ByteBuffer>

A buffer of bytes.

A byte buffer can be created in either of the following ways:


Method Summary
static ByteBuffer allocate(int capacity)
          Creates a byte buffer based on a new allocated byte array.
static ByteBuffer allocateDirect(int capacity)
          Creates a direct byte buffer based on a new allocated memory block.
 byte[] array()
          Returns the byte array which this buffer is based on, if there's one.
 int arrayOffset()
          Returns the offset of the byte array which this buffer is based on, if there's one.
abstract  CharBuffer asCharBuffer()
          Returns a char buffer which is based on the remaining content of this byte buffer.
abstract  DoubleBuffer asDoubleBuffer()
          Returns a double buffer which is based on the remaining content of this byte buffer.
abstract  FloatBuffer asFloatBuffer()
          Returns a float buffer which is based on the remaining content of this byte buffer.
abstract  IntBuffer asIntBuffer()
          Returns a int buffer which is based on the remaining content of this byte buffer.
abstract  LongBuffer asLongBuffer()
          Returns a long buffer which is based on the remaining content of this byte buffer.
abstract  ByteBuffer asReadOnlyBuffer()
          Returns a readonly buffer that shares content with this buffer.
abstract  ShortBuffer asShortBuffer()
          Returns a short buffer which is based on the remaining content of this byte buffer.
abstract  ByteBuffer compact()
          Compacts this byte buffer.
 int compareTo(ByteBuffer otherBuffer)
          Compare the remaining bytes of this buffer to another byte buffer's remaining bytes.
abstract  ByteBuffer duplicate()
          Returns a duplicated buffer that shares content with this buffer.
 boolean equals(Object other)
          Tests whether this byte buffer equals to another object.
abstract  byte get()
          Returns the byte at the current position and increase the position by 1.
 ByteBuffer get(byte[] dest)
          Reads bytes from the current position into the specified byte array and increase 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 from the specified offset, and increase the position by the number of bytes read.
abstract  byte get(int index)
          Returns a byte at the specified index, and the position is not changed.
abstract  char getChar()
          Returns the char at the current position and increase the position by 2.
abstract  char getChar(int index)
          Returns the char at the specified index.
abstract  double getDouble()
          Returns the double at the current position and increase the position by 8.
abstract  double getDouble(int index)
          Returns the double at the specified index.
abstract  float getFloat()
          Returns the float at the current position and increase the position by 4.
abstract  float getFloat(int index)
          Returns the float at the specified index.
abstract  int getInt()
          Returns the int at the current position and increase the position by 4.
abstract  int getInt(int index)
          Returns the int at the specified index.
abstract  long getLong()
          Returns the long at the current position and increase the position by 8.
abstract  long getLong(int index)
          Returns the long at the specified index.
abstract  short getShort()
          Returns the short at the current position and increase the position by 2.
abstract  short getShort(int index)
          Returns the short at the specified index.
 boolean hasArray()
          Returns whether this buffer is based on a byte array and is read/write.
 int hashCode()
          Hash code is calculated from the remaining bytes.
abstract  boolean isDirect()
          Returns true if this buffer is direct.
 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.
abstract  ByteBuffer put(byte b)
          Writes the given byte to the current position and increase the position by 1.
 ByteBuffer put(byte[] src)
          Writes bytes in the given byte array to the current position and increase 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 increase 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 increase both buffers' position by the number of bytes copied.
abstract  ByteBuffer put(int index, byte b)
          Write a byte to the specified index of this buffer and the position is not changed.
abstract  ByteBuffer putChar(char value)
          Writes the given char to the current position and increase the position by 2.
abstract  ByteBuffer putChar(int index, char value)
          Write a char to the specified index of this buffer.
abstract  ByteBuffer putDouble(double value)
          Writes the given double to the current position and increase the position by 8.
abstract  ByteBuffer putDouble(int index, double value)
          Write a double to the specified index of this buffer.
abstract  ByteBuffer putFloat(float value)
          Writes the given float to the current position and increase the position by 4.
abstract  ByteBuffer putFloat(int index, float value)
          Write a float to the specified index of this buffer.
abstract  ByteBuffer putInt(int value)
          Writes the given int to the current position and increase the position by 4.
abstract  ByteBuffer putInt(int index, int value)
          Write a int to the specified index of this buffer.
abstract  ByteBuffer putLong(int index, long value)
          Write a long to the specified index of this buffer.
abstract  ByteBuffer putLong(long value)
          Writes the given long to the current position and increase the position by 8.
abstract  ByteBuffer putShort(int index, short value)
          Write a short to the specified index of this buffer.
abstract  ByteBuffer putShort(short value)
          Writes the given short to the current position and increase the position by 2.
abstract  ByteBuffer slice()
          Returns a sliced buffer that shares content with this buffer.
 String toString()
          Returns a string represents the state of this byte buffer.
static ByteBuffer wrap(byte[] array)
          Creates a new byte buffer by wrapping the given byte array.
static ByteBuffer wrap(byte[] array, int start, int len)
          Creates new a byte buffer by wrapping the given byte array.
 
Methods inherited from class java.nio.Buffer
capacity, clear, flip, hasRemaining, isReadOnly, limit, limit, mark, position, position, remaining, reset, rewind
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Method Detail

allocate

public static ByteBuffer allocate(int capacity)
Creates a byte buffer based on a new allocated byte array.

Parameters:
capacity - The capacity of the new buffer
Returns:
The created byte buffer
Throws:
IllegalArgumentException - If capacity is less than zero

allocateDirect

public static ByteBuffer allocateDirect(int capacity)
Creates a direct byte buffer based on a new allocated memory block.

Parameters:
capacity - The capacity of the new buffer
Returns:
The created byte buffer
Throws:
IllegalArgumentException - If capacity is less than zero

wrap

public static ByteBuffer wrap(byte[] array)
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

public static ByteBuffer wrap(byte[] array,
                              int start,
                              int len)
Creates new a byte buffer by wrapping the given byte array.

The new buffer's position will be start, limit will be start + len, 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 be no less than zero and no greater than array.length
len - The length, must be no less than zero and no greater than array.length - start
Returns:
The created byte buffer
Throws:
IndexOutOfBoundsException - If either start or len is invalid

array

public final byte[] array()
Returns the byte array which this buffer is based on, if there's one.

Returns:
The byte array which this buffer is based on
Throws:
ReadOnlyBufferException - If this buffer is based on a readonly 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's one.

The offset is the index of the array corresponds to the zero position of the buffer.

Returns:
The offset of the byte array which this buffer is based on
Throws:
ReadOnlyBufferException - If this buffer is based on a readonly array
UnsupportedOperationException - If this buffer is not based on an array

asCharBuffer

public abstract CharBuffer 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 readonly property and byte order are same as this buffer. The new buffer is direct, if this byte buffer is direct.

The new buffer shares 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.

Returns:
A char buffer which is based on the content of this byte buffer.

asDoubleBuffer

public abstract DoubleBuffer 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 two, and its mark is not set. The new buffer's readonly property and byte order are same as this buffer. The new buffer is direct, if this byte buffer is direct.

The new buffer shares 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.

Returns:
A double buffer which is based on the content of this byte buffer.

asFloatBuffer

public abstract FloatBuffer 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 two, and its mark is not set. The new buffer's readonly property and byte order are same as this buffer. The new buffer is direct, if this byte buffer is direct.

The new buffer shares 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.

Returns:
A float buffer which is based on the content of this byte buffer.

asIntBuffer

public abstract IntBuffer 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 two, and its mark is not set. The new buffer's readonly property and byte order are same as this buffer. The new buffer is direct, if this byte buffer is direct.

The new buffer shares 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.

Returns:
A int buffer which is based on the content of this byte buffer.

asLongBuffer

public abstract LongBuffer 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 two, and its mark is not set. The new buffer's readonly property and byte order are same as this buffer. The new buffer is direct, if this byte buffer is direct.

The new buffer shares 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.

Returns:
A long buffer which is based on the content of this byte buffer.

asReadOnlyBuffer

public abstract ByteBuffer asReadOnlyBuffer()
Returns a readonly buffer that shares content with this buffer.

The returned buffer is guaranteed to be a new instance, even if this buffer is readonly itself. The new buffer's position, limit, capacity and mark are the same as this buffer.

The new buffer shares 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 readonly version of this buffer.

asShortBuffer

public abstract ShortBuffer 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 readonly property and byte order are same as this buffer. The new buffer is direct, if this byte buffer is direct.

The new buffer shares 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.

Returns:
A short buffer which is based on the content of this byte buffer.

compact

public abstract ByteBuffer compact()
Compacts this byte buffer.

The remaining bytes will be moved to the head of the buffer, staring from position zero. Then the position is set to remaining(); the limit is set to capacity; the mark is cleared.

Returns:
This buffer
Throws:
ReadOnlyBufferException - If no changes may be made to the contents of this buffer

compareTo

public int compareTo(ByteBuffer otherBuffer)
Compare the remaining bytes of this buffer to another byte buffer's remaining bytes.

Specified by:
compareTo in interface Comparable<ByteBuffer>
Parameters:
otherBuffer - Another byte buffer
Returns:
a negative value if this is less than other; 0 if this equals to other; a positive value if this is greater than other
Throws:
ClassCastException - If other is not a byte buffer

duplicate

public abstract ByteBuffer duplicate()
Returns a duplicated buffer that shares content with this buffer.

The duplicated buffer's position, limit, capacity and mark are the same as this buffer. The duplicated buffer's readonly property and byte order are same as this buffer too.

The new buffer shares 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.

Returns:
A duplicated buffer that shares content with this buffer.

equals

public boolean equals(Object other)
Tests whether this byte buffer equals to another object.

If other is not a byte buffer, then false is returned.

Two byte buffers are equals if, and only if, their remaining bytes are exactly the same. Position, limit, capacity and mark are not considered.

Overrides:
equals in class Object
Parameters:
other - the object to compare against
Returns:
Whether this byte buffer equals to another object.
See Also:
Object.hashCode()

get

public abstract byte get()
Returns the byte at the current position and increase the position by 1.

Returns:
The byte at the current position.
Throws:
BufferUnderflowException - If the position is equal or greater than limit

get

public ByteBuffer get(byte[] dest)
Reads bytes from the current position into the specified byte array and increase the position by the number of bytes read.

Calling this method has the same effect as get(dest, 0, dest.length).

Parameters:
dest - The destination byte array
Returns:
This buffer
Throws:
BufferUnderflowException - if dest.length is greater than remaining()

get

public ByteBuffer get(byte[] dest,
                      int off,
                      int len)
Reads bytes from the current position into the specified byte array, starting from the specified offset, and increase the position by the number of bytes read.

Parameters:
dest - The target byte array
off - The offset of the byte array, must be no less than zero and no greater than dest.length
len - The number of bytes to read, must be no less than zero and no greater than dest.length - off
Returns:
This buffer
Throws:
IndexOutOfBoundsException - If either off or len is invalid
BufferUnderflowException - If len is greater than remaining()

get

public abstract byte get(int index)
Returns a byte at the specified index, and the position is not changed.

Parameters:
index - The index, must be no less than zero and less than limit
Returns:
A byte at the specified index.
Throws:
IndexOutOfBoundsException - If index is invalid

getChar

public abstract char getChar()
Returns the char at the current position and increase the position by 2.

The 2 bytes start from the current position are composed into a char according to current byte order and returned. The position increases by 2.

Returns:
The char at the current position.
Throws:
BufferUnderflowException - If the position is greater than limit - 2

getChar

public abstract char getChar(int index)
Returns the char at the specified index.

The 2 bytes start from the specified index are composed into a char according to current byte order and returned. The position is not changed.

Parameters:
index - The index, must be no less than zero and equal or less than limit - 2
Returns:
The char at the specified index.
Throws:
IndexOutOfBoundsException - If index is invalid

getDouble

public abstract double getDouble()
Returns the double at the current position and increase the position by 8.

The 8 bytes start from the current position are composed into a double according to current byte order and returned. The position increases by 8.

Returns:
The double at the current position.
Throws:
BufferUnderflowException - If the position is greater than limit - 8

getDouble

public abstract double getDouble(int index)
Returns the double at the specified index.

The 8 bytes start from the specified index are composed into a double according to current byte order and returned. The position is not changed.

Parameters:
index - The index, must be no less than zero and equal or less than limit - 8
Returns:
The double at the specified index.
Throws:
IndexOutOfBoundsException - If index is invalid

getFloat

public abstract float getFloat()
Returns the float at the current position and increase the position by 4.

The 4 bytes start from the current position are composed into a float according to current byte order and returned. The position increases by 4.

Returns:
The float at the current position.
Throws:
BufferUnderflowException - If the position is greater than limit - 4

getFloat

public abstract float getFloat(int index)
Returns the float at the specified index.

The 4 bytes start from the specified index are composed into a float according to current byte order and returned. The position is not changed.

Parameters:
index - The index, must be no less than zero and equal or less than limit - 4
Returns:
The float at the specified index.
Throws:
IndexOutOfBoundsException - If index is invalid

getInt

public abstract int getInt()
Returns the int at the current position and increase the position by 4.

The 4 bytes start from the current position are composed into a int according to current byte order and returned. The position increases by 4.

Returns:
The int at the current position.
Throws:
BufferUnderflowException - If the position is greater than limit - 4

getInt

public abstract int getInt(int index)
Returns the int at the specified index.

The 4 bytes start from the specified index are composed into a int according to current byte order and returned. The position is not changed.

Parameters:
index - The index, must be no less than zero and equal or less than limit - 4
Returns:
The int at the specified index.
Throws:
IndexOutOfBoundsException - If index is invalid

getLong

public abstract long getLong()
Returns the long at the current position and increase the position by 8.

The 8 bytes start from the current position are composed into a long according to current byte order and returned. The position increases by 8.

Returns:
The long at the current position.
Throws:
BufferUnderflowException - If the position is greater than limit - 8

getLong

public abstract long getLong(int index)
Returns the long at the specified index.

The 8 bytes start from the specified index are composed into a long according to current byte order and returned. The position is not changed.

Parameters:
index - The index, must be no less than zero and equal or less than limit - 8
Returns:
The long at the specified index.
Throws:
IndexOutOfBoundsException - If index is invalid

getShort

public abstract short getShort()
Returns the short at the current position and increase the position by 2.

The 2 bytes start from the current position are composed into a short according to current byte order and returned. The position increases by 2.

Returns:
The short at the current position.
Throws:
BufferUnderflowException - If the position is greater than limit - 2

getShort

public abstract short getShort(int index)
Returns the short at the specified index.

The 2 bytes start from the specified index are composed into a short according to current byte order and returned. The position is not changed.

Parameters:
index - The index, must be no less than zero and equal or less than limit - 2
Returns:
The short at the specified index.
Throws:
IndexOutOfBoundsException - If index is invalid

hasArray

public final boolean hasArray()
Returns whether this buffer is based on a byte array and is read/write.

If this buffer is readonly, then false is returned.

Returns:
Whether this buffer is based on a byte array and is read/write.

hashCode

public int hashCode()
Hash code is calculated from the remaining bytes.

Position, limit, capacity and mark don't affect the hash code.

Overrides:
hashCode in class Object
Returns:
The hash code calculated from the remaining bytes.
See Also:
Object.equals(java.lang.Object)

isDirect

public abstract boolean isDirect()
Returns true if this buffer is direct.

A byte buffer is direct, if it is based on a byte buffer and the byte buffer is direct.

Returns:
True if this buffer is direct.

order

public final ByteOrder 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

public final ByteBuffer order(ByteOrder byteOrder)
Sets the byte order of this buffer.

Parameters:
byteOrder - The byte order to set. If null then the order will be LITTLE_ENDIAN.
Returns:
This buffer
See Also:
ByteOrder

put

public abstract ByteBuffer put(byte b)
Writes the given byte to the current position and increase the position by 1.

Parameters:
b - The byte to write
Returns:
This buffer
Throws:
BufferOverflowException - If position is equal or greater than limit
ReadOnlyBufferException - If no changes may be made to the contents of this buffer

put

public final ByteBuffer put(byte[] src)
Writes bytes in the given byte array to the current position and increase 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 buffer
Throws:
BufferOverflowException - If remaining() is less than src.length
ReadOnlyBufferException - If no changes may be made to the contents of this buffer

put

public 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 increase the position by the number of bytes written.

Parameters:
src - The source byte array
off - The offset of byte array, must be no less than zero and no greater than src.length
len - The number of bytes to write, must be no less than zero and no greater than src.length - off
Returns:
This buffer
Throws:
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

put

public ByteBuffer put(ByteBuffer src)
Writes all the remaining bytes of the src byte buffer to this buffer's current position, and increase both buffers' position by the number of bytes copied.

Parameters:
src - The source byte buffer
Returns:
This buffer
Throws:
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

put

public abstract ByteBuffer put(int index,
                               byte b)
Write a byte to the specified index of this buffer and the position is not changed.

Parameters:
index - The index, must be no less than zero and less than the limit
b - The byte to write
Returns:
This buffer
Throws:
IndexOutOfBoundsException - If index is invalid
ReadOnlyBufferException - If no changes may be made to the contents of this buffer

putChar

public abstract ByteBuffer putChar(char value)
Writes the given char to the current position and increase the position by 2.

The char is converted to bytes using the current byte order.

Parameters:
value - The char to write
Returns:
This buffer
Throws:
BufferOverflowException - If position is greater than limit - 2
ReadOnlyBufferException - If no changes may be made to the contents of this buffer

putChar

public abstract ByteBuffer putChar(int index,
                                   char value)
Write a 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 be no less than zero and equal or less than limit - 2
value - The char to write
Returns:
This buffer
Throws:
IndexOutOfBoundsException - If index is invalid
ReadOnlyBufferException - If no changes may be made to the contents of this buffer

putDouble

public abstract ByteBuffer putDouble(double value)
Writes the given double to the current position and increase the position by 8.

The double is converted to bytes using the current byte order.

Parameters:
value - The double to write
Returns:
This buffer
Throws:
BufferOverflowException - If position is greater than limit - 8
ReadOnlyBufferException - If no changes may be made to the contents of this buffer

putDouble

public abstract ByteBuffer putDouble(int index,
                                     double value)
Write a 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 be no less than zero and equal or less than limit - 8
value - The double to write
Returns:
This buffer
Throws:
IndexOutOfBoundsException - If index is invalid
ReadOnlyBufferException - If no changes may be made to the contents of this buffer

putFloat

public abstract ByteBuffer putFloat(float value)
Writes the given float to the current position and increase the position by 4.

The float is converted to bytes using the current byte order.

Parameters:
value - The float to write
Returns:
This buffer
Throws:
BufferOverflowException - If position is greater than limit - 4
ReadOnlyBufferException - If no changes may be made to the contents of this buffer

putFloat

public abstract ByteBuffer putFloat(int index,
                                    float value)
Write a 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 be no less than zero and equal or less than limit - 4
value - The float to write
Returns:
This buffer
Throws:
IndexOutOfBoundsException - If index is invalid
ReadOnlyBufferException - If no changes may be made to the contents of this buffer

putInt

public abstract ByteBuffer putInt(int value)
Writes the given int to the current position and increase the position by 4.

The int is converted to bytes using the current byte order.

Parameters:
value - The int to write
Returns:
This buffer
Throws:
BufferOverflowException - If position is greater than limit - 4
ReadOnlyBufferException - If no changes may be made to the contents of this buffer

putInt

public abstract ByteBuffer putInt(int index,
                                  int value)
Write a 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 be no less than zero and equal or less than limit - 4
value - The int to write
Returns:
This buffer
Throws:
IndexOutOfBoundsException - If index is invalid
ReadOnlyBufferException - If no changes may be made to the contents of this buffer

putLong

public abstract ByteBuffer putLong(long value)
Writes the given long to the current position and increase the position by 8.

The long is converted to bytes using the current byte order.

Parameters:
value - The long to write
Returns:
This buffer
Throws:
BufferOverflowException - If position is greater than limit - 8
ReadOnlyBufferException - If no changes may be made to the contents of this buffer

putLong

public abstract ByteBuffer putLong(int index,
                                   long value)
Write a 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 be no less than zero and equal or less than limit - 8
value - The long to write
Returns:
This buffer
Throws:
IndexOutOfBoundsException - If index is invalid
ReadOnlyBufferException - If no changes may be made to the contents of this buffer

putShort

public abstract ByteBuffer putShort(short value)
Writes the given short to the current position and increase the position by 2.

The short is converted to bytes using the current byte order.

Parameters:
value - The short to write
Returns:
This buffer
Throws:
BufferOverflowException - If position is greater than limit - 2
ReadOnlyBufferException - If no changes may be made to the contents of this buffer

putShort

public abstract ByteBuffer putShort(int index,
                                    short value)
Write a 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 be no less than zero and equal or less than limit - 2
value - The short to write
Returns:
This buffer
Throws:
IndexOutOfBoundsException - If index is invalid
ReadOnlyBufferException - If no changes may be made to the contents of this buffer

slice

public abstract ByteBuffer slice()
Returns a sliced buffer that shares content with this buffer.

The sliced buffer's capacity will be this buffer's remaining(), and its 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 unset. The new buffer's readonly property and byte order are same as this buffer.

The new buffer shares 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.

Returns:
A sliced buffer that shares content with this buffer.

toString

public String toString()
Returns a string represents the state of this byte buffer.

Overrides:
toString in class Object
Returns:
A string represents the state of this byte buffer.

Final

Licensed Materials - Property of IBM
© Copyright IBM Corp. 2006, 2008 All Rights Reserved.