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


Java ByteBuffer 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

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


用法:

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-



相關用法


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