Java CharBuffer order()方法及示例
java.nio.CharBuffer 类的 order() 方法是用来检索这个缓冲区的字节顺序。通过分配或包装现有的char数组而创建的char缓冲区的字节顺序是底层硬件的本地顺序。作为一个字节缓冲区的视图创建的char缓冲区的字节顺序是视图创建时的字节缓冲区的字节顺序。
语法
public abstract 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 CharBuffer
// and allocating size capacity
CharBuffer cb
= CharBuffer.allocate(4);
// append the int value in the charbuffer
cb.append('a')
.append('b')
.append('c')
.append('d');
// rewind the Bytebuffer
cb.rewind();
// Retrieve the ByteOrder
// using order() method
ByteOrder order = cb.order();
// print the char buffer and order
System.out.println("CharBuffer is : "
+ Arrays.toString(cb.array())
+ "\nOrder: " + order);
}
}
输出
CharBuffer is : [a, b, c, d]
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 CharBuffer
// and allocating size capacity
CharBuffer cb = CharBuffer.allocate(4);
// Retrieve the ByteOrder
// using order() method
ByteOrder order = cb.order();
// print the char buffer and order
System.out.println("CharBuffer is : "
+ Arrays.toString(cb.array())
+ "\nOrder: " + order);
}
}
输出
CharBuffer is : [,,, ]
Order: LITTLE_ENDIAN
参考资料: https://docs.oracle.com/javase/9/docs/api/java/nio/CharBuffer.html#order-