FileDocCategorySizeDatePackage
FloatBuffer.javaAPI DocAndroid 1.5 API20733Wed May 06 22:41:04 BST 2009java.nio

FloatBuffer

public abstract class FloatBuffer extends Buffer implements Comparable
A buffer of floats.

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

  • {@link #allocate(int) Allocate} a new float array and create a buffer based on it;
  • {@link #wrap(float[]) Wrap} an existing float array to create a new buffer;
  • Use {@link java.nio.ByteBuffer#asFloatBuffer() ByteBuffer.asFloatBuffer} to create a float buffer based on a byte buffer.
since
Android 1.0

Fields Summary
Constructors Summary
FloatBuffer(int capacity)
Constructs a {@code FloatBuffer} with given capacity.

param
capacity The capacity of the buffer

        super(capacity);
        // BEGIN android-added
        _elementSizeShift = 2;
        // END android-added
    
Methods Summary
java.lang.Object_array()

        if (hasArray()) {
            return array();
        }
        return null;
    
int_arrayOffset()

        if (hasArray()) {
            return arrayOffset();
        }
        return 0;
    
public static java.nio.FloatBufferallocate(int capacity)
Creates a float buffer based on a newly allocated float array.

param
capacity the capacity of the new buffer.
return
the created float buffer.
throws
IllegalArgumentException if {@code capacity} is less than zero.
since
Android 1.0

        if (capacity < 0) {
            throw new IllegalArgumentException();
        }
        return BufferFactory.newFloatBuffer(capacity);
    
public final float[]array()
Returns the float array which this buffer is based on, if there is one.

return
the float array which this buffer is based on.
exception
ReadOnlyBufferException if this buffer is based on an array, but it is read-only.
exception
UnsupportedOperationException if this buffer is not based on an array.
since
Android 1.0

        return protectedArray();
    
public final intarrayOffset()
Returns the offset of the float array which this buffer is based on, if there is one.

The offset is the index of the array and corresponds to the zero position of the buffer.

return
the offset of the float array which this buffer is based on.
exception
ReadOnlyBufferException if this buffer is based on an array, but it is read-only.
exception
UnsupportedOperationException if this buffer is not based on an array.
since
Android 1.0

        return protectedArrayOffset();
    
public abstract java.nio.FloatBufferasReadOnlyBuffer()
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.

return
a read-only version of this buffer.
since
Android 1.0

public abstract java.nio.FloatBuffercompact()
Compacts this float buffer.

The remaining floats 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.

return
this buffer.
exception
ReadOnlyBufferException if no changes may be made to the contents of this buffer.
since
Android 1.0

public intcompareTo(java.nio.FloatBuffer otherBuffer)
Compare the remaining floats of this buffer to another float buffer's remaining floats.

param
otherBuffer another float buffer.
return
a negative value if this is less than {@code otherBuffer}; 0 if this equals to {@code otherBuffer}; a positive value if this is greater than {@code otherBuffer}.
exception
ClassCastException if {@code otherBuffer} is not a float buffer.
since
Android 1.0

        int compareRemaining = (remaining() < otherBuffer.remaining()) ? remaining()
                : otherBuffer.remaining();
        int thisPos = position;
        int otherPos = otherBuffer.position;
        // BEGIN android-changed
        float thisFloat, otherFloat;
        while (compareRemaining > 0) {
            thisFloat = get(thisPos);
            otherFloat = otherBuffer.get(otherPos);
            // checks for float and NaN inequality
            if ((thisFloat != otherFloat)
                    && ((thisFloat == thisFloat) || (otherFloat == otherFloat))) {
                return thisFloat < otherFloat ? -1 : 1;
            }
            thisPos++;
            otherPos++;
            compareRemaining--;
        }
        // END android-changed
        return remaining() - otherBuffer.remaining();
    
public abstract java.nio.FloatBufferduplicate()
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. The duplicated buffer's read-only property and byte order are same as this buffer 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.

return
a duplicated buffer that shares its content with this buffer.
since
Android 1.0

public booleanequals(java.lang.Object other)
Checks whether this float buffer is equal to another object.

If {@code other} is not a float buffer then {@code false} is returned. Two float buffers are equal if and only if their remaining floats are exactly the same. Position, limit, capacity and mark are not considered.

param
other the object to compare with this float buffer.
return
{@code true} if this float buffer is equal to {@code other}, {@code false} otherwise.
since
Android 1.0

        if (!(other instanceof FloatBuffer)) {
            return false;
        }
        FloatBuffer otherBuffer = (FloatBuffer) other;

        if (remaining() != otherBuffer.remaining()) {
            return false;
        }

        int myPosition = position;
        int otherPosition = otherBuffer.position;
        boolean equalSoFar = true;
        while (equalSoFar && (myPosition < limit)) {
            equalSoFar = get(myPosition++) == otherBuffer.get(otherPosition++);
        }

        return equalSoFar;
    
public abstract floatget()
Returns the float at the current position and increases the position by 1.

return
the float at the current position.
exception
BufferUnderflowException if the position is equal or greater than limit.
since
Android 1.0

public java.nio.FloatBufferget(float[] dest)
Reads floats from the current position into the specified float array and increases the position by the number of floats read.

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

param
dest the destination float array.
return
this buffer.
exception
BufferUnderflowException if {@code dest.length} is greater than {@code remaining()}.
since
Android 1.0

        return get(dest, 0, dest.length);
    
public java.nio.FloatBufferget(float[] dest, int off, int len)
Reads floats from the current position into the specified float array, starting from the specified offset, and increases the position by the number of floats read.

param
dest the target float array.
param
off the offset of the float array, must not be negative and no greater than {@code dest.length}.
param
len the number of floats to read, must be no less than zero and no greater than {@code dest.length - off}.
return
this buffer.
exception
IndexOutOfBoundsException if either {@code off} or {@code len} is invalid.
exception
BufferUnderflowException if {@code len} is greater than {@code remaining()}.
since
Android 1.0

        int length = dest.length;
        if (off < 0 || len < 0 || (long)off + (long)len > length) {
            throw new IndexOutOfBoundsException();
        }
        
        if (len > remaining()) {
            throw new BufferUnderflowException();
        }
        for (int i = off; i < off + len; i++) {
            dest[i] = get();
        }
        return this;
    
public abstract floatget(int index)
Returns a float at the specified index; the position is not changed.

param
index the index, must not be negative and less than limit.
return
a float at the specified index.
exception
IndexOutOfBoundsException if index is invalid.
since
Android 1.0

public final booleanhasArray()
Indicates whether this buffer is based on a float array and is read/write.

return
{@code true} if this buffer is based on a float array and provides read/write access, {@code false} otherwise.
since
Android 1.0

        return protectedHasArray();
    
public inthashCode()
Calculates this buffer's hash code from the remaining chars. The position, limit, capacity and mark don't affect the hash code.

return
the hash code calculated from the remaining floats.
since
Android 1.0

        int myPosition = position;
        int hash = 0;
        while (myPosition < limit) {
            hash = hash + Float.floatToIntBits(get(myPosition++));
        }
        return hash;
    
public abstract booleanisDirect()
Indicates whether this buffer is direct. A direct buffer will try its best to take advantage of native memory APIs and it may not stay in the Java heap, so it is not affected by garbage collection.

A float buffer is direct if it is based on a byte buffer and the byte buffer is direct.

return
{@code true} if this buffer is direct, {@code false} otherwise.
since
Android 1.0

public abstract java.nio.ByteOrderorder()
Returns the byte order used by this buffer when converting floats from/to bytes.

If this buffer is not based on a byte buffer, then always return the platform's native byte order.

return
the byte order used by this buffer when converting floats from/to bytes.
since
Android 1.0

abstract float[]protectedArray()
Child class implements this method to realize {@code array()}.

return
see {@code array()}

abstract intprotectedArrayOffset()
Child class implements this method to realize {@code arrayOffset()}.

return
see {@code arrayOffset()}

abstract booleanprotectedHasArray()
Child class implements this method to realize {@code hasArray()}.

return
see {@code hasArray()}

public abstract java.nio.FloatBufferput(float f)
Writes the given float to the current position and increases the position by 1.

param
f the float to write.
return
this buffer.
exception
BufferOverflowException if position is equal or greater than limit.
exception
ReadOnlyBufferException if no changes may be made to the contents of this buffer.
since
Android 1.0

public final java.nio.FloatBufferput(float[] src)
Writes floats from the given float array to the current position and increases the position by the number of floats written.

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

param
src the source float array.
return
this buffer.
exception
BufferOverflowException if {@code remaining()} is less than {@code src.length}.
exception
ReadOnlyBufferException if no changes may be made to the contents of this buffer.
since
Android 1.0

        return put(src, 0, src.length);
    
public java.nio.FloatBufferput(float[] src, int off, int len)
Writes floats from the given float array, starting from the specified offset, to the current position and increases the position by the number of floats written.

param
src the source float array.
param
off the offset of float array, must not be negative and not greater than {@code src.length}.
param
len the number of floats to write, must be no less than zero and no greater than {@code src.length - off}.
return
this buffer.
exception
BufferOverflowException if {@code remaining()} is less than {@code len}.
exception
IndexOutOfBoundsException if either {@code off} or {@code len} is invalid.
exception
ReadOnlyBufferException if no changes may be made to the contents of this buffer.
since
Android 1.0

        int length = src.length;
        if (off < 0 || len < 0 || (long)off + (long)len > length) {
            throw new IndexOutOfBoundsException();
        }
        
        if (len > remaining()) {
            throw new BufferOverflowException();
        }
        for (int i = off; i < off + len; i++) {
            put(src[i]);
        }
        return this;
    
public java.nio.FloatBufferput(java.nio.FloatBuffer src)
Writes all the remaining floats of the {@code src} float buffer to this buffer's current position, and increases both buffers' position by the number of floats copied.

param
src the source float buffer.
return
this buffer.
exception
BufferOverflowException if {@code src.remaining()} is greater than this buffer's {@code remaining()}.
exception
IllegalArgumentException if {@code src} is this buffer.
exception
ReadOnlyBufferException if no changes may be made to the contents of this buffer.
since
Android 1.0

        if (src == this) {
            throw new IllegalArgumentException();
        }
        if (src.remaining() > remaining()) {
            throw new BufferOverflowException();
        }
        float[] contents = new float[src.remaining()];
        src.get(contents);
        put(contents);
        return this;
    
public abstract java.nio.FloatBufferput(int index, float f)
Writes a float to the specified index of this buffer; the position is not changed.

param
index the index, must not be negative and less than the limit.
param
f the float to write.
return
this buffer.
exception
IndexOutOfBoundsException if index is invalid.
exception
ReadOnlyBufferException if no changes may be made to the contents of this buffer.
since
Android 1.0

public abstract java.nio.FloatBufferslice()
Returns a sliced buffer that shares its content with this buffer.

The sliced buffer's capacity will be this buffer's {@code remaining()}, and its 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 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.

return
a sliced buffer that shares its content with this buffer.
since
Android 1.0

public java.lang.StringtoString()
Returns a string representing the state of this float buffer.

return
a string representing the state of this float buffer.
since
Android 1.0

        StringBuffer buf = new StringBuffer();
        buf.append(getClass().getName());
        buf.append(", status: capacity="); //$NON-NLS-1$
        buf.append(capacity());
        buf.append(" position="); //$NON-NLS-1$
        buf.append(position());
        buf.append(" limit="); //$NON-NLS-1$
        buf.append(limit());
        return buf.toString();
    
public static java.nio.FloatBufferwrap(float[] array)
Creates a new float buffer by wrapping the given float array.

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

param
array the float array which the new buffer will be based on.
return
the created float buffer.
since
Android 1.0

        return wrap(array, 0, array.length);
    
public static java.nio.FloatBufferwrap(float[] array, int start, int len)
Creates a new float buffer by wrapping the given float array.

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

param
array the float array which the new buffer will be based on.
param
start the start index, must not be negative and not greater than {@code array.length}.
param
len the length, must not be negative and not greater than {@code array.length - start}.
return
the created float buffer.
exception
IndexOutOfBoundsException if either {@code start} or {@code len} is invalid.
exception
NullPointerException if {@code array} is null.
since
Android 1.0

                if (array == null) {
                        throw new NullPointerException();
                }
                if (start < 0 || len < 0 || (long)start + (long)len > array.length) {
                        throw new IndexOutOfBoundsException();
                }

        FloatBuffer buf = BufferFactory.newFloatBuffer(array);
        buf.position = start;
        buf.limit = start + len;

        return buf;