BufferByteOrder.java

import java.nio.*;
class BufferByteOrder {
  public static void main(String[] args) {
    System.out.println(ByteOrder.nativeOrder());  // Affiche LITTLE_ENDIAN
    ByteBuffer byteB = ByteBuffer.allocate(12);
    ByteBuffer byteBBis = byteB.duplicate(); 
    System.out.println(byteB.order());            // Affiche BIG_ENDIAN
    byteB.putInt(3);
    for (int i=0; i<4; i++)
      System.out.print(byteBBis.get());           // Affiche 0003
    System.out.println();
    byteB.order(ByteOrder.LITTLE_ENDIAN);
    System.out.println(byteB.order());            // Affiche LITTLE_ENDIAN
    byteB.putInt(3);
    for (int i=0; i<4; i++)
      System.out.print(byteBBis.get());           // Affiche 3000
    System.out.println();
    
    int[] tab = new int[1];
    IntBuffer intB = IntBuffer.wrap(tab);
    System.out.println(intB.order());             // Affiche LITTLE_INDIAN
    intB.put(3);
    System.out.println(tab[0]);                   // Affiche 3

    byteB.order(ByteOrder.BIG_ENDIAN);
    intB = byteB.asIntBuffer();
    System.out.println(intB.order());             // Affiche BIG_ENDIAN
    byteB.order(ByteOrder.LITTLE_ENDIAN);
    System.out.println(intB.order());             // Affiche BIG_ENDIAN
    System.out.println(byteB.order());            // Affiche LITTLE_ENDIAN
  }
}