Example usage for java.nio ByteBuffer order

List of usage examples for java.nio ByteBuffer order

Introduction

In this page you can find the example usage for java.nio ByteBuffer order.

Prototype

Endianness order

To view the source code for java.nio ByteBuffer order.

Click Source Link

Document

The byte order of this buffer, default is BIG_ENDIAN .

Usage

From source file:MainClass.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[12]);
    bb.asCharBuffer().put("abcdef");
    System.out.println(toString(bb.array()));
    bb.rewind();//from ww  w .j  a  v a2s. c  o m
    bb.order(ByteOrder.BIG_ENDIAN);
    bb.asCharBuffer().put("abcdef");
    System.out.println(toString(bb.array()));
    bb.rewind();
    bb.order(ByteOrder.LITTLE_ENDIAN);
    bb.asCharBuffer().put("abcdef");
    System.out.println(toString(bb.array()));
}

From source file:Endians.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[12]);
    bb.asCharBuffer().put("abcdef");
    System.out.println(toString(bb.array()));
    bb.rewind();/*from w  w w.  j ava  2s  .c o  m*/
    bb.order(ByteOrder.BIG_ENDIAN);
    bb.asCharBuffer().put("abcdef");
    System.out.println(toString(bb.array()));
    bb.rewind();
    bb.order(ByteOrder.LITTLE_ENDIAN);
    bb.asCharBuffer().put("abcdef");
    System.out.println(toString(bb.array()));

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ByteBuffer bbuf = ByteBuffer.allocate(10);
    int capacity = bbuf.capacity(); // 10
    System.out.println(capacity);
    bbuf.putShort(2, (short) 123);

    ByteOrder byteOrder = bbuf.order();

    System.out.println(byteOrder);
}

From source file:MainClass.java

public static void main(String[] argv) throws Exception {
    ByteBuffer bb = ByteBuffer.allocate(20);

    bb.put((byte) 0x07);
    bb.put((byte) 0x08);
    bb.put((byte) 0x09);
    bb.put((byte) 0x10);
    bb.put((byte) 0x11);
    bb.put((byte) 0x12);
    bb.put((byte) 0x13);
    bb.put((byte) 0x14);

    bb.position(1).limit(5);/*from ww w . j  ava 2s.  c o m*/
    bb.mark();

    System.out.println("Expect an exception here");
    System.out.println("" + bb.order().toString() + ": " + bb.getLong());

}

From source file:Main.java

public static byte[] toArray(int value) {
    ByteBuffer buffer = ByteBuffer.allocate(4);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    buffer.putInt(value);//from  w w w. jav  a2s  . com
    return buffer.array();
}

From source file:Main.java

public static int fromArray(byte[] payload) {
    ByteBuffer buffer = ByteBuffer.wrap(payload);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    return buffer.getInt();
}

From source file:Main.java

public static void showByteOrder(ByteBuffer bb) {
    System.out.println("Byte  Order: " + bb.order());
    while (bb.hasRemaining()) {
        System.out.print(bb.get() + "    ");
    }//from   ww w.  j a v a  2  s  .c  o  m
    System.out.println();
}

From source file:Main.java

private static void checkByteOrderLittleEndian(final ByteBuffer buffer) {
    if (buffer.order() != ByteOrder.LITTLE_ENDIAN) {
        throw new IllegalArgumentException("ByteBuffer byte order must be little endian");
    }/*from ww w.  ja v a  2  s.  co  m*/
}

From source file:Main.java

public static byte[] shortArr2byteArr(final short[] pcm, final int length) {
    byte[] dst = new byte[length * 2];
    ByteBuffer buf = ByteBuffer.wrap(dst);
    buf.order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().put(pcm, 0, length);
    return dst;// ww w  .j ava 2 s  . c o m
}

From source file:Main.java

public static byte[] shortToByteArray(int value) {
    ByteBuffer bb = ByteBuffer.allocate(2);
    bb.order(ByteOrder.LITTLE_ENDIAN);
    bb.putShort((short) value);
    return bb.array();
}