当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。