java.nio
Class HeapByteBuffer

java.lang.Object
  extended byjava.nio.Buffer
      extended byjava.nio.ByteBuffer
          extended byjava.nio.HeapByteBuffer
All Implemented Interfaces:
Comparable
Direct Known Subclasses:
HeapByteBufferR

class HeapByteBuffer
extends ByteBuffer

A read/write HeapByteBuffer.


Field Summary
 
Fields inherited from class java.nio.ByteBuffer
bigEndian, hb, isReadOnly, nativeByteOrder, offset
 
Fields inherited from class java.nio.Buffer
address
 
Constructor Summary
(package private) HeapByteBuffer(byte[] buf, int off, int len)
           
protected HeapByteBuffer(byte[] buf, int mark, int pos, int lim, int cap, int off)
           
(package private) HeapByteBuffer(int cap, int lim)
           
 
Method Summary
(package private)  byte _get(int i)
           
(package private)  void _put(int i, byte b)
           
 CharBuffer asCharBuffer()
          Creates a view of this byte buffer as a char buffer.
 DoubleBuffer asDoubleBuffer()
          Creates a view of this byte buffer as a double buffer.
 FloatBuffer asFloatBuffer()
          Creates a view of this byte buffer as a float buffer.
 IntBuffer asIntBuffer()
          Creates a view of this byte buffer as an int buffer.
 LongBuffer asLongBuffer()
          Creates a view of this byte buffer as a long buffer.
 ByteBuffer asReadOnlyBuffer()
          Creates a new, read-only byte buffer that shares this buffer's content.
 ShortBuffer asShortBuffer()
          Creates a view of this byte buffer as a short buffer.
 ByteBuffer compact()
          Compacts this buffer  (optional operation).
 ByteBuffer duplicate()
          Creates a new byte buffer that shares this buffer's content.
 byte get()
          Relative get method.
 ByteBuffer get(byte[] dst, int offset, int length)
          Relative bulk get method.
 byte get(int i)
          Absolute get method.
 char getChar()
          Relative get method for reading a char value.
 char getChar(int i)
          Absolute get method for reading a char value.
 double getDouble()
          Relative get method for reading a double value.
 double getDouble(int i)
          Absolute get method for reading a double value.
 float getFloat()
          Relative get method for reading a float value.
 float getFloat(int i)
          Absolute get method for reading a float value.
 int getInt()
          Relative get method for reading an int value.
 int getInt(int i)
          Absolute get method for reading an int value.
 long getLong()
          Relative get method for reading a long value.
 long getLong(int i)
          Absolute get method for reading a long value.
 short getShort()
          Relative get method for reading a short value.
 short getShort(int i)
          Absolute get method for reading a short value.
 boolean isDirect()
          Tells whether or not this byte buffer is direct.
 boolean isReadOnly()
          Tells whether or not this buffer is read-only.
protected  int ix(int i)
           
 ByteBuffer put(byte x)
          Relative put method  (optional operation).
 ByteBuffer put(byte[] src, int offset, int length)
          Relative bulk put method  (optional operation).
 ByteBuffer put(ByteBuffer src)
          Relative bulk put method  (optional operation).
 ByteBuffer put(int i, byte x)
          Absolute put method  (optional operation).
 ByteBuffer putChar(char x)
          Relative put method for writing a char value  (optional operation).
 ByteBuffer putChar(int i, char x)
          Absolute put method for writing a char value  (optional operation).
 ByteBuffer putDouble(double x)
          Relative put method for writing a double value  (optional operation).
 ByteBuffer putDouble(int i, double x)
          Absolute put method for writing a double value  (optional operation).
 ByteBuffer putFloat(float x)
          Relative put method for writing a float value  (optional operation).
 ByteBuffer putFloat(int i, float x)
          Absolute put method for writing a float value  (optional operation).
 ByteBuffer putInt(int x)
          Relative put method for writing an int value  (optional operation).
 ByteBuffer putInt(int i, int x)
          Absolute put method for writing an int value  (optional operation).
 ByteBuffer putLong(int i, long x)
          Absolute put method for writing a long value  (optional operation).
 ByteBuffer putLong(long x)
          Relative put method for writing a long value  (optional operation).
 ByteBuffer putShort(int i, short x)
          Absolute put method for writing a short value  (optional operation).
 ByteBuffer putShort(short x)
          Relative put method for writing a short value  (optional operation).
 ByteBuffer slice()
          Creates a new byte buffer whose content is a shared subsequence of this buffer's content.
 
Methods inherited from class java.nio.ByteBuffer
allocate, allocateDirect, array, arrayOffset, compareTo, equals, get, hasArray, hashCode, order, order, put, toString, wrap, wrap
 
Methods inherited from class java.nio.Buffer
capacity, checkBounds, checkIndex, checkIndex, clear, flip, hasRemaining, limit, limit, mark, markValue, nextGetIndex, nextGetIndex, nextPutIndex, nextPutIndex, position, position, remaining, reset, rewind
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

HeapByteBuffer

HeapByteBuffer(int cap,
               int lim)

HeapByteBuffer

HeapByteBuffer(byte[] buf,
               int off,
               int len)

HeapByteBuffer

protected HeapByteBuffer(byte[] buf,
                         int mark,
                         int pos,
                         int lim,
                         int cap,
                         int off)
Method Detail

slice

public ByteBuffer slice()
Description copied from class: ByteBuffer
Creates a new byte buffer whose content is a shared subsequence of this buffer's content.

The content of the new buffer will start at this buffer's current position. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independent.

The new buffer's position will be zero, its capacity and its limit will be the number of bytes remaining in this buffer, and its mark will be undefined. The new buffer will be direct if, and only if, this buffer is direct, and it will be read-only if, and only if, this buffer is read-only.

Specified by:
slice in class ByteBuffer
Returns:
The new byte buffer

duplicate

public ByteBuffer duplicate()
Description copied from class: ByteBuffer
Creates a new byte buffer that shares this buffer's content.

The content of the new buffer will be that of this buffer. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independent.

The new buffer's capacity, limit, position, and mark values will be identical to those of this buffer. The new buffer will be direct if, and only if, this buffer is direct, and it will be read-only if, and only if, this buffer is read-only.

Specified by:
duplicate in class ByteBuffer
Returns:
The new byte buffer

asReadOnlyBuffer

public ByteBuffer asReadOnlyBuffer()
Description copied from class: ByteBuffer
Creates a new, read-only byte buffer that shares this buffer's content.

The content of the new buffer will be that of this buffer. Changes to this buffer's content will be visible in the new buffer; the new buffer itself, however, will be read-only and will not allow the shared content to be modified. The two buffers' position, limit, and mark values will be independent.

The new buffer's capacity, limit, position, and mark values will be identical to those of this buffer.

If this buffer is itself read-only then this method behaves in exactly the same way as the duplicate method.

Specified by:
asReadOnlyBuffer in class ByteBuffer
Returns:
The new, read-only byte buffer

ix

protected int ix(int i)

get

public byte get()
Description copied from class: ByteBuffer
Relative get method. Reads the byte at this buffer's current position, and then increments the position.

Specified by:
get in class ByteBuffer
Returns:
The byte at the buffer's current position

get

public byte get(int i)
Description copied from class: ByteBuffer
Absolute get method. Reads the byte at the given index.

Specified by:
get in class ByteBuffer
Parameters:
i - The index from which the byte will be read
Returns:
The byte at the given index

get

public ByteBuffer get(byte[] dst,
                      int offset,
                      int length)
Description copied from class: ByteBuffer
Relative bulk get method.

This method transfers bytes from this buffer into the given destination array. If there are fewer bytes remaining in the buffer than are required to satisfy the request, that is, if length > remaining(), then no bytes are transferred and a BufferUnderflowException is thrown.

Otherwise, this method copies length bytes from this buffer into the given array, starting at the current position of this buffer and at the given offset in the array. The position of this buffer is then incremented by length.

In other words, an invocation of this method of the form src.get(dst, off, len) has exactly the same effect as the loop

     for (int i = off; i < off + len; i++)
         dst[i] = src.get(); 
except that it first checks that there are sufficient bytes in this buffer and it is potentially much more efficient.

Overrides:
get in class ByteBuffer
Parameters:
dst - The array into which bytes are to be written
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
Returns:
This buffer

isDirect

public boolean isDirect()
Description copied from class: ByteBuffer
Tells whether or not this byte buffer is direct.

Specified by:
isDirect in class ByteBuffer
Returns:
true if, and only if, this buffer is direct

isReadOnly

public boolean isReadOnly()
Description copied from class: Buffer
Tells whether or not this buffer is read-only.

Specified by:
isReadOnly in class Buffer
Returns:
true if, and only if, this buffer is read-only

put

public ByteBuffer put(byte x)
Description copied from class: ByteBuffer
Relative put method  (optional operation).

Writes the given byte into this buffer at the current position, and then increments the position.

Specified by:
put in class ByteBuffer
Parameters:
x - The byte to be written
Returns:
This buffer

put

public ByteBuffer put(int i,
                      byte x)
Description copied from class: ByteBuffer
Absolute put method  (optional operation).

Writes the given byte into this buffer at the given index.

Specified by:
put in class ByteBuffer
Parameters:
i - The index at which the byte will be written
x - The byte value to be written
Returns:
This buffer

put

public ByteBuffer put(byte[] src,
                      int offset,
                      int length)
Description copied from class: ByteBuffer
Relative bulk put method  (optional operation).

This method transfers bytes into this buffer from the given source array. If there are more bytes to be copied from the array than remain in this buffer, that is, if length > remaining(), then no bytes are transferred and a BufferOverflowException is thrown.

Otherwise, this method copies length bytes from the given array into this buffer, starting at the given offset in the array and at the current position of this buffer. The position of this buffer is then incremented by length.

In other words, an invocation of this method of the form dst.put(src, off, len) has exactly the same effect as the loop

     for (int i = off; i < off + len; i++)
         dst.put(a[i]); 
except that it first checks that there is sufficient space in this buffer and it is potentially much more efficient.

Overrides:
put in class ByteBuffer
Parameters:
src - The array from which bytes are to be read
offset - The offset within the array of the first byte to be read; must be non-negative and no larger than array.length
length - The number of bytes to be read from the given array; must be non-negative and no larger than array.length - offset
Returns:
This buffer

put

public ByteBuffer put(ByteBuffer src)
Description copied from class: ByteBuffer
Relative bulk put method  (optional operation).

This method transfers the bytes remaining in the given source buffer into this buffer. If there are more bytes remaining in the source buffer than in this buffer, that is, if src.remaining() > remaining(), then no bytes are transferred and a BufferOverflowException is thrown.

Otherwise, this method copies n = src.remaining() bytes from the given buffer into this buffer, starting at each buffer's current position. The positions of both buffers are then incremented by n.

In other words, an invocation of this method of the form dst.put(src) has exactly the same effect as the loop

     while (src.hasRemaining())
         dst.put(src.get()); 
except that it first checks that there is sufficient space in this buffer and it is potentially much more efficient.

Overrides:
put in class ByteBuffer
Parameters:
src - The source buffer from which bytes are to be read; must not be this buffer
Returns:
This buffer

compact

public ByteBuffer compact()
Description copied from class: ByteBuffer
Compacts this buffer  (optional operation).

The bytes between the buffer's current position and its limit, if any, are copied to the beginning of the buffer. That is, the byte at index p = position() is copied to index zero, the byte at index p + 1 is copied to index one, and so forth until the byte at index limit() - 1 is copied to index n = limit() - 1 - p. The buffer's position is then set to n+1 and its limit is set to its capacity. The mark, if defined, is discarded.

The buffer's position is set to the number of bytes copied, rather than to zero, so that an invocation of this method can be followed immediately by an invocation of another relative put method.

Invoke this method after writing data from a buffer in case the write was incomplete. The following loop, for example, copies bytes from one channel to another via the buffer buf:

 buf.clear();          // Prepare buffer for use
 for (;;) {
     if (in.read(buf) < 0 && !buf.hasRemaining())
         break;        // No more bytes to transfer
     buf.flip();
     out.write(buf);
     buf.compact();    // In case of partial write
 }

Specified by:
compact in class ByteBuffer
Returns:
This buffer

_get

byte _get(int i)
Specified by:
_get in class ByteBuffer

_put

void _put(int i,
          byte b)
Specified by:
_put in class ByteBuffer

getChar

public char getChar()
Description copied from class: ByteBuffer
Relative get method for reading a char value.

Reads the next two bytes at this buffer's current position, composing them into a char value according to the current byte order, and then increments the position by two.

Specified by:
getChar in class ByteBuffer
Returns:
The char value at the buffer's current position

getChar

public char getChar(int i)
Description copied from class: ByteBuffer
Absolute get method for reading a char value.

Reads two bytes at the given index, composing them into a char value according to the current byte order.

Specified by:
getChar in class ByteBuffer
Parameters:
i - The index from which the bytes will be read
Returns:
The char value at the given index

putChar

public ByteBuffer putChar(char x)
Description copied from class: ByteBuffer
Relative put method for writing a char value  (optional operation).

Writes two bytes containing the given char value, in the current byte order, into this buffer at the current position, and then increments the position by two.

Specified by:
putChar in class ByteBuffer
Parameters:
x - The char value to be written
Returns:
This buffer

putChar

public ByteBuffer putChar(int i,
                          char x)
Description copied from class: ByteBuffer
Absolute put method for writing a char value  (optional operation).

Writes two bytes containing the given char value, in the current byte order, into this buffer at the given index.

Specified by:
putChar in class ByteBuffer
Parameters:
i - The index at which the bytes will be written
x - The char value to be written
Returns:
This buffer

asCharBuffer

public CharBuffer asCharBuffer()
Description copied from class: ByteBuffer
Creates a view of this byte buffer as a char buffer.

The content of the new buffer will start at this buffer's current position. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independent.

The new buffer's position will be zero, its capacity and its limit will be the number of bytes remaining in this buffer divided by two, and its mark will be undefined. The new buffer will be direct if, and only if, this buffer is direct, and it will be read-only if, and only if, this buffer is read-only.

Specified by:
asCharBuffer in class ByteBuffer
Returns:
A new char buffer

getShort

public short getShort()
Description copied from class: ByteBuffer
Relative get method for reading a short value.

Reads the next two bytes at this buffer's current position, composing them into a short value according to the current byte order, and then increments the position by two.

Specified by:
getShort in class ByteBuffer
Returns:
The short value at the buffer's current position

getShort

public short getShort(int i)
Description copied from class: ByteBuffer
Absolute get method for reading a short value.

Reads two bytes at the given index, composing them into a short value according to the current byte order.

Specified by:
getShort in class ByteBuffer
Parameters:
i - The index from which the bytes will be read
Returns:
The short value at the given index

putShort

public ByteBuffer putShort(short x)
Description copied from class: ByteBuffer
Relative put method for writing a short value  (optional operation).

Writes two bytes containing the given short value, in the current byte order, into this buffer at the current position, and then increments the position by two.

Specified by:
putShort in class ByteBuffer
Parameters:
x - The short value to be written
Returns:
This buffer

putShort

public ByteBuffer putShort(int i,
                           short x)
Description copied from class: ByteBuffer
Absolute put method for writing a short value  (optional operation).

Writes two bytes containing the given short value, in the current byte order, into this buffer at the given index.

Specified by:
putShort in class ByteBuffer
Parameters:
i - The index at which the bytes will be written
x - The short value to be written
Returns:
This buffer

asShortBuffer

public ShortBuffer asShortBuffer()
Description copied from class: ByteBuffer
Creates a view of this byte buffer as a short buffer.

The content of the new buffer will start at this buffer's current position. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independent.

The new buffer's position will be zero, its capacity and its limit will be the number of bytes remaining in this buffer divided by two, and its mark will be undefined. The new buffer will be direct if, and only if, this buffer is direct, and it will be read-only if, and only if, this buffer is read-only.

Specified by:
asShortBuffer in class ByteBuffer
Returns:
A new short buffer

getInt

public int getInt()
Description copied from class: ByteBuffer
Relative get method for reading an int value.

Reads the next four bytes at this buffer's current position, composing them into an int value according to the current byte order, and then increments the position by four.

Specified by:
getInt in class ByteBuffer
Returns:
The int value at the buffer's current position

getInt

public int getInt(int i)
Description copied from class: ByteBuffer
Absolute get method for reading an int value.

Reads four bytes at the given index, composing them into a int value according to the current byte order.

Specified by:
getInt in class ByteBuffer
Parameters:
i - The index from which the bytes will be read
Returns:
The int value at the given index

putInt

public ByteBuffer putInt(int x)
Description copied from class: ByteBuffer
Relative put method for writing an int value  (optional operation).

Writes four bytes containing the given int value, in the current byte order, into this buffer at the current position, and then increments the position by four.

Specified by:
putInt in class ByteBuffer
Parameters:
x - The int value to be written
Returns:
This buffer

putInt

public ByteBuffer putInt(int i,
                         int x)
Description copied from class: ByteBuffer
Absolute put method for writing an int value  (optional operation).

Writes four bytes containing the given int value, in the current byte order, into this buffer at the given index.

Specified by:
putInt in class ByteBuffer
Parameters:
i - The index at which the bytes will be written
x - The int value to be written
Returns:
This buffer

asIntBuffer

public IntBuffer asIntBuffer()
Description copied from class: ByteBuffer
Creates a view of this byte buffer as an int buffer.

The content of the new buffer will start at this buffer's current position. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independent.

The new buffer's position will be zero, its capacity and its limit will be the number of bytes remaining in this buffer divided by four, and its mark will be undefined. The new buffer will be direct if, and only if, this buffer is direct, and it will be read-only if, and only if, this buffer is read-only.

Specified by:
asIntBuffer in class ByteBuffer
Returns:
A new int buffer

getLong

public long getLong()
Description copied from class: ByteBuffer
Relative get method for reading a long value.

Reads the next eight bytes at this buffer's current position, composing them into a long value according to the current byte order, and then increments the position by eight.

Specified by:
getLong in class ByteBuffer
Returns:
The long value at the buffer's current position

getLong

public long getLong(int i)
Description copied from class: ByteBuffer
Absolute get method for reading a long value.

Reads eight bytes at the given index, composing them into a long value according to the current byte order.

Specified by:
getLong in class ByteBuffer
Parameters:
i - The index from which the bytes will be read
Returns:
The long value at the given index

putLong

public ByteBuffer putLong(long x)
Description copied from class: ByteBuffer
Relative put method for writing a long value  (optional operation).

Writes eight bytes containing the given long value, in the current byte order, into this buffer at the current position, and then increments the position by eight.

Specified by:
putLong in class ByteBuffer
Parameters:
x - The long value to be written
Returns:
This buffer

putLong

public ByteBuffer putLong(int i,
                          long x)
Description copied from class: ByteBuffer
Absolute put method for writing a long value  (optional operation).

Writes eight bytes containing the given long value, in the current byte order, into this buffer at the given index.

Specified by:
putLong in class ByteBuffer
Parameters:
i - The index at which the bytes will be written
x - The long value to be written
Returns:
This buffer

asLongBuffer

public LongBuffer asLongBuffer()
Description copied from class: ByteBuffer
Creates a view of this byte buffer as a long buffer.

The content of the new buffer will start at this buffer's current position. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independent.

The new buffer's position will be zero, its capacity and its limit will be the number of bytes remaining in this buffer divided by eight, and its mark will be undefined. The new buffer will be direct if, and only if, this buffer is direct, and it will be read-only if, and only if, this buffer is read-only.

Specified by:
asLongBuffer in class ByteBuffer
Returns:
A new long buffer

getFloat

public float getFloat()
Description copied from class: ByteBuffer
Relative get method for reading a float value.

Reads the next four bytes at this buffer's current position, composing them into a float value according to the current byte order, and then increments the position by four.

Specified by:
getFloat in class ByteBuffer
Returns:
The float value at the buffer's current position

getFloat

public float getFloat(int i)
Description copied from class: ByteBuffer
Absolute get method for reading a float value.

Reads four bytes at the given index, composing them into a float value according to the current byte order.

Specified by:
getFloat in class ByteBuffer
Parameters:
i - The index from which the bytes will be read
Returns:
The float value at the given index

putFloat

public ByteBuffer putFloat(float x)
Description copied from class: ByteBuffer
Relative put method for writing a float value  (optional operation).

Writes four bytes containing the given float value, in the current byte order, into this buffer at the current position, and then increments the position by four.

Specified by:
putFloat in class ByteBuffer
Parameters:
x - The float value to be written
Returns:
This buffer

putFloat

public ByteBuffer putFloat(int i,
                           float x)
Description copied from class: ByteBuffer
Absolute put method for writing a float value  (optional operation).

Writes four bytes containing the given float value, in the current byte order, into this buffer at the given index.

Specified by:
putFloat in class ByteBuffer
Parameters:
i - The index at which the bytes will be written
x - The float value to be written
Returns:
This buffer

asFloatBuffer

public FloatBuffer asFloatBuffer()
Description copied from class: ByteBuffer
Creates a view of this byte buffer as a float buffer.

The content of the new buffer will start at this buffer's current position. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independent.

The new buffer's position will be zero, its capacity and its limit will be the number of bytes remaining in this buffer divided by four, and its mark will be undefined. The new buffer will be direct if, and only if, this buffer is direct, and it will be read-only if, and only if, this buffer is read-only.

Specified by:
asFloatBuffer in class ByteBuffer
Returns:
A new float buffer

getDouble

public double getDouble()
Description copied from class: ByteBuffer
Relative get method for reading a double value.

Reads the next eight bytes at this buffer's current position, composing them into a double value according to the current byte order, and then increments the position by eight.

Specified by:
getDouble in class ByteBuffer
Returns:
The double value at the buffer's current position

getDouble

public double getDouble(int i)
Description copied from class: ByteBuffer
Absolute get method for reading a double value.

Reads eight bytes at the given index, composing them into a double value according to the current byte order.

Specified by:
getDouble in class ByteBuffer
Parameters:
i - The index from which the bytes will be read
Returns:
The double value at the given index

putDouble

public ByteBuffer putDouble(double x)
Description copied from class: ByteBuffer
Relative put method for writing a double value  (optional operation).

Writes eight bytes containing the given double value, in the current byte order, into this buffer at the current position, and then increments the position by eight.

Specified by:
putDouble in class ByteBuffer
Parameters:
x - The double value to be written
Returns:
This buffer

putDouble

public ByteBuffer putDouble(int i,
                            double x)
Description copied from class: ByteBuffer
Absolute put method for writing a double value  (optional operation).

Writes eight bytes containing the given double value, in the current byte order, into this buffer at the given index.

Specified by:
putDouble in class ByteBuffer
Parameters:
i - The index at which the bytes will be written
x - The double value to be written
Returns:
This buffer

asDoubleBuffer

public DoubleBuffer asDoubleBuffer()
Description copied from class: ByteBuffer
Creates a view of this byte buffer as a double buffer.

The content of the new buffer will start at this buffer's current position. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independent.

The new buffer's position will be zero, its capacity and its limit will be the number of bytes remaining in this buffer divided by eight, and its mark will be undefined. The new buffer will be direct if, and only if, this buffer is direct, and it will be read-only if, and only if, this buffer is read-only.

Specified by:
asDoubleBuffer in class ByteBuffer
Returns:
A new double buffer