Example usage for java.nio CharBuffer allocate

List of usage examples for java.nio CharBuffer allocate

Introduction

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

Prototype

public static CharBuffer allocate(int capacity) 

Source Link

Document

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

Usage

From source file:MainClass.java

public static void main(String[] argv) throws Exception {
    CharBuffer cb = CharBuffer.allocate(100);

    cb.put("This is a test String");

    cb.flip();/*ww  w . ja  v  a2  s  .c  om*/

    // This throws an IllegalArgumentException
    cb.put(cb);

    System.out.println(cb);
}

From source file:Main.java

public static void main(String[] args) {
    CharBuffer cb1 = CharBuffer.allocate(5);
    cb1.put(2, 'j');
    cb1.rewind();/*  w w  w. j  a va2 s. co  m*/

    char[] charArray = new char[5];
    cb1.get(charArray, 0, 2);
    System.out.println(Arrays.toString(charArray));

}

From source file:Main.java

public static void main(String[] args) {
    CharBuffer cb1 = CharBuffer.allocate(5);
    cb1.put(2, 'j');
    cb1.rewind();/*w w  w  . j a  v  a2  s.  co  m*/

    char[] charArray = new char[5];
    cb1.get(charArray);
    System.out.println(Arrays.toString(charArray));

}

From source file:Main.java

public static void main(String[] args) {
    CharBuffer cb1 = CharBuffer.allocate(50);
    cb1.append("java2s.com");
    cb1.rewind();/*from   w ww  . j  a  va  2  s . c  o m*/

    System.out.println(Arrays.toString(cb1.array()));

    CharBuffer cb2 = cb1.slice();
    System.out.println(cb2.equals(cb1));

}

From source file:Main.java

public static void main(String[] args) {
    CharBuffer cb1 = CharBuffer.allocate(50);
    cb1.append("java2s.com");
    cb1.rewind();/*from   w  w  w . j  av  a  2s.c om*/

    System.out.println(Arrays.toString(cb1.array()));

    CharBuffer cb2 = cb1.slice();
    System.out.println(cb2.compareTo(cb1));

}

From source file:Main.java

public static void main(String[] args) {
    CharBuffer cb1 = CharBuffer.allocate(10);
    cb1.append("java2s.com");
    cb1.rewind();//www.j a  v  a 2s. c  o m

    System.out.println(Arrays.toString(cb1.array()));

    CharBuffer cb2 = cb1.subSequence(0, 2);

    System.out.println(cb2);

}

From source file:Main.java

public static void main(String[] args) {
    CharBuffer cb1 = CharBuffer.allocate(50);
    cb1.append("java2s.com");
    cb1.rewind();/*from w ww . ja  va 2  s  . c o  m*/

    System.out.println(cb1.charAt(2));
    System.out.println(Arrays.toString(cb1.array()));

    CharBuffer cb2 = cb1.slice();
    System.out.println(Arrays.toString(cb2.array()));

}

From source file:Main.java

public static void main(String[] args) {
    CharBuffer cb1 = CharBuffer.allocate(50);
    cb1.append("java2s.com");
    cb1.rewind();/*from   w  ww. ja  v  a 2  s.c o  m*/

    System.out.println(cb1.charAt(2));
    System.out.println(Arrays.toString(cb1.array()));

    CharBuffer cb2 = cb1.compact();
    System.out.println(Arrays.toString(cb2.array()));

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    CharBuffer cb = CharBuffer.allocate(100);

    cb.put("This is a test String");

    cb.flip();/*from   w w  w. ja  v  a2  s  .  co  m*/

    System.out.println("hasArray() = " + cb.hasArray());

    char[] carray = cb.array();

    System.out.print("array=");

    for (int i = 0; i < carray.length; i++) {
        System.out.print(carray[i]);
    }

}

From source file:MainClass.java

License:asdf

public static void main(String[] argv) throws Exception {
    CharBuffer buffer = CharBuffer.allocate(100);

    String string = "asdf";

    for (int i = 0; i < string.length(); i++) {
        buffer.put(string.charAt(i));//from w ww .j a  va 2s. c o  m
    }

    buffer.flip();
    drainBuffer(buffer);
    buffer.clear();
}