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 . j a  v a2 s  . c o 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]);
    }

    System.out.println("");
    System.out.flush();
}

From source file:MainClass.java

public static void main(String[] argv) throws Exception {
    CharBuffer buffer = CharBuffer.allocate(8);
    buffer.position(3).limit(5);/*from w w w.  java  2s .c  o m*/
    CharBuffer sliceBuffer = buffer.slice();

    println(buffer);
    println(sliceBuffer);

    char[] myBuffer = new char[100];
    CharBuffer cb = CharBuffer.wrap(myBuffer);

    cb.position(12).limit(21);

    CharBuffer sliced = cb.slice();

    println(cb);
    println(sliced);
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    CharBuffer cb1 = CharBuffer.allocate(20);
    cb1.append("java2s.com");
    cb1.rewind();// w  w  w .  j a v a2s .c o m

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

    CharBuffer cb2 = CharBuffer.allocate(50);
    cb2.read(cb1);

    System.out.println(cb2);

}

From source file:MainClass.java

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

    cb.put("Hello World");
    println(cb);//w w  w . ja v  a2 s  .  com

    cb.flip();
    println(cb);

    cb.flip();
    println(cb);
}

From source file:Main.java

public static void main(String[] args) {

    String s = "tutorial from java2s.com";

    CharBuffer cb = CharBuffer.allocate(100);

    Reader reader = new StringReader(s);

    try {/*from   w w  w .j  a  v  a  2 s  .c  o m*/
        reader.read(cb);

        cb.flip();

        // print the char buffer
        System.out.println(cb.toString());

        reader.close();

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:BufferEquality.java

public static void main(String[] args) {
    CharBuffer cb1 = CharBuffer.allocate(5), cb2 = CharBuffer.allocate(5);
    cb1.put('B').put('u').put('f').put('f').put('A');
    cb2.put('B').put('u').put('f').put('f').put('B');
    cb1.rewind();/*w  w w. ja va  2 s  .  c o m*/
    cb2.rewind();
    System.out.println(cb1.limit(4).equals(cb2.limit(4)));
    // Should be "true"
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Charset charset = Charset.forName("ISO-8859-1");
    CharsetDecoder decoder = charset.newDecoder();

    CharBuffer cbuf = CharBuffer.allocate(10);

    decoder.flush(cbuf);//from  www .  ja v a  2 s .  c o  m
}

From source file:Main.java

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

    String s = "from java2s.com";

    StringReader sr = new StringReader(s);

    // create new buffered reader
    BufferedReader br = new BufferedReader(sr);

    // Destination source is created
    CharBuffer target = CharBuffer.allocate(s.length());

    // ready is invoked to test if character stream is ready
    if (br.ready()) {
        br.read(target);//from   ww w  .  j a  v  a  2 s .co m
    }
    System.out.print(target.array());

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Charset charset = Charset.forName("ISO-8859-1");
    CharsetDecoder decoder = charset.newDecoder();
    CharsetEncoder encoder = charset.newEncoder();
    ByteBuffer bbuf = ByteBuffer.allocateDirect(1024);

    CharBuffer cbuf = CharBuffer.allocate(1024);

    encoder.encode(cbuf, bbuf, false);//from  w  ww.j a va  2 s. c o m

    bbuf.flip();

    decoder.decode(bbuf, cbuf, false);

    cbuf.flip();

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Charset charset = Charset.forName("ISO-8859-1");
    CharsetDecoder decoder = charset.newDecoder();
    CharsetEncoder encoder = charset.newEncoder();

    ByteBuffer bbuf = encoder.encode(CharBuffer.wrap("a string"));

    CharBuffer cbuf = CharBuffer.allocate(10);

    decoder.decode(bbuf, cbuf, true);//from w w  w  .j  av  a 2 s .  c  om
    String s = cbuf.toString();
}