Example usage for java.nio ByteBuffer allocate

List of usage examples for java.nio ByteBuffer allocate

Introduction

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

Prototype

public static ByteBuffer allocate(int capacity) 

Source Link

Document

Creates a byte buffer based on a newly allocated byte array.

Usage

From source file:Main.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.allocate(BSIZE);
    bb.asCharBuffer().put("Howdy!");
    char c;//  w w w. ja va  2s .  c  om
    while ((c = bb.getChar()) != 0)
        System.out.print(c + " ");
    System.out.println();

    bb.rewind();
    // Store and read a short:
    bb.asShortBuffer().put((short) 471142);
    System.out.println(bb.getShort());

}

From source file:Main.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.allocate(BSIZE);
    bb.asIntBuffer().put(99471142);//from w  w  w  .  j av  a  2  s  .  c  o m
    System.out.println(bb.getInt());

    bb = ByteBuffer.allocate(BSIZE);
    bb.asLongBuffer().put(99472342341142L);
    System.out.println(bb.getLong());

    bb = ByteBuffer.allocate(BSIZE);

    bb.asFloatBuffer().put(99471142);
    System.out.println(bb.getFloat());

    bb = ByteBuffer.allocate(BSIZE);

    bb.asDoubleBuffer().put(99471142);
    System.out.println(bb.getDouble());

}

From source file:Main.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.allocate(BSIZE);
    bb.asCharBuffer().put("java2s!");
    byte[] dst = new byte[BSIZE];
    bb.rewind();//ww  w  .j a va 2  s  .  c o  m
    bb.get(dst, 0, 3);
    System.out.println(Arrays.toString(dst));

}

From source file:Main.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.allocate(BSIZE);
    bb.asCharBuffer().put("java2s!");
    bb.rewind();//from  ww w .j  a va2s  .  c  om
    byte[] dst = new byte[BSIZE];
    bb.get(dst);
    System.out.println(Arrays.toString(dst));

}

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.order(ByteOrder.BIG_ENDIAN);

    bbuf.put("java2s.com".getBytes());

    System.out.println(Arrays.toString(bbuf.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.order(ByteOrder.LITTLE_ENDIAN);

    bbuf.put("java2s.com".getBytes());

    System.out.println(Arrays.toString(bbuf.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.order(ByteOrder.nativeOrder());

    bbuf.put("java2s.com".getBytes());

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

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:MainClass.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.allocate(BSIZE);
    IntBuffer ib = bb.asIntBuffer();

    ib.put(new int[] { 1, 2, 7, 9, 3, 8, 6 });

    System.out.println(ib.get(3));
    ib.put(3, 1811);/* w  ww .  j a va 2 s.  c o  m*/
    ib.rewind();
    while (ib.hasRemaining()) {
        int i = ib.get();
        if (i == 0)
            break; // Else we'll get the entire buffer
        System.out.println(i);
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ByteBuffer bbuf = ByteBuffer.allocate(100);
    File file = new File("filename");

    boolean append = false;

    FileChannel wChannel = new FileOutputStream(file, append).getChannel();

    wChannel.write(bbuf);//  www. j  a v  a2  s.c  o m

    wChannel.close();
}