Java ByteBuffer order()方法及示例

Java ByteBuffer order()方法及示例

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

order(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-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程