Wednesday, November 20, 2024
Google search engine
HomeLanguagesJavaByteBuffer order() method in Java with Examples

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:




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


Output:

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


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:




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


Output:

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


Output:

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

Reference:

RELATED ARTICLES

Most Popular

Recent Comments