Home  >  Core Java

Example of ByteOrder in Java NIO

By Arvind Rai, November 17, 2013
ByteOrder belongs to the package java.nio. ByteOrder is the enumeration of byte orders. ByteOrder has two methods nativeOrder() and toString(). nativeOrder retrieves the native byte order to allocate the direct buffers for better java performance. We have constants in ByteOrder.

ByteOrder.BIG_ENDIAN

ByteOrder.BIG_ENDIAN is the constant of ByteOrder. It orders bytes from most significant to least significant value.

ByteOrder.LITTLE_ENDIAN

ByteOrder.LITTLE_ENDIAN is the constant of ByteOrder. It orders bytes from least significant to most significant value.

Find the example below.

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());
		  }
	  }
}
 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us