Setting the Byte Ordering for a ByteBuffer - Java File Path IO

Java examples for File Path IO:ByteBuffer

Description

Setting the Byte Ordering for a ByteBuffer

Demo Code

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

public class Main {
  public static void main(String[] args) throws Exception {
    // Obtain a ByteBuffer; see also Creating a ByteBuffer
    ByteBuffer buf = ByteBuffer.allocate(10);

    // Get default byte ordering
    ByteOrder order = buf.order(); // ByteOrder.BIG_ENDIAN

    // Put a multibyte value
    buf.putShort(0, (short) 123);
    buf.get(0); // 0
    buf.get(1); // 123

    // Set to little endian
    buf.order(ByteOrder.LITTLE_ENDIAN);

    // Put a multibyte value
    buf.putShort(0, (short) 123);
    buf.get(0); // 123
    buf.get(1); // 0
  }//from   www  .j av a 2  s .  c om
}

Related Tutorials