Example usage for java.nio CharBuffer wrap

List of usage examples for java.nio CharBuffer wrap

Introduction

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

Prototype

public static CharBuffer wrap(CharSequence chseq) 

Source Link

Document

Creates a new char buffer by wrapping the given char sequence.

Usage

From source file:Main.java

public static void main(String[] args) {
    CharBuffer cb1 = CharBuffer.wrap("java2s.com");
    System.out.println(cb1.toString());

}

From source file:Main.java

public static void main(String[] args) {
    CharBuffer cb1 = CharBuffer.wrap(new char[] { 'j', });
    System.out.println(cb1.toString());

}

From source file:MainClass.java

public static void main(String[] argv) throws Exception {
    CharBuffer buffer = CharBuffer.wrap("01234567");

    buffer.position(3).limit(6).mark().position(5);

    CharBuffer dupeBuffer = buffer.duplicate();

    buffer.clear();/*w  w  w  .ja  va2 s  .  c  o m*/

    println(buffer);
    println(dupeBuffer);

    dupeBuffer.reset();
    println(dupeBuffer);

    dupeBuffer.clear();
    println(dupeBuffer);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    CharsetEncoder encoder = Charset.forName("US-ASCII").newEncoder();

    String response = "java2s.com";
    System.out.println(encoder.encode(CharBuffer.wrap(response)));
}

From source file:MainClass.java

public static void main(String argv[]) {
    ByteBuffer bb = ByteBuffer.allocate(100);

    bb.mark();/*from  www.j  av  a2 s  . c  om*/
    bb.position(5);
    bb.reset();

    bb.mark().position(5).reset();

    char[] myBuffer = new char[100];

    CharBuffer cb = CharBuffer.wrap(myBuffer);
    cb.position(12).limit(21);

    CharBuffer sliced = cb.slice();

    System.out.println("Sliced: offset=" + sliced.arrayOffset() + ", capacity=" + sliced.capacity());
}

From source file:MainClass.java

public static void main(String[] argv) throws Exception {
    CharBuffer buffer = CharBuffer.allocate(8);
    buffer.position(3).limit(5);/*from www  .j  a  v a2  s  .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 Exception {
    CharsetEncoder encoder = Charset.forName("US-ASCII").newEncoder();

    String response = "java2s.com";

    ByteBuffer bb = ByteBuffer.allocate(10);

    System.out.println(encoder.encode(CharBuffer.wrap(response), bb, 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();
    CharsetEncoder encoder = charset.newEncoder();

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

    CharBuffer cbuf = decoder.decode(bbuf);
    String s = cbuf.toString();/*from   w  w  w .  jav  a 2  s  .co  m*/
}

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  ww.  j a v  a2s .  c om*/
    String s = cbuf.toString();
}

From source file:MainClass.java

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

    CharsetEncoder encoder = Charset.forName("US-ASCII").newEncoder();

    ServerSocketChannel server = ServerSocketChannel.open();
    server.socket().bind(new java.net.InetSocketAddress(8000));

    for (;;) { // This server runs forever
        SocketChannel client = server.accept();
        String response = new java.util.Date().toString() + "\r\n";
        client.write(encoder.encode(CharBuffer.wrap(response)));
        client.close();//w w  w  .  j  a v  a  2  s  . c om
    }
}