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>

Since:
1.4

Method Summary
static ByteBuffer allocate(int capacity)
          Allocates a new ByteBuffer object with a given capacity.
static ByteBuffer allocateDirect(int capacity)
          Allocates a new direct byte buffer.
 byte[] array()
          Returns the byte array that backs this buffer.
 int arrayOffset()
          Returns the offset within this buffer's backing array of the first element.
abstract  CharBuffer asCharBuffer()
          Creates a view of this byte buffer as a char buffer.
abstract  DoubleBuffer asDoubleBuffer()
          Creates a view of this byte buffer as a double buffer.
abstract  FloatBuffer asFloatBuffer()
          Creates a view of this byte buffer as a float buffer.
abstract  IntBuffer asIntBuffer()
          Creates a view of this byte buffer as an integer buffer.
abstract  LongBuffer asLongBuffer()
          Creates a view of this byte buffer as a long buffer.
abstract  ByteBuffer asReadOnlyBuffer()
          Creates a new read-only ByteBuffer that shares this buffer's content.
abstract  ShortBuffer asShortBuffer()
          Creates a view of this byte buffer as a short buffer.
abstract  ByteBuffer compact()
          Compacts this buffer.
 int compareTo(ByteBuffer other)
          Compares two ByteBuffer objects.
abstract  ByteBuffer duplicate()
          Creates a new ByteBuffer that shares this buffer's content.
 boolean equals(Object obj)
          Checks if this buffer is equal to obj.
abstract  byte get()
          Reads the byte at this buffer's current position, and then increments the position.
 ByteBuffer get(byte[] dst)
          This method transfers bytes from this buffer into the given destination array.
 ByteBuffer get(byte[] dst, int offset, int length)
          This method transfers bytes from this buffer into the given destination array.
abstract  byte get(int index)
          Absolute get method.
abstract  char getChar()
          Relative get method for reading a character value.
abstract  char getChar(int index)
          Absolute get method for reading a character value.
abstract  double getDouble()
          Relative get method for reading a double value.
abstract  double getDouble(int index)
          Absolute get method for reading a double value.
abstract  float getFloat()
          Relative get method for reading a float value.
abstract  float getFloat(int index)
          Absolute get method for reading a float value.
abstract  int getInt()
          Relative get method for reading an integer value.
abstract  int getInt(int index)
          Absolute get method for reading an integer value.
abstract  long getLong()
          Relative get method for reading a long value.
abstract  long getLong(int index)
          Absolute get method for reading a long value.
abstract  short getShort()
          Relative get method for reading a short value.
abstract  short getShort(int index)
          Absolute get method for reading a short value.
 boolean hasArray()
          Tells whether ot not this buffer is backed by an accessible byte array.
 int hashCode()
          Calculates a hash code for this buffer.
abstract  boolean isDirect()
          Tells whether or not this buffer is direct.
 ByteOrder order()
          Returns the byte order of this buffer.
 ByteBuffer order(ByteOrder endian)
          Modifies this buffer's byte order.
abstract  ByteBuffer put(byte b)
          Writes the byte at this buffer's current position, and then increments the position.
 ByteBuffer put(byte[] src)
          Writes the content of the the byte array src into the buffer.
 ByteBuffer put(byte[] src, int offset, int length)
          Writes the content of the the byte array src into the buffer.
 ByteBuffer put(ByteBuffer src)
          Writes the content of the the ByteBUFFER src into the buffer.
abstract  ByteBuffer put(int index, byte b)
          Absolute put method.
abstract  ByteBuffer putChar(char value)
          Relative put method for writing a character value.
abstract  ByteBuffer putChar(int index, char value)
          Absolute put method for writing a character value.
abstract  ByteBuffer putDouble(double value)
          Relative put method for writing a double value.
abstract  ByteBuffer putDouble(int index, double value)
          Absolute put method for writing a double value.
abstract  ByteBuffer putFloat(float value)
          Relative put method for writing a float value.
abstract  ByteBuffer putFloat(int index, float value)
          Relative put method for writing a float value.
abstract  ByteBuffer putInt(int value)
          Relative put method for writing an integer value.
abstract  ByteBuffer putInt(int index, int value)
          Absolute put method for writing an integer value.
abstract  ByteBuffer putLong(int index, long value)
          Absolute put method for writing a float value.
abstract  ByteBuffer putLong(long value)
          Relative put method for writing a long value.
abstract  ByteBuffer putShort(int index, short value)
          Absolute put method for writing a short value.
abstract  ByteBuffer putShort(short value)
          Relative put method for writing a short value.
abstract  ByteBuffer slice()
          Creates a new ByteBuffer whose content is a shared subsequence of this buffer's content.
 String toString()
          Returns a string summarizing the state of this buffer.
static ByteBuffer wrap(byte[] array)
          Wraps a byte array into a ByteBuffer object.
static ByteBuffer wrap(byte[] array, int offset, int length)
          Wraps a byte array into a ByteBuffer object.
 
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

allocateDirect

public static ByteBuffer allocateDirect(int capacity)
Allocates a new direct byte buffer.


allocate

public static ByteBuffer allocate(int capacity)
Allocates a new ByteBuffer object with a given capacity.


wrap

public static final ByteBuffer wrap(byte[] array,
                                    int offset,
                                    int length)
Wraps a byte array into a ByteBuffer object.

Throws:
IndexOutOfBoundsException - If the preconditions on the offset and length parameters do not hold

wrap

public static final ByteBuffer wrap(byte[] array)
Wraps a byte array into a ByteBuffer object.


get

public ByteBuffer get(byte[] dst,
                      int offset,
                      int length)
This method transfers bytes from this buffer into the given destination array. Before the transfer, it checks if there are fewer than length bytes remaining in this buffer.

Parameters:
dst - The destination array
offset - The offset within the array of the first byte to be written; must be non-negative and no larger than dst.length.
length - The maximum number of bytes to be written to the given array; must be non-negative and no larger than dst.length - offset.
Throws:
BufferUnderflowException - If there are fewer than length bytes remaining in this buffer.
IndexOutOfBoundsException - If the preconditions on the offset and length parameters do not hold.

get

public ByteBuffer get(byte[] dst)
This method transfers bytes from this buffer into the given destination array.

Parameters:
dst - The byte array to write into.
Throws:
BufferUnderflowException - If there are fewer than dst.length bytes remaining in this buffer.

put

public ByteBuffer put(ByteBuffer src)
Writes the content of the the ByteBUFFER src into the buffer. Before the transfer, it checks if there is fewer than src.remaining() space remaining in this buffer.

Parameters:
src - The source data.
Throws:
BufferOverflowException - If there is insufficient space in this buffer for the remaining bytes in the source buffer.
IllegalArgumentException - If the source buffer is this buffer.
ReadOnlyBufferException - If this buffer is read-only.

put

public ByteBuffer put(byte[] src,
                      int offset,
                      int length)
Writes the content of the the byte array src into the buffer. Before the transfer, it checks if there is fewer than length space remaining in this buffer.

Parameters:
src - The array to copy into the buffer.
offset - The offset within the array of the first byte to be read; must be non-negative and no larger than src.length.
length - The number of bytes to be read from the given array; must be non-negative and no larger than src.length - offset.
Throws:
BufferOverflowException - If there is insufficient space in this buffer for the remaining bytes in the source array.
IndexOutOfBoundsException - If the preconditions on the offset and length parameters do not hold
ReadOnlyBufferException - If this buffer is read-only.

put

public final ByteBuffer put(byte[] src)
Writes the content of the the byte array src into the buffer.

Parameters:
src - The array to copy into the buffer.
Throws:
BufferOverflowException - If there is insufficient space in this buffer for the remaining bytes in the source array.
ReadOnlyBufferException - If this buffer is read-only.

hasArray

public final boolean hasArray()
Tells whether ot not this buffer is backed by an accessible byte array.


array

public final byte[] array()
Returns the byte array that backs this buffer.

Throws:
ReadOnlyBufferException - If this buffer is read-only.
UnsupportedOperationException - If this buffer is not backed by an accessible array.

arrayOffset

public final int arrayOffset()
Returns the offset within this buffer's backing array of the first element.

Throws:
ReadOnlyBufferException - If this buffer is read-only.
UnsupportedOperationException - If this buffer is not backed by an accessible array.

hashCode

public int hashCode()
Calculates a hash code for this buffer. This is done with int arithmetic, where ** represents exponentiation, by this formula:
s[position()] + 31 + (s[position()+1] + 30)*31**1 + ... + (s[limit()-1]+30)*31**(limit()-1). Where s is the buffer data. Note that the hashcode is dependent on buffer content, and therefore is not useful if the buffer content may change.

Overrides:
hashCode in class Object
Returns:
the hash code
See Also:
Object.equals(Object), System.identityHashCode(Object)

equals

public boolean equals(Object obj)
Checks if this buffer is equal to obj.

Overrides:
equals in class Object
Parameters:
obj - the Object to compare to
Returns:
whether this Object is semantically equal to another
See Also:
Object.hashCode()

compareTo

public int compareTo(ByteBuffer other)
Compares two ByteBuffer objects.

Specified by:
compareTo in interface Comparable<ByteBuffer>
Parameters:
other - the object to be compared
Returns:
an integer describing the comparison
Throws:
ClassCastException - If obj is not an object derived from ByteBuffer.

order

public final ByteOrder order()
Returns the byte order of this buffer.


order

public final ByteBuffer order(ByteOrder endian)
Modifies this buffer's byte order.


get

public abstract byte get()
Reads the byte at this buffer's current position, and then increments the position.

Throws:
BufferUnderflowException - If there are no remaining bytes in this buffer.

put

public abstract ByteBuffer put(byte b)
Writes the byte at this buffer's current position, and then increments the position.

Throws:
BufferOverflowException - If there no remaining bytes in this buffer.
ReadOnlyBufferException - If this buffer is read-only.

get

public abstract byte get(int index)
Absolute get method.

Throws:
IndexOutOfBoundsException - If index is negative or not smaller than the buffer's limit.

put

public abstract ByteBuffer put(int index,
                               byte b)
Absolute put method.

Throws:
IndexOutOfBoundsException - If index is negative or not smaller than the buffer's limit.
ReadOnlyBufferException - If this buffer is read-only.

compact

public abstract ByteBuffer compact()
Compacts this buffer.

Throws:
ReadOnlyBufferException - If this buffer is read-only.

isDirect

public abstract boolean isDirect()
Tells whether or not this buffer is direct.


slice

public abstract ByteBuffer slice()
Creates a new ByteBuffer whose content is a shared subsequence of this buffer's content.


duplicate

public abstract ByteBuffer duplicate()
Creates a new ByteBuffer that shares this buffer's content.


asReadOnlyBuffer

public abstract ByteBuffer asReadOnlyBuffer()
Creates a new read-only ByteBuffer that shares this buffer's content.


asShortBuffer

public abstract ShortBuffer asShortBuffer()
Creates a view of this byte buffer as a short buffer.


asCharBuffer

public abstract CharBuffer asCharBuffer()
Creates a view of this byte buffer as a char buffer.


asIntBuffer

public abstract IntBuffer asIntBuffer()
Creates a view of this byte buffer as an integer buffer.


asLongBuffer

public abstract LongBuffer asLongBuffer()
Creates a view of this byte buffer as a long buffer.


asFloatBuffer

public abstract FloatBuffer asFloatBuffer()
Creates a view of this byte buffer as a float buffer.


asDoubleBuffer

public abstract DoubleBuffer asDoubleBuffer()
Creates a view of this byte buffer as a double buffer.


getChar

public abstract char getChar()
Relative get method for reading a character value.

Throws:
BufferUnderflowException - If there are fewer than two bytes remaining in this buffer.

putChar

public abstract ByteBuffer putChar(char value)
Relative put method for writing a character value.

Throws:
BufferOverflowException - If this buffer's current position is not smaller than its limit.

getChar

public abstract char getChar(int index)
Absolute get method for reading a character value.

Throws:
IndexOutOfBoundsException - If there are fewer than two bytes remaining in this buffer

putChar

public abstract ByteBuffer putChar(int index,
                                   char value)
Absolute put method for writing a character value.

Throws:
IndexOutOfBoundsException - If index is negative or not smaller than the buffer's limit, minus one.

getShort

public abstract short getShort()
Relative get method for reading a short value.

Throws:
BufferUnderflowException - If index is negative or not smaller than the buffer's limit, minus one.

putShort

public abstract ByteBuffer putShort(short value)
Relative put method for writing a short value.

Throws:
BufferOverflowException - If this buffer's current position is not smaller than its limit.

getShort

public abstract short getShort(int index)
Absolute get method for reading a short value.

Throws:
IndexOutOfBoundsException - If there are fewer than two bytes remaining in this buffer

putShort

public abstract ByteBuffer putShort(int index,
                                    short value)
Absolute put method for writing a short value.

Throws:
IndexOutOfBoundsException - If index is negative or not smaller than the buffer's limit, minus one.

getInt

public abstract int getInt()
Relative get method for reading an integer value.

Throws:
BufferUnderflowException - If there are fewer than four bytes remaining in this buffer.

putInt

public abstract ByteBuffer putInt(int value)
Relative put method for writing an integer value.

Throws:
BufferOverflowException - If this buffer's current position is not smaller than its limit.

getInt

public abstract int getInt(int index)
Absolute get method for reading an integer value.

Throws:
IndexOutOfBoundsException - If index is negative or not smaller than the buffer's limit, minus three.

putInt

public abstract ByteBuffer putInt(int index,
                                  int value)
Absolute put method for writing an integer value.

Throws:
IndexOutOfBoundsException - If index is negative or not smaller than the buffer's limit, minus three.

getLong

public abstract long getLong()
Relative get method for reading a long value.

Throws:
BufferUnderflowException - If there are fewer than eight bytes remaining in this buffer.

putLong

public abstract ByteBuffer putLong(long value)
Relative put method for writing a long value.

Throws:
BufferOverflowException - If this buffer's current position is not smaller than its limit.

getLong

public abstract long getLong(int index)
Absolute get method for reading a long value.

Throws:
IndexOutOfBoundsException - If index is negative or not smaller than the buffer's limit, minus seven.

putLong

public abstract ByteBuffer putLong(int index,
                                   long value)
Absolute put method for writing a float value.

Throws:
IndexOutOfBoundsException - If index is negative or not smaller than the buffer's limit, minus seven.

getFloat

public abstract float getFloat()
Relative get method for reading a float value.

Throws:
BufferUnderflowException - If there are fewer than four bytes remaining in this buffer.

putFloat

public abstract ByteBuffer putFloat(float value)
Relative put method for writing a float value.

Throws:
BufferOverflowException - If there are fewer than four bytes remaining in this buffer.

getFloat

public abstract float getFloat(int index)
Absolute get method for reading a float value.

Throws:
IndexOutOfBoundsException - If index is negative or not smaller than the buffer's limit, minus three.

putFloat

public abstract ByteBuffer putFloat(int index,
                                    float value)
Relative put method for writing a float value.

Throws:
IndexOutOfBoundsException - If index is negative or not smaller than the buffer's limit, minus three.

getDouble

public abstract double getDouble()
Relative get method for reading a double value.

Throws:
BufferUnderflowException - If there are fewer than eight bytes remaining in this buffer.

putDouble

public abstract ByteBuffer putDouble(double value)
Relative put method for writing a double value.

Throws:
BufferOverflowException - If this buffer's current position is not smaller than its limit.

getDouble

public abstract double getDouble(int index)
Absolute get method for reading a double value.

Throws:
IndexOutOfBoundsException - If index is negative or not smaller than the buffer's limit, minus seven.

putDouble

public abstract ByteBuffer putDouble(int index,
                                     double value)
Absolute put method for writing a double value.

Throws:
IndexOutOfBoundsException - If index is negative or not smaller than the buffer's limit, minus seven.

toString

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

Overrides:
toString in class Object
Returns:
the String representing this Object, which may be null
See Also:
Object.getClass(), Object.hashCode(), Class.getName(), Integer.toHexString(int)