Java ByteOrder Example
November 17, 2023
Java ByteOrder
belongs to the package java.nio
. The ByteOrder
is the enumeration of byte orders. It has two methods nativeOrder()
and toString()
. The nativeOrder
retrieves the native byte order to allocate the direct buffers for better java performance. We have following constants in ByteOrder
.
ByteOrder.BIG_ENDIAN : Orders bytes from most significant to least significant value. ByteOrder.LITTLE_ENDIAN : Orders bytes from least significant to most significant value.
Example
ByteOrderTest.java
package com.concretepage.nio; import java.nio.ByteBuffer; import java.nio.ByteOrder; public class ByteOrderTest { public static void main(String[] args) { ByteBuffer bb = ByteBuffer.wrap(new byte[30]); bb.asCharBuffer().put("Hellow World!"); print(bb); bb.rewind(); bb.order(ByteOrder.BIG_ENDIAN); bb.asCharBuffer().put("Hellow World!"); print(bb); bb.rewind(); bb.order(ByteOrder.LITTLE_ENDIAN); bb.asCharBuffer().put("Hellow World!"); print(bb); } static void print(ByteBuffer bb){ while(bb.hasRemaining()){ System.out.print(bb.getChar()); } } }