📌  相关文章
📜  Java中的 ByteBuffer order() 方法及示例

📅  最后修改于: 2022-05-13 01:54:32.245000             🧑  作者: Mango

Java中的 ByteBuffer order() 方法及示例

命令()

Java.nio.ByteBuffer类的order()方法用于检索此缓冲区的字节顺序。在读取或写入多字节值以及创建作为此字节缓冲区视图的缓冲区时使用字节顺序。新创建的字节缓冲区的顺序总是 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
订单(字节订单博)

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-