Home » openjdk-7 » java » nio » [javadoc | source]
java.nio
abstract public class: ByteBuffer [javadoc | source]
java.lang.Object
   java.nio.Buffer
      java.nio.ByteBuffer

All Implemented Interfaces:
    Comparable

Direct Known Subclasses:
    ReadOnly, MappedByteBufferImpl, MappedByteBufferAdapter, ReadWriteHeapByteBuffer, DirectByteBuffer, ReadWrite, BaseByteBuffer, ReadOnlyDirectByteBuffer, DirectByteBufferImpl, ReadOnlyHeapByteBuffer, ReadWriteDirectByteBuffer, HeapByteBuffer, ByteBufferImpl, MappedByteBuffer

A buffer for bytes.

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

Field Summary
 Endianness order    The byte order of this buffer, default is {@code BIG_ENDIAN}. 
Fields inherited from java.nio.Buffer:
address
Constructor:
 ByteBuffer(int capacity) 
Method from java.nio.ByteBuffer Summary:
allocate,   allocateDirect,   array,   arrayOffset,   asCharBuffer,   asDoubleBuffer,   asFloatBuffer,   asIntBuffer,   asLongBuffer,   asReadOnlyBuffer,   asShortBuffer,   compact,   compareTo,   duplicate,   equals,   get,   get,   get,   get,   getChar,   getChar,   getDouble,   getDouble,   getFloat,   getFloat,   getInt,   getInt,   getLong,   getLong,   getShort,   getShort,   hasArray,   hashCode,   isDirect,   order,   order,   orderImpl,   protectedArray,   protectedArrayOffset,   protectedHasArray,   put,   put,   put,   put,   put,   putChar,   putChar,   putDouble,   putDouble,   putFloat,   putFloat,   putInt,   putInt,   putLong,   putLong,   putShort,   putShort,   slice,   toString,   wrap,   wrap
Methods from java.nio.Buffer:
array,   arrayOffset,   capacity,   checkBounds,   checkIndex,   checkIndex,   clear,   discardMark,   flip,   hasArray,   hasRemaining,   isDirect,   isReadOnly,   limit,   limit,   mark,   markValue,   nextGetIndex,   nextGetIndex,   nextPutIndex,   nextPutIndex,   position,   position,   remaining,   reset,   rewind
Methods from java.lang.Object:
clone,   equals,   finalize,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from java.nio.ByteBuffer Detail:
 public static ByteBuffer allocate(int capacity) 
    Creates a byte buffer based on a newly allocated byte array.
 public static ByteBuffer allocateDirect(int capacity) 
    Creates a direct byte buffer based on a newly allocated memory block.
 public final byte[] array() 
    Returns the byte array which this buffer is based on, if there is one.
 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.

 abstract public 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 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.

 abstract public 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 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 buffer's position, limit and mark are independent.

 abstract public 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 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.

 abstract public 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 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.

 abstract public 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 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 buffer's position, limit and mark are independent.

 abstract public ByteBuffer 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.

 abstract public 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 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.

 abstract public ByteBuffer 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 {@code remaining()}; the limit is set to capacity; the mark is cleared.

 public int compareTo(ByteBuffer otherBuffer) 
    Compares the remaining bytes of this buffer to another byte buffer's remaining bytes.
 abstract public ByteBuffer 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 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) 
    Checks whether this byte buffer is equal to another object.

    If {@code other} is not a byte buffer then {@code 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.

 abstract public byte get()
    Returns the byte at the current position and increases the position by 1.
 public 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.

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

 abstract public byte get(int index)
    Returns the byte at the specified index and does not change the position.
 public 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.
 abstract public 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.

 abstract public 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.

 abstract public 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.

 abstract public 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.

 abstract public 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.

 abstract public 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.

 abstract public 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.

 abstract public 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.

 abstract public 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.

 abstract public 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.

 abstract public 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.

 abstract public 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.

 public final boolean hasArray() 
    Indicates whether this buffer is based on a byte array and provides read/write access.
 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.
 abstract public boolean isDirect()
    Indicates whether this buffer is direct.
 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

 public final ByteBuffer order(ByteOrder byteOrder) 
    Sets the byte order of this buffer.
 ByteBuffer orderImpl(ByteOrder byteOrder) 
 abstract byte[] protectedArray()
    Child class implements this method to realize {@code array()}.
 abstract int protectedArrayOffset()
    Child class implements this method to realize {@code arrayOffset()}.
 abstract boolean protectedHasArray()
    Child class implements this method to realize {@code hasArray()}.
 abstract public ByteBuffer put(byte b)
    Writes the given byte to the current position and increases the position by 1.
 public final 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.

    Calling this method has the same effect as {@code put(src, 0, src.length)}.

 public ByteBuffer put(ByteBuffer src) 
    Writes all the remaining bytes of the {@code src} byte buffer to this buffer's current position, and increases both buffers' position by the number of bytes copied.
 abstract public ByteBuffer put(int index,
    byte b)
    Write a byte to the specified index of this buffer without changing the position.
 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 increases the position by the number of bytes written.
 abstract public ByteBuffer putChar(char value)
    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.

 abstract public ByteBuffer putChar(int index,
    char value)
    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.

 abstract public ByteBuffer putDouble(double value)
    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.

 abstract public ByteBuffer putDouble(int index,
    double value)
    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.

 abstract public ByteBuffer putFloat(float value)
    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.

 abstract public ByteBuffer putFloat(int index,
    float value)
    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.

 abstract public ByteBuffer putInt(int value)
    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.

 abstract public ByteBuffer putInt(int index,
    int value)
    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.

 abstract public ByteBuffer putLong(long value)
    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.

 abstract public ByteBuffer putLong(int index,
    long value)
    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.

 abstract public ByteBuffer putShort(short value)
    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.

 abstract public ByteBuffer putShort(int index,
    short value)
    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.

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

    The sliced buffer's capacity will be this buffer's {@code 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() 
    Returns a string representing the state of this byte buffer.
 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 {@code wrap(array, 0, array.length)}.

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

    The new buffer's position will be {@code start}, limit will be {@code start + len}, capacity will be the length of the array.