ByteBuffer order() method in Java with Examples

order()

The order() method of java.nio.ByteBuffer class is used to retrieve this buffer’s byte order. The byte order is used when reading or writing multibyte values, and when creating buffers that are views of this byte buffer. The order of a newly-created byte buffer is always BIG_ENDIAN.

Syntax:

public final ByteOrder order()

Return Value: This method returns this buffer’s byte order.

Below are the examples to illustrate the order() method:

Examples 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

Original ByteBuffer: 
10 20 30 

Byte Value: BIG_ENDIAN

Examples 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

Byte Value: BIG_ENDIAN
order(ByteOrder bo)

The order(ByteOrder bo) method of ByteBuffer is used to modifie this buffer’s byte order.

Syntax:

public final ByteBuffer order(ByteOrder bo)

Parameters: This method takes the new byte order, either BIG_ENDIAN or LITTLE_ENDIAN as a parameter.

Return Value: This method returns This buffer.

Below are the examples to illustrate the order(ByteOrder bo) method:

Examples 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

Old Byte Order: BIG_ENDIAN
New Byte Order: LITTLE_ENDIAN

Examples 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

Original ByteBuffer: 
10 20 30 Old Byte Order: BIG_ENDIAN
New Byte Order: LITTLE_ENDIAN

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.