FileDocCategorySizeDatePackage
DirectByteBuffer.javaAPI DocAndroid 1.5 API9565Wed May 06 22:41:04 BST 2009java.nio

DirectByteBuffer

public abstract class DirectByteBuffer extends BaseByteBuffer implements org.apache.harmony.nio.internal.DirectBuffer
DirectByteBuffer, ReadWriteDirectByteBuffer and ReadOnlyDirectByteBuffer compose the implementation of platform memory based byte buffers.

DirectByteBuffer 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 SafeAddress
safeAddress
protected final int
offset
Constructors Summary
DirectByteBuffer(int capacity)

        this(new SafeAddress(PlatformAddressFactory.alloc(capacity, (byte)0)), capacity, 0);
        safeAddress.address.autoFree();
    
DirectByteBuffer(SafeAddress address, int capacity, int offset)

        super(capacity);

        // BEGIN android-added
        PlatformAddress baseAddress = address.address;
        long baseSize = baseAddress.getSize();

        if ((baseSize >= 0) && ((offset + capacity) > baseSize)) {
            throw new IllegalArgumentException("slice out of range");
        }
        // END android-added
        
        this.safeAddress = address;
        this.offset = offset;
    
Methods Summary
public final voidaddressValidityCheck()

        if (!isAddressValid()) {
            // nio.08=Cannot use the direct byte buffer after it has been explicitly freed.
            throw new IllegalStateException(
                    Messages.getString("nio.08"));  //$NON-NLS-1$
        }
    
public final voidfree()
Explicitly free the memory used by this direct byte buffer. If the memory has already been freed then this is a no-op. Once the memory has been freed then operations requiring access to the memory will throw an IllegalStateException.

Note this is is possible that the memory is freed by code that reaches into the address and explicitly frees it 'beneith' us -- this is bad form.

        if (isAddressValid()) {
            markAddressInvalid();
            safeAddress.address.free();
        }
    
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();
        }
        getBaseAddress().getByteArray(offset+position, dest, off, len);
        position += len;
        return this;
    
public final byteget()

        if (position == limit) {
            throw new BufferUnderflowException();
        }
        return getBaseAddress().getByte(offset + position++);
    
public final byteget(int index)

        if (index < 0 || index >= limit) {
            throw new IndexOutOfBoundsException();
        }
        return getBaseAddress().getByte(offset + index);
    
public final org.apache.harmony.luni.platform.PlatformAddressgetBaseAddress()

        addressValidityCheck();
        return safeAddress.address;
    
public final intgetByteCapacity()

        return capacity;
    
public final doublegetDouble()

        int newPosition = position + 8;
        if (newPosition > limit) {
            throw new BufferUnderflowException();
        }
        double result = getBaseAddress().getDouble(offset + position, order);
        position = newPosition;
        return result;
    
public final doublegetDouble(int index)

        if (index < 0 || (long)index + 8 > limit) {
            throw new IndexOutOfBoundsException();
        }
        return getBaseAddress().getDouble(offset + index, order);
    
public final org.apache.harmony.luni.platform.PlatformAddressgetEffectiveAddress()
Returns the platform address of the start of this buffer instance. You must not attempt to free the returned address!! It may not be an address that was explicitly malloc'ed (i.e. if this buffer is the result of a split); and it may be memory shared by multiple buffers.

If you can guarantee that you want to free the underlying memory call the #free() method on this instance -- generally applications will rely on the garbage collector to autofree this memory.

return
the effective address of the start of the buffer.
throws
IllegalStateException if this buffer address is known to have been freed previously.

        return getBaseAddress().offsetBytes(offset);
    
public final floatgetFloat()

        int newPosition = position + 4;
        if (newPosition > limit) {
            throw new BufferUnderflowException();
        }
        float result = getBaseAddress().getFloat(offset + position, order);
        position = newPosition;
        return result;
    
public final floatgetFloat(int index)

        if (index < 0 || (long)index + 4 > limit) {
            throw new IndexOutOfBoundsException();
        }
        return getBaseAddress().getFloat(offset + index, order);
    
public final intgetInt()

        int newPosition = position + 4;
        if (newPosition > limit) {
            throw new BufferUnderflowException();
        }
        int result = getBaseAddress().getInt(offset + position, order);
        position = newPosition;
        return result;
    
public final intgetInt(int index)

        if (index < 0 || (long)index + 4 > limit) {
            throw new IndexOutOfBoundsException();
        }
        return getBaseAddress().getInt(offset + index, order);
    
public final longgetLong()

        int newPosition = position + 8;
        if (newPosition > limit) {
            throw new BufferUnderflowException();
        }
        long result = getBaseAddress().getLong(offset + position, order);
        position = newPosition;
        return result;
    
public final longgetLong(int index)

        if (index < 0 || (long)index + 8 > limit) {
            throw new IndexOutOfBoundsException();
        }
        return getBaseAddress().getLong(offset + index, order);
    
public final shortgetShort()

        int newPosition = position + 2;
        if (newPosition > limit) {
            throw new BufferUnderflowException();
        }
        short result = getBaseAddress().getShort(offset + position, order);
        position = newPosition;
        return result;
    
public final shortgetShort(int index)

        if (index < 0 || (long)index + 2 > limit) {
            throw new IndexOutOfBoundsException();
        }
        return getBaseAddress().getShort(offset + index, order);
    
public final booleanisAddressValid()

        return safeAddress.isValid;
    
public final booleanisDirect()

        return true;
    
private voidmarkAddressInvalid()

        safeAddress.isValid = false;
    
protected final byte[]protectedArray()

        throw new UnsupportedOperationException();
    
protected final intprotectedArrayOffset()

        throw new UnsupportedOperationException();
    
protected final booleanprotectedHasArray()

        return false;