java.nio.ByteBuffer类的flip()方法用于翻转此缓冲区。将限制设置为当前位置,然后将该位置设置为零。如果定义了标记,则将其丢弃。在序列完成channel-read或放置操作之后,调用此方法为序列进行准备的channel-write或相对get操作。
例如:
buf.put(magic); // Prepend header in.read(buf); // Read data into rest of buffer buf.flip(); // Flip buffer out.write(buf); // Write header + data to channel
当将数据从一个地方传输到另一个地方时,通常将此方法与紧凑方法结合使用。
用法:
public ByteBuffer flip()
返回值:此方法返回此缓冲区。
下面是说明flip()方法的示例:
范例1:
// Java program to demonstrate
// flip() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declare and initialize the byte array
byte[] bb = { 10, 20, 30 };
// wrap the byte array into floatBuffer
// using wrap() method
ByteBuffer byteBuffer = ByteBuffer.wrap(bb);
// set position at index 1
byteBuffer.position(1);
// print the byte buffer
System.out.println("ByteBuffer before flip:"
+ Arrays.toString(byteBuffer.array())
+ "\nPosition:" + byteBuffer.position()
+ "\nLimit:" + byteBuffer.limit());
// Flip the byteBuffer
// using flip() method
byteBuffer.flip();
// print the byte buffer
System.out.println("\nByteBuffer after flip:"
+ Arrays.toString(byteBuffer.array())
+ "\nPosition:" + byteBuffer.position()
+ "\nLimit:" + byteBuffer.limit());
}
}
输出:
ByteBuffer before flip:[10, 20, 30] Position:1 Limit:3 ByteBuffer after flip:[10, 20, 30] Position:0 Limit:1
输出:
ByteBuffer before flip:[10, 20, 30] Position:1 Limit:3 ByteBuffer after flip:[10, 20, 30] Position:0 Limit:1
范例2:
// Java program to demonstrate
// flip() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// defining and allocating ByteBuffer
// using allocate() method
ByteBuffer byteBuffer = ByteBuffer.allocate(4);
// put byte value in byteBuffer
// using put() method
byteBuffer.put((byte)20);
byteBuffer.put((byte)30);
// print the byte buffer
System.out.println("ByteBuffer before flip:"
+ Arrays.toString(byteBuffer.array())
+ "\nPosition:" + byteBuffer.position()
+ "\nLimit:" + byteBuffer.limit());
// Flip the byteBuffer
// using flip() method
byteBuffer.flip();
// print the byte buffer
System.out.println("\nByteBuffer after flip:"
+ Arrays.toString(byteBuffer.array())
+ "\nPosition:" + byteBuffer.position()
+ "\nLimit:" + byteBuffer.limit());
}
}
输出:
ByteBuffer before flip:[20, 30, 0, 0] Position:2 Limit:4 ByteBuffer after flip:[20, 30, 0, 0] Position:0 Limit:2
参考: https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#flip-
相关用法
- Java ByteBuffer put()方法用法及代码示例
- Java ByteBuffer putShort()用法及代码示例
- Java ByteBuffer wrap()用法及代码示例
- Java ByteBuffer putLong()用法及代码示例
- Java ByteBuffer putInt()用法及代码示例
- Java ByteBuffer putFloat()用法及代码示例
- Java ByteBuffer putDouble()用法及代码示例
- Java ByteBuffer putChar()用法及代码示例
- Java ByteBuffer limit()用法及代码示例
- Java ByteBuffer isDirect()用法及代码示例
- Java ByteBuffer clear()用法及代码示例
- Java ByteBuffer rewind()用法及代码示例
- Java ByteBuffer reset()用法及代码示例
- Java ByteBuffer position()用法及代码示例
- Java ByteBuffer mark()用法及代码示例
注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 ByteBuffer flip() methods in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。