📜  Java中的缓冲区翻转()方法与示例

📅  最后修改于: 2022-05-13 01:55:13.401000             🧑  作者: Mango

Java中的缓冲区翻转()方法与示例

Java.nio.ByteBuffer 类flip()方法用于翻转这个缓冲区。限制设置为当前位置,然后位置设置为零。如果标记已定义,则将其丢弃。在一系列通道读取或放置操作之后,调用此方法以准备一系列通道写入或相关获取操作。

例如:

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

在将数据从一个地方传输到另一个地方时,此方法通常与 compact() 方法结合使用。

句法:

public Buffer 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 ByteBuffer
        // using wrap() method
        ByteBuffer byteBuffer = ByteBuffer.wrap(bb);
  
        // Typecast ByteBuffer to Buffer
        Buffer buffer = (Buffer)byteBuffer;
  
        // set position at index 1
        buffer.position(1);
  
        // print the buffer
        System.out.println("Buffer before flip: "
                           + Arrays.toString((byte[])buffer.array())
                           + "\nPosition: " + buffer.position()
                           + "\nLimit: " + buffer.limit());
  
        // Flip the Buffer
        // using flip() method
        buffer.flip();
  
        // print the buffer
        System.out.println("\nBuffer after flip: "
                           + Arrays.toString((byte[])buffer.array())
                           + "\nPosition: " + buffer.position()
                           + "\nLimit: " + buffer.limit());
    }
}
输出:
Buffer before flip: [10, 20, 30]
Position: 1
Limit: 3

Buffer 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);
  
        // Typecast ByteBuffer to Buffer
        Buffer buffer = (Buffer)byteBuffer;
  
        // set position at index 1
        buffer.position(1);
  
        // print the buffer
        System.out.println("Buffer before flip: "
                           + Arrays.toString((byte[])buffer.array())
                           + "\nPosition: " + buffer.position()
                           + "\nLimit: " + buffer.limit());
  
        // Flip the Buffer
        // using flip() method
        buffer.flip();
  
        // print the buffer
        System.out.println("\nBuffer after flip: "
                           + Arrays.toString((byte[])buffer.array())
                           + "\nPosition: " + buffer.position()
                           + "\nLimit: " + buffer.limit());
    }
}
输出:
Buffer before flip: [20, 30, 0, 0]
Position: 1
Limit: 4

Buffer after flip: [20, 30, 0, 0]
Position: 0
Limit: 1

参考: https://docs.oracle.com/javase/9/docs/api/ Java/nio/Buffer.html#flip–