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


Java ByteBuffer order()用法及代碼示例


order()

java.nio.ByteBuffer類的order()方法用於檢索此緩衝區的字節順序。在讀取或寫入多字節值以及創建作為此字節緩衝區視圖的緩衝區時,將使用字節順序。 newly-created字節緩衝區的順序始終為BIG_ENDIAN。

用法:

public final ByteOrder order()

返回值:此方法返回此緩衝區的字節順序。


下麵是說明order()方法的示例:

範例1:

// Java program to demonstrate 
// order() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
        // creating object of ByteBuffer 
        // and allocating size capacity 
        ByteBuffer bb = ByteBuffer.allocate(12); 
  
        // putting the int value in the bytebuffer 
        bb.asIntBuffer() 
            .put(10) 
            .put(20) 
            .put(30); 
  
        // rewind the Bytebuffer 
        bb.rewind(); 
  
        // print the ByteBuffer 
        System.out.println("Original ByteBuffer: "); 
        for (int i = 1; i <= 3; i++) 
            System.out.print(bb.getInt() + " "); 
  
        // rewind the Bytebuffer 
        bb.rewind(); 
  
        // Reads the Int at this buffer's current position 
        // using order() method 
        ByteOrder value = bb.order(); 
  
        // print the int value 
        System.out.println("\n\nByte Value: " + value); 
    } 
}
輸出:
Original ByteBuffer: 
10 20 30 

Byte Value: BIG_ENDIAN

範例2:

// Java program to demonstrate 
// order() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
        // creating object of ByteBuffer 
        // and allocating size capacity 
        ByteBuffer bb = ByteBuffer.allocate(12); 
  
        // Reads the Int at this buffer's current position 
        // using order() method 
        ByteOrder value = bb.order(); 
  
        // print the int value 
        System.out.println("Byte Value: " + value); 
    } 
}
輸出:
Byte Value: BIG_ENDIAN
訂單(ByteOrder bo)

ByteBuffer的order(ByteOrder bo)方法用於修改此緩衝區的字節順序。

用法:

public final ByteBuffer order(ByteOrder bo)

參數:此方法采用新的字節順序,即BIG_ENDIAN或LITTLE_ENDIAN作為參數。

返回值:此方法返回此緩衝區。

下麵的示例說明了order(ByteOrder bo)方法:


範例1:

// Java program to demonstrate 
// order() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
        // creating object of ByteBuffer 
        // and allocating size capacity 
        ByteBuffer bb = ByteBuffer.allocate(12); 
  
        // Reads the Int at this buffer's current position 
        // using order() method 
        ByteOrder oldbyteorder = bb.order(); 
  
        // print the result 
        System.out.println("Old Byte Order: " + oldbyteorder); 
  
        // Modifies this buffer's byte order 
        // by using order() method 
        ByteBuffer bb1 = bb.order(ByteOrder.nativeOrder()); 
  
        // Reads the Int at this buffer's current position 
        // using order() method 
        ByteOrder newbyteorder = bb1.order(); 
  
        // print the result 
        System.out.println("New Byte Order: " + newbyteorder); 
    } 
}
輸出:
Old Byte Order: BIG_ENDIAN
New Byte Order: LITTLE_ENDIAN

範例2:

// Java program to demonstrate 
// order() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
        // creating object of ByteBuffer 
        // and allocating size capacity 
        ByteBuffer bb = ByteBuffer.allocate(12); 
  
        // putting the int value in the bytebuffer 
        bb.asIntBuffer() 
            .put(10) 
            .put(20) 
            .put(30); 
  
        // rewind the Bytebuffer 
        bb.rewind(); 
  
        // print the ByteBuffer 
        System.out.println("Original ByteBuffer: "); 
        for (int i = 1; i <= 3; i++) 
            System.out.print(bb.getInt() + " "); 
  
        // rewind the Bytebuffer 
        bb.rewind(); 
  
        // Reads the Int at this buffer's current position 
        // using order() method 
        ByteOrder oldbyteorder = bb.order(); 
  
        // print the result 
        System.out.println("Old Byte Order: " + oldbyteorder); 
  
        // Modifies this buffer's byte order 
        // by using order() method 
        ByteBuffer bb1 = bb.order(ByteOrder.nativeOrder()); 
  
        // Reads the Int at this buffer's current position 
        // using order() method 
        ByteOrder newbyteorder = bb1.order(); 
  
        // print the result 
        System.out.println("New Byte Order: " + newbyteorder); 
    } 
}
輸出:
Original ByteBuffer: 
10 20 30 Old Byte Order: BIG_ENDIAN
New Byte Order: LITTLE_ENDIAN

參考:



相關用法


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