當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Buffer flip()用法及代碼示例


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

當將數據從一個地方傳輸到另一個地方時,通常將此方法與compact()方法結合使用。


用法:

public Buffer 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 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-



相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Buffer flip() methods in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。