java.nio.FloatBuffer类的rewind()方法用于倒带此缓冲区。此方法将位置设置为零,并且限制不受影响,并且如果有任何先前标记的位置将被丢弃。当需要对channel-write进行序列化或进行get操作时,应调用此方法。这意味着,如果已经写入了缓冲区数据,则需要将其复制到另一个数组中。例如:
out.write(buf); // Writes the remaining data buf.rewind(); // Rewind the buffer buf.get(array); // Copy data into array
用法:
public final FloatBuffer rewind()
参数:该方法不带任何参数。
返回值:该方法返回此缓冲区。
下面是说明rewind()方法的示例:
范例1:
// Java program to demonstrate
// rewind() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// defining and allocating FloatBuffer
// using allocate() method
FloatBuffer floatBuffer = FloatBuffer.allocate(4);
// put char value in FloatBuffer
// using put() method
floatBuffer.put(10.5f);
floatBuffer.put(20.5f);
// print the float buffer
System.out.println("Buffer before operation:"
+ Arrays.toString(
floatBuffer.array())
+ "\nPosition:"
+ floatBuffer.position()
+ "\nLimit:"
+ floatBuffer.limit());
// rewind the Buffer
// using rewind() method
floatBuffer.rewind();
// print the floatbuffer
System.out.println("\nBuffer after operation:"
+ Arrays.toString(
floatBuffer.array())
+ "\nPosition:"
+ floatBuffer.position()
+ "\nLimit:"
+ floatBuffer.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
范例2:
// Java program to demonstrate
// rewind() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// defining and allocating FloatBuffer
// using allocate() method
FloatBuffer floatBuffer
= FloatBuffer.allocate(5);
// put float value in floatBuffer
// using put() method
floatBuffer.put(10.5f);
floatBuffer.put(20.5f);
floatBuffer.put(30.5f);
// mark will be going to discarded by rewind()
floatBuffer.mark();
// print the buffer
System.out.println("Buffer before operation:"
+ Arrays.toString(
floatBuffer.array())
+ "\nPosition:"
+ floatBuffer.position()
+ "\nLimit:"
+ floatBuffer.limit());
// Rewind the Buffer
// using rewind() method
floatBuffer.rewind();
// print the buffer
System.out.println("\nBuffer after operation:"
+ Arrays.toString(
floatBuffer.array())
+ "\nPosition:"
+ floatBuffer.position()
+ "\nLimit:"
+ floatBuffer.limit());
}
}
输出:
Buffer before operation:[10.5, 20.5, 30.5, 0.0, 0.0] Position:3 Limit:5 Buffer after operation:[10.5, 20.5, 30.5, 0.0, 0.0] Position:0 Limit:5
参考: https://docs.oracle.com/javase/9/docs/api/java/nio/FloatBuffer.html#rewind-
相关用法
- Java FloatBuffer get()用法及代码示例
- Java FloatBuffer put()用法及代码示例
- Java FloatBuffer reset()用法及代码示例
- Java FloatBuffer clear()用法及代码示例
- Java CharBuffer rewind()用法及代码示例
- Java FloatBuffer mark()用法及代码示例
- Java Buffer rewind()用法及代码示例
- Java FloatBuffer flip()用法及代码示例
- Java ByteBuffer rewind()用法及代码示例
- Java FloatBuffer hasArray()用法及代码示例
- Java FloatBuffer arrayOffset()用法及代码示例
- Java FloatBuffer compareTo()用法及代码示例
- Java FloatBuffer compact()用法及代码示例
- Java FloatBuffer allocate()用法及代码示例
- Java FloatBuffer limit()用法及代码示例
注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 FloatBuffer rewind() methods in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。