Java的.nio.FloatBuffer类在Java中
Buffer 对象可以被称为固定数量数据的容器。缓冲区充当存储盒或临时暂存区,可以在其中存储数据,然后根据使用情况进行检索。 Java Buffer 类是构建Java.nio 的基础或基础。浮点缓冲区是通过分配(为缓冲区的内容分配空间)、将现有浮点数组包装到缓冲区中或通过创建现有字节缓冲区的视图来创建的。
此类定义了对浮点缓冲区的四类操作:
- 读取和写入单个浮点数的绝对和相对 get 和 put 方法
- 将连续浮点序列从此缓冲区传输到数组中的相对批量获取方法;和
- 将连续浮点序列从浮点数组或其他一些浮点缓冲区传输到此缓冲区的相对批量放置方法
- 压缩、复制和切片浮动缓冲区的方法。
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. |
下面是Java.nio.FloatBuffer Class 的一些方法的实现:
1.reset():这个方法用来把这个buffer的位置重置到之前标记的位置。
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);
}
}
}
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());
}
}
Pos before reset: 4
Pos after reset: 2
2. rewind():该方法用于回绕这个缓冲区。
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