FileDocCategorySizeDatePackage
HeapByteBuffer.javaAPI DocAndroid 1.5 API7678Wed May 06 22:41:04 BST 2009java.nio

HeapByteBuffer

public abstract class HeapByteBuffer extends BaseByteBuffer
HeapByteBuffer, ReadWriteHeapByteBuffer and ReadOnlyHeapByteBuffer compose the implementation of array based byte buffers.

HeapByteBuffer implements all the shared readonly methods and is extended by the other two classes.

All methods are marked final for runtime performance.

Fields Summary
protected final byte[]
backingArray
protected final int
offset
Constructors Summary
HeapByteBuffer(byte[] backingArray)

        this(backingArray, backingArray.length, 0);
    
HeapByteBuffer(int capacity)

        this(new byte[capacity], capacity, 0);
    
HeapByteBuffer(byte[] backingArray, int capacity, int offset)

        super(capacity);
        this.backingArray = backingArray;
        this.offset = offset;

        if (offset + capacity > backingArray.length) {
            throw new IndexOutOfBoundsException();
        }
    
Methods Summary
public final java.nio.ByteBufferget(byte[] dest, int off, int len)

        int length = dest.length;
        if (off < 0 || len < 0 || (long)off + (long)len > length) {
            throw new IndexOutOfBoundsException();
        }
        if (len > remaining()) {
            throw new BufferUnderflowException();
        }
        System.arraycopy(backingArray, offset + position, dest, off, len);
        position += len;
        return this;
    
public final byteget()

        if (position == limit) {
            throw new BufferUnderflowException();
        }
        return backingArray[offset + position++];
    
public final byteget(int index)

        if (index < 0 || index >= limit) {
            throw new IndexOutOfBoundsException();
        }
        return backingArray[offset + index];
    
public final doublegetDouble()

        return Double.longBitsToDouble(getLong());
    
public final doublegetDouble(int index)

        return Double.longBitsToDouble(getLong(index));
    
public final floatgetFloat(int index)

        return Float.intBitsToFloat(getInt(index));
    
public final floatgetFloat()

        return Float.intBitsToFloat(getInt());
    
public final intgetInt()

        int newPosition = position + 4;
        if (newPosition > limit) {
            throw new BufferUnderflowException();
        }
        int result = loadInt(position);
        position = newPosition;
        return result;
    
public final intgetInt(int index)

        if (index < 0 || index + 4 > limit) {
            throw new IndexOutOfBoundsException();
        }
        return loadInt(index);
    
public final longgetLong()

        int newPosition = position + 8;
        if (newPosition > limit) {
            throw new BufferUnderflowException();
        }
        long result = loadLong(position);
        position = newPosition;
        return result;
    
public final longgetLong(int index)

        if (index < 0 || index + 8 > limit) {
            throw new IndexOutOfBoundsException();
        }
        return loadLong(index);
    
public final shortgetShort()

        int newPosition = position + 2;
        if (newPosition > limit) {
            throw new BufferUnderflowException();
        }
        short result = loadShort(position);
        position = newPosition;
        return result;
    
public final shortgetShort(int index)

        if (index < 0 || index + 2 > limit) {
            throw new IndexOutOfBoundsException();
        }
        return loadShort(index);
    
public final booleanisDirect()

        return false;
    
protected final intloadInt(int index)

        int baseOffset = offset + index;
        int bytes = 0;
        if(order == Endianness.BIG_ENDIAN){
            for (int i = 0; i < 4; i++) {
                bytes = bytes << 8;
                bytes = bytes | (backingArray[baseOffset + i] & 0xFF);
            }    
        }else{
            for (int i = 3; i >= 0; i--) {
                bytes = bytes << 8;
                bytes = bytes | (backingArray[baseOffset + i] & 0xFF);
            }
        }
        return bytes;
    
protected final longloadLong(int index)

        int baseOffset = offset + index;
        long bytes = 0;
        if(order == Endianness.BIG_ENDIAN){
            for (int i = 0; i < 8; i++) {
                bytes = bytes << 8;
                bytes = bytes | (backingArray[baseOffset + i] & 0xFF);
            }    
        }else{
            for (int i = 7; i >= 0; i--) {
                bytes = bytes << 8;
                bytes = bytes | (backingArray[baseOffset + i] & 0xFF);
            }
        }
        return bytes;
    
protected final shortloadShort(int index)

        int baseOffset = offset + index;
        short bytes  = 0;
        if(order == Endianness.BIG_ENDIAN){
            bytes = (short) (backingArray[baseOffset] << 8);
            bytes |= (backingArray[baseOffset + 1] & 0xFF);   
        }else{
            bytes = (short) (backingArray[baseOffset+1] << 8);
            bytes |= (backingArray[baseOffset] & 0xFF);
        }
        return bytes;
    
protected final voidstore(int index, int value)

        int baseOffset = offset + index;
        if (order == Endianness.BIG_ENDIAN) {
            for (int i = 3; i >= 0; i--) {
                backingArray[baseOffset + i] = (byte) (value & 0xFF);
                value = value >> 8;
            }
        } else {
            for (int i = 0; i <= 3; i++) {
                backingArray[baseOffset + i] = (byte) (value & 0xFF);
                value = value >> 8;
            }
        }
    
protected final voidstore(int index, long value)

        int baseOffset = offset + index;
        if (order == Endianness.BIG_ENDIAN) {
            for (int i = 7; i >= 0; i--) {
                backingArray[baseOffset + i] = (byte) (value & 0xFF);
                value = value >> 8;
            }
        } else {
            for (int i = 0; i <= 7; i++) {
                backingArray[baseOffset + i] = (byte) (value & 0xFF);
                value = value >> 8;
            }
        }
    
protected final voidstore(int index, short value)

        int baseOffset = offset + index;
        if (order == Endianness.BIG_ENDIAN) {
            backingArray[baseOffset] = (byte) ((value >> 8) & 0xFF);
            backingArray[baseOffset + 1] = (byte) (value & 0xFF);
        } else {
            backingArray[baseOffset+1] = (byte) ((value >> 8) & 0xFF);
            backingArray[baseOffset] = (byte) (value & 0xFF);
        }