Example usage for java.nio ByteBuffer array

List of usage examples for java.nio ByteBuffer array

Introduction

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

Prototype

public final byte[] array() 

Source Link

Document

Returns the byte array which this buffer is based on, if there is one.

Usage

From source file:Main.java

public static void main(String args[]) throws IOException {
    FileInputStream fis = new FileInputStream("FileChannelExample.java");
    FileChannel fc = fis.getChannel();

    ByteBuffer bb = ByteBuffer.allocate((int) fc.size());

    fc.read(bb);/*from   w w  w.  j  a v  a  2 s  .c  o m*/
    bb.flip();

    String fileContent = new String(bb.array());

    fc.close();
    fc = null;

    System.out.println("fileContent = " + fileContent);
}

From source file:Test.java

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

    AsynchronousSocketChannel client = AsynchronousSocketChannel.open();
    InetSocketAddress address = new InetSocketAddress("localhost", 5000);

    Future<Void> future = client.connect(address);
    System.out.println("Client: Waiting for the connection to complete");
    future.get();//from ww w  . ja v  a  2  s .co m

    String message = "";
    while (!message.equals("quit")) {
        System.out.print("Enter a message: ");
        Scanner scanner = new Scanner(System.in);
        message = scanner.nextLine();
        System.out.println("Client: Sending ...");
        ByteBuffer buffer = ByteBuffer.wrap(message.getBytes());
        System.out.println("Client: Message sent: " + new String(buffer.array()));
        client.write(buffer);
    }
}

From source file:com.moscona.dataSpace.debug.ByteBufferTest.java

public static void main(String[] args) {
    ByteBuffer bytes = ByteBuffer.allocate(40);
    print("after allocation", bytes);
    byte[] byteArray = bytes.array();
    for (int i = 0; i < byteArray.length; i++) {
        byteArray[i] = 1;//from ww w  .  j  ava 2 s.  c o  m
    }
    print("after filling with 1s", bytes);
    IntBuffer ints = bytes.asIntBuffer();
    System.out.println("ints has array: " + ints.hasArray());
    int[] array = ints.array();
    for (int i = 0; i < array.length; i++) {
        array[i] = 100 + i;
    }
    print("after filling ints array", bytes);
}

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.putInt(123);//from  ww  w.  jav  a  2  s  .  c o m

    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.putLong(123);/*from www.  j  av a 2 s. c  o  m*/

    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.putChar('a');

    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.putFloat(123F);/*from www . ja  va2s.c o m*/

    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.putInt(2, 123);//ww  w .j  a v a  2s.c o  m

    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.putDouble(123);/*ww  w. j  a v a  2 s  .  co  m*/

    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.putChar(5, 'a');

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