ByteBuffer order() in Java

order(): This method is available in java.nio.ByteBuffer class of Java.

Approach 1: When the method does not take any argument.

Syntax:

ByteOrder java.nio.ByteBuffer.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.

Parameters: NA

Returns: This buffer's byte order.

Exceptions: NA

Java

import java.nio.ByteBuffer;

public class ByteBufferorder {
    public static void main(String[] args) {

        byte array[] = { 1234 };
        ByteBuffer bb = ByteBuffer.wrap(array);

        System.out.println(bb.order());

    }
}

Output:

BIG_ENDIAN


Approach 2: When the method takes one argument.

Syntax:

ByteBuffer java.nio.ByteBuffer.order(ByteOrder bo)

This method Modifies this buffer's byte order.

Parameters: One parameter is required for this method.

bo: The new byte order, either BIG_ENDIAN or LITTLE_ENDIAN.

Returns: This buffer.

Exceptions: NA

Java

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class ByteBufferorder2 {
    public static void main(String[] args) {

        byte array[] = { 1234 };
        ByteBuffer bb = ByteBuffer.wrap(array);

        ByteOrder bo = ByteOrder.BIG_ENDIAN;
        System.out.println(bb.order(bo));

    }
}

Output:

java.nio.HeapByteBuffer[pos=0 lim=4 cap=4]

No comments:

Post a Comment