The order()
method of the DoubleBuffer
class in Java retrieves a buffer’s byte order.
The process is illustrated below.
To use the order()
method, you will need to import the DoubleBuffer
class into your program, as shown below.
import java.nio.DoubleBuffer
The prototype of this method is shown below.
public final ByteOrder order()
The order()
method does not accept any parameters.
The order()
method returns the buffer’s byte order.
The byte order of a DoubleBuffer
object can be one of the following:
If the buffer is created by allocation or by wrapping an existing double
array, then the byte order reflects the native order of the underlying hardware.
If the buffer is created as a view of a ByteBuffer
, then the byte order reflects the byte order of the ByteBuffer
object when the DoubleBuffer
view was created.
The code below shows how the order()
method can be used in Java.
import java.nio.*;import java.util.*;class orderMethod {public static void main(String[] args) {// initialize buffer instancesByteBuffer byteBuffer = ByteBuffer.allocate(5);DoubleBuffer firstBuffer = DoubleBuffer.allocate(5);DoubleBuffer secondBuffer = byteBuffer.asDoubleBuffer();// Retrieve byte order of buffersByteOrder currentByteOrder = firstBuffer.order();System.out.println("The byte order of firstBuffer is: " + currentByteOrder);currentByteOrder = secondBuffer.order();System.out.println("The byte order of secondBuffer is: " + currentByteOrder);}}
First, two DoubleBuffer
objects are initialized: firstBuffer
and secondBuffer
. firstBuffer
is created through the allocate()
method, whereas secondBuffer
is a view of a ByteBuffer
object created through the asDoubleBuffer()
method.
Since firstBuffer
was allocated directly, the order()
method in line returns the native order of the underlying hardware, i.e., LITTLE_ENDIAN
.
On the other hand, secondBuffer
is a view of a ByteBuffer
object, so the order()
method in line returns the byte order of the ByteBuffer
object at the moment when the DoubleBuffer
view was created, i.e., BIG_ENDIAN
.