java.nio.FloatBuffer Class in Java
A Buffer object can be termed as a container for a fixed amount of data. The buffer acts as a storing box, or temporary staging area, where data can be stored and later retrieved according to the usage. Java Buffer classes are the foundation or base upon which java.nio is built. Float buffers are 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.
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
- Methods for compacting, duplicating, and slicing a float buffer.
METHODS | DESCRIPTION |
---|---|
allocate(int capacity) |
This method allocates a new float buffer. |
array() |
This method returns the float array that backs this buffer |
arrayOffset() |
This method returns the offset within this buffer’s backing array of the first element of the buffer |
asReadOnlyBuffer() |
This method creates a new, read-only float buffer that shares this buffer’s content. |
compact() |
This method compacts this buffer |
compareTo(FloatBuffer that) |
This method compares this buffer to another. |
duplicate() |
This method creates a new float buffer that shares this buffer’s content. |
equals(Object ob) |
This method tells whether this buffer is equal to another object. |
get() |
This method relative gets method. |
get(float[] dst) |
This method relative bulk get method. |
get(float[] dst, int offset, int length) |
This method relative bulk get method. |
get(int index) |
This method absolute get method. |
hasArray() |
This method tells whether this buffer is backed by an accessible float array. |
hashCode() |
This method returns the current hash code of this buffer. |
isDirect() |
This method tells whether this floating buffer is direct. |
order() |
This method retrieves this buffer’s byte order. |
put(float f) |
This method relative put method |
put(float[] src) |
This method relative bulk put method |
put(float[] src, int offset, int length) |
This method relative bulk put method |
put(FloatBuffer src) |
Relative bulk put method |
put(int index, float f) |
Absolute put method |
slice() |
Creates a new float buffer whose content is a shared subsequence of this buffer’s content. |
toString() |
This method returns a string summarizing the state of this buffer. |
wrap(float[] array) |
This method wraps a float array into a buffer. |
wrap(float[] array, int offset, int length) |
This method wraps a float array into a buffer. |
Below is the implementation of some methods of java.nio.FloatBuffer Class :
1. reset(): This method is used to reset the position of this buffer to a position that was previously-marked.
Syntax: public final FloatBuffer reset() Parameters: None Return: Returns the buffer.
Java
// Implementation of reset() method in Java import java.nio.*; import java.util.*; public class Example { public static void main(String[] args) { try { float [] arr = { 10 .5f, 20 .5f, 30 .5f, 40 .5f }; // creating object of FloatBuffer // and allocating size capacity FloatBuffer x = FloatBuffer.wrap(arr); // try to set the position at index 2 x.position( 2 ); // Set this buffer mark position // using mark() method x.mark(); // try to set the position at index 4 x.position( 4 ); // display position System.out.println( "Pos before reset: " + x.position()); // try to call reset() to restore // to the position we marked x.reset(); // display position System.out.println( "Pos after reset: " + x.position()); } catch (InvalidMarkException e) { System.out.println( "New pos is less than " + "the pos " + " marked before " ); System.out.println( "Exception throws: " + e); } } } |
Pos before reset: 4 Pos after reset: 2
2. rewind(): This method is used to rewind this buffer.
Syntax: public final FloatBuffer rewind() Parameters: None Return: Returns the buffer
Java
// Implementation of rewind() method in Java import java.nio.*; import java.util.*; public class Example2 { public static void main(String[] args) { // defining and allocating FloatBuffer // using allocate() method FloatBuffer x = FloatBuffer.allocate( 4 ); // put char value in FloatBuffer // using put() method x.put( 10 .5f); x.put( 20 .5f); // print the float buffer System.out.println( "Buffer before operation: " + Arrays.toString(x.array()) + "\nPosition: " + x.position() + "\nLimit: " + x.limit()); // rewind the Buffer // using rewind() method x.rewind(); // print the floatbuffer System.out.println( "\nBuffer after operation: " + Arrays.toString(x.array()) + "\nPosition: " + x.position() + "\nLimit: " + x.limit()); } } |
Buffer before operation: [10.5, 20.5, 0.0, 0.0] Position: 2 Limit: 4 Buffer after operation: [10.5, 20.5, 0.0, 0.0] Position: 0 Limit: 4
Please Login to comment...