ByteBuffer order() method in Java with Examples - GeeksforGeeks

17 Jun, 2019
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:
// 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

Examples 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

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:
// 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

Examples 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

Reference:
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
Java-Functions
Java-NIO package
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Different ways of Reading a text file in Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Generics in Java
Comparator Interface in Java with Examples
HashMap get() Method in Java
Introduction to Java
Difference between Abstract Class and Interface in Java