Module java.base
Package java.nio

Class FloatBuffer

  • All Implemented Interfaces:
    Comparable<FloatBuffer>

    public abstract class FloatBuffer
    extends Buffer
    implements Comparable<FloatBuffer>
    A float buffer.

    This class defines four categories of operations upon float buffers:

    • Absolute and relative get and put methods that read and write single floats;

    • Relative bulk get methods that transfer contiguous sequences of floats from this buffer into an array; and

    • Relative bulk put methods that transfer contiguous sequences of floats from a float array or some other float buffer into this buffer; and

    • A method for compacting a float buffer.

    Float buffers can be created either by allocation, which allocates space for the buffer's content, by wrapping an existing float array into a buffer, or by creating a view of an existing byte buffer.

    Like a byte buffer, a float buffer is either direct or non-direct. A float buffer created via the wrap methods of this class will be non-direct. A float buffer created as a view of a byte buffer will be direct if, and only if, the byte buffer itself is direct. Whether or not a float buffer is direct may be determined by invoking the isDirect method.

    Methods in this class that do not otherwise have a value to return are specified to return the buffer upon which they are invoked. This allows method invocations to be chained.

    Since:
    1.4
    • Method Detail

      • allocate

        public static FloatBuffer allocate​(int capacity)
        Allocates a new float buffer.

        The new buffer's position will be zero, its limit will be its capacity, its mark will be undefined, each of its elements will be initialized to zero, and its byte order will be the native order of the underlying hardware. It will have a backing array, and its array offset will be zero.

        Parameters:
        capacity - The new buffer's capacity, in floats
        Returns:
        The new float buffer
        Throws:
        IllegalArgumentException - If the capacity is a negative integer
      • wrap

        public static FloatBuffer wrap​(float[] array,
                                       int offset,
                                       int length)
        Wraps a float array into a buffer.

        The new buffer will be backed by the given float array; that is, modifications to the buffer will cause the array to be modified and vice versa. The new buffer's capacity will be array.length, its position will be offset, its limit will be offset + length, its mark will be undefined, and its byte order will be the native order of the underlying hardware. Its backing array will be the given array, and its array offset will be zero.

        Parameters:
        array - The array that will back the new buffer
        offset - The offset of the subarray to be used; must be non-negative and no larger than array.length. The new buffer's position will be set to this value.
        length - The length of the subarray to be used; must be non-negative and no larger than array.length - offset. The new buffer's limit will be set to offset + length.
        Returns:
        The new float buffer
        Throws:
        IndexOutOfBoundsException - If the preconditions on the offset and length parameters do not hold
      • wrap

        public static FloatBuffer wrap​(float[] array)
        Wraps a float array into a buffer.

        The new buffer will be backed by the given float array; that is, modifications to the buffer will cause the array to be modified and vice versa. The new buffer's capacity and limit will be array.length, its position will be zero, its mark will be undefined, and its byte order will be the native order of the underlying hardware. Its backing array will be the given array, and its array offset will be zero.

        Parameters:
        array - The array that will back this buffer
        Returns:
        The new float buffer
      • slice

        public abstract FloatBuffer slice()
        Creates a new float 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 floats remaining in this buffer, its mark will be undefined, and its byte order will be identical to that 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:
        slice in class Buffer
        Returns:
        The new float buffer
      • duplicate

        public abstract FloatBuffer duplicate()
        Creates a new float 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, mark values, and byte order 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 Buffer
        Returns:
        The new float buffer
      • asReadOnlyBuffer

        public abstract FloatBuffer asReadOnlyBuffer()
        Creates a new, read-only float 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, mark values, and byte order 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.

        Returns:
        The new, read-only float buffer
      • get

        public abstract float get()
        Relative get method. Reads the float at this buffer's current position, and then increments the position.
        Returns:
        The float at the buffer's current position
        Throws:
        BufferUnderflowException - If the buffer's current position is not smaller than its limit
      • put

        public abstract FloatBuffer put​(float f)
        Relative put method  (optional operation).

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

        Parameters:
        f - The float to be written
        Returns:
        This buffer
        Throws:
        BufferOverflowException - If this buffer's current position is not smaller than its limit
        ReadOnlyBufferException - If this buffer is read-only
      • get

        public abstract float get​(int index)
        Absolute get method. Reads the float at the given index.
        Parameters:
        index - The index from which the float will be read
        Returns:
        The float at the given index
        Throws:
        IndexOutOfBoundsException - If index is negative or not smaller than the buffer's limit
      • put

        public abstract FloatBuffer put​(int index,
                                        float f)
        Absolute put method  (optional operation).

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

        Parameters:
        index - The index at which the float will be written
        f - The float value to be written
        Returns:
        This buffer
        Throws:
        IndexOutOfBoundsException - If index is negative or not smaller than the buffer's limit
        ReadOnlyBufferException - If this buffer is read-only
      • get

        public FloatBuffer get​(float[] dst,
                               int offset,
                               int length)
        Relative bulk get method.

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

        Otherwise, this method copies length floats 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 floats in this buffer and it is potentially much more efficient.

        Parameters:
        dst - The array into which floats are to be written
        offset - The offset within the array of the first float to be written; must be non-negative and no larger than dst.length
        length - The maximum number of floats to be written to the given array; must be non-negative and no larger than dst.length - offset
        Returns:
        This buffer
        Throws:
        BufferUnderflowException - If there are fewer than length floats remaining in this buffer
        IndexOutOfBoundsException - If the preconditions on the offset and length parameters do not hold
      • get

        public FloatBuffer get​(float[] dst)
        Relative bulk get method.

        This method transfers floats from this buffer into the given destination array. An invocation of this method of the form src.get(a) behaves in exactly the same way as the invocation

             src.get(a, 0, a.length) 

        Parameters:
        dst - The destination array
        Returns:
        This buffer
        Throws:
        BufferUnderflowException - If there are fewer than length floats remaining in this buffer
      • put

        public FloatBuffer put​(FloatBuffer src)
        Relative bulk put method  (optional operation).

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

        Otherwise, this method copies n = src.remaining() floats 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. If this buffer and the source buffer share the same backing array or memory, then the result will be as if the source elements were first copied to an intermediate location before being written into this buffer.

        Parameters:
        src - The source buffer from which floats are to be read; must not be this buffer
        Returns:
        This buffer
        Throws:
        BufferOverflowException - If there is insufficient space in this buffer for the remaining floats in the source buffer
        IllegalArgumentException - If the source buffer is this buffer
        ReadOnlyBufferException - If this buffer is read-only
      • put

        public FloatBuffer put​(float[] src,
                               int offset,
                               int length)
        Relative bulk put method  (optional operation).

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

        Otherwise, this method copies length floats 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.

        Parameters:
        src - The array from which floats are to be read
        offset - The offset within the array of the first float to be read; must be non-negative and no larger than array.length
        length - The number of floats to be read from the given array; must be non-negative and no larger than array.length - offset
        Returns:
        This buffer
        Throws:
        BufferOverflowException - If there is insufficient space in this buffer
        IndexOutOfBoundsException - If the preconditions on the offset and length parameters do not hold
        ReadOnlyBufferException - If this buffer is read-only
      • put

        public final FloatBuffer put​(float[] src)
        Relative bulk put method  (optional operation).

        This method transfers the entire content of the given source float array into this buffer. An invocation of this method of the form dst.put(a) behaves in exactly the same way as the invocation

             dst.put(a, 0, a.length) 

        Parameters:
        src - The source array
        Returns:
        This buffer
        Throws: