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
参考:
- https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#order–
- https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#order-java.nio.ByteOrder-
相关用法
- Java ByteBuffer get()用法及代码示例
- Java ByteBuffer duplicate()用法及代码示例
- Java ByteBuffer asShortBuffer()用法及代码示例
- Java ByteBuffer asReadOnlyBuffer()用法及代码示例
- Java ByteBuffer compact()用法及代码示例
- Java ByteBuffer asIntBuffer()用法及代码示例
- Java ByteBuffer compareTo()用法及代码示例
- Java ByteBuffer equals()用法及代码示例
- Java ByteBuffer asLongBuffer()用法及代码示例
- Java ByteBuffer asFloatBuffer()用法及代码示例
- Java ByteBuffer array()用法及代码示例
- Java ByteBuffer asCharBuffer()用法及代码示例
- Java ByteBuffer arrayOffset()用法及代码示例
- Java ByteBuffer wrap()用法及代码示例
- Java ByteBuffer slice()用法及代码示例
注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 ByteBuffer order() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。