Example usage for java.nio ByteBuffer wrap

List of usage examples for java.nio ByteBuffer wrap

Introduction

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

Prototype

public static ByteBuffer wrap(byte[] array) 

Source Link

Document

Creates a new byte buffer by wrapping the given byte array.

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 w  w w .j  a v a 2s . co 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 {
    byte[] bytes = new byte[10];
    ByteBuffer buf = ByteBuffer.wrap(bytes);

    buf.clear();/*from w ww  .ja v a 2 s .co  m*/
    bytes = new byte[buf.capacity()];
    buf.get(bytes, 0, bytes.length);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    byte[] bytes = new byte[10];
    ByteBuffer buf = ByteBuffer.wrap(bytes);

    System.out.println(Arrays.toString(buf.array()));
    System.out.println(buf.toString());
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    // Create a ByteBuffer from a byte array
    byte[] bytes = new byte[10];
    ByteBuffer buf = ByteBuffer.wrap(bytes);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    // Create a ByteBuffer using a byte array
    byte[] bytes = new byte[10];
    ByteBuffer buffer = ByteBuffer.wrap(bytes);

    // Create a non-direct ByteBuffer with a 10 byte capacity
    // The underlying storage is a byte array.
    buffer = ByteBuffer.allocate(10);

    // Create a memory-mapped ByteBuffer with a 10 byte capacity.
    buffer = ByteBuffer.allocateDirect(10);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    // Create a ByteBuffer from a byte array
    byte[] bytes = new byte[10];
    ByteBuffer buffer = ByteBuffer.wrap(bytes);

    // Retrieve bytes between the position and limit
    bytes = new byte[buffer.remaining()];
    buffer.get(bytes, 0, bytes.length);//w ww  .j av  a 2 s. c o m

    // Retrieve all bytes in the buffer
    buffer.clear();
    bytes = new byte[buffer.capacity()];
    buffer.get(bytes, 0, bytes.length);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    byte[] bytes = new byte[10];
    ByteBuffer buf = ByteBuffer.allocate(10);
    buf = ByteBuffer.wrap(bytes);

    System.out.println(Arrays.toString(buf.array()));
    System.out.println(buf.toString());
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    byte[] bytes = new byte[10];
    ByteBuffer buf = ByteBuffer.allocateDirect(10);
    buf = ByteBuffer.wrap(bytes);

    System.out.println(Arrays.toString(buf.array()));
    System.out.println(buf.toString());
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    ByteBuffer bbuf = ByteBuffer.wrap(new byte[10]);
    boolean isDirect = bbuf.isDirect(); // false

    bbuf = ByteBuffer.allocate(10);
    isDirect = bbuf.isDirect(); // false

    bbuf = ByteBuffer.allocateDirect(10);
    isDirect = bbuf.isDirect(); // true

}

From source file:Main.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, 'a' });
    bb.rewind();//from   w  w w . j a v  a  2s .  c om
    System.out.println("Byte Buffer");
    while (bb.hasRemaining())
        System.out.println(bb.position() + " -> " + bb.get());
    CharBuffer cb = ((ByteBuffer) bb.rewind()).asCharBuffer();
    System.out.println("Char Buffer");
    while (cb.hasRemaining())
        System.out.println(cb.position() + " -> " + cb.get());
    FloatBuffer fb = ((ByteBuffer) bb.rewind()).asFloatBuffer();
    System.out.println("Float Buffer");
    while (fb.hasRemaining())
        System.out.println(fb.position() + " -> " + fb.get());
    IntBuffer ib = ((ByteBuffer) bb.rewind()).asIntBuffer();
    System.out.println("Int Buffer");
    while (ib.hasRemaining())
        System.out.println(ib.position() + " -> " + ib.get());
    LongBuffer lb = ((ByteBuffer) bb.rewind()).asLongBuffer();
    System.out.println("Long Buffer");
    while (lb.hasRemaining())
        System.out.println(lb.position() + " -> " + lb.get());
    ShortBuffer sb = ((ByteBuffer) bb.rewind()).asShortBuffer();
    System.out.println("Short Buffer");
    while (sb.hasRemaining())
        System.out.println(sb.position() + " -> " + sb.get());
    DoubleBuffer db = ((ByteBuffer) bb.rewind()).asDoubleBuffer();
    System.out.println("Double Buffer");
    while (db.hasRemaining())
        System.out.println(db.position() + " -> " + db.get());
}